最近两个星期,我一直在开发Sinatra应用程序,而我遇到了第一个主要障碍。最近3天,我一直在寻找解决方案,但需要寻求帮助。
我正在使用Sinatra开发一个小型游戏,但在理解一个概念时遇到了麻烦。
游戏跟踪回合和得分。
这是我的一些代码:
#Player Class
class Player
attr_accessor :name, :guess, :round_score, :total_score
def initialize
@guess = 0
@round_score = 0
@total_score = 0
end
end
#Tracks Round
class RoundCount
attr_accessor :round
def initialize
@round = 0
end
def count
@round += 1
end
end
helpers do
#New game function. Makes set number of player classes in @@player array.
def new_game(players)
@@i = RoundCount.new
@@player = []
players.times do |x|
@@player[x] = Player.new
@@player[x].name = "Player#{x}"
end
end
#Calculates score
def player_score()
params.each do |x, y|
@@player.each do |z|
if z.name == x
z.guess = y.to_i
z.round_score = (y.to_i - @@movie[@@i.round].ratings['critics_score'].to_i).abs
z.total_score += (y.to_i - @@movie[@@i.round].ratings['critics_score'].to_i).abs
end
end
end
end
get '/' do
slim :home
end
get '/players' do
slim :players
end
post '/numplayers' do
new_game(params[:numplayers].to_i)
slim :names
end
post '/names' do
slim :titles
end
当前,该程序给我错误“警告:从topelevel访问类变量”,并且受到同时在不同计算机上玩的其他用户的影响。
我需要帮助来了解如何在每个视图中跟踪玩家得分和游戏回合,而不受其他运行其他游戏的玩家的影响。
关于如何避免在轮次和得分中使用类变量,同时保留在每个路径和视图中访问它们的方式,有何建议?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句