get '/blackjack/*' do
if params[:splat] == "/hit" and defined? session[:bj_game]
erb :blackjack
elsif params[:splat] == "/fold" and defined? session[:bj_game]
session[:bj_hum].fold = true
erb :blackjack
else
if defined? session[:bj_game]
new_session_check
score_check
erb :blackjack
else
session[:bj_game] = false
session[:score] = 0
new_session_check
score_check
erb :blackjack
end
end
end
def new_session_check
if session[:bj_game] == false
session[:bj_hum] = Blackjack.new
session[:bj_com] = Blackjack.new
session[:bj_game] = true
end
end
def score_check
if session[:bj_hum].game_loop == false
if session[:bj_hum].score.to_i > 0
session[:score] += session[:bj_hum].score.to_i
check_save(session[:score])
else
session[:score] = 0
end
session[:bj_game] = false
session[:bj_hum] = Blackjack.new
session[:bj_com] = Blackjack.new
end
end
每当它进行得分检查时,我都会得到 NoMethodError - NilClass 的未定义方法“game_loop”。
但是,如果我从主页开始:
get '/' do
session[:bj_game] = false
session[:score] = 0
erb :home
end
然后单击 home erb 中的 /blackjack 链接,它将工作,因为 new_session_check 看到变量为假,然后创建了 Blackjack 类的新实例(它确实具有用于 game_loop 的 attr_accessor)。
为什么它没有在 get '/blackjack' 版本中注册?
http://pastebin.com/6EFpp5gh - 这是一个模型版本,您可以运行它来验证这一点。首先去 localhost:4567/blackjack ,你会得到一个内部服务错误。重新启动服务器,然后先访问 localhost:4567,然后再访问 localhost:4567/blackjack,您会看到它可以工作。