如何:在比赛中获得积分和分数?

class GameController  @games } end end def start_game session[:round] = 1 session[:points] = 0 @round = session[:round] @points = session[:points] end def next_round @round += 1 @points += 1200 end def game_over puts "Game Over." end def highscore puts "Leaderboard action here." end def generate_round numbers = Array.new(6){rand(9)} @addition = [] @display = numbers numbers.inject do |s, i| @addition << s + i @addition.last end end def new start_game generate_round puts @points puts @round if session[:addition] if not session[:addition].index(params[:guess].to_i).nil? puts "Correct." next_round else game_over puts "Wrong anwser." highscore end end session[:addition] = @addition respond_to do |format| format.html end end end 

嘿伙计们,所以我试图在rails中构建一个简单的游戏。 它的前提是在你的记忆中添加所有数字,并在预定义的时间限制内输入最高金额。 现在我能够生成数字并检查正确的答案,但是我不知道如何以每次成功回合后添加点的方式循环方法。

我假设问题是,对于每个“new”实例,它再次设置start_game方法,从而将点归零并再次舍入?

你是对的,每次你点击“新”动作,它都会重新开始你的游戏。 您可以执行类似下面的操作,以便将多个请求保留为“新”

  def start_game session[:round] ||= 1 session[:points] ||= 0 @round = session[:round] @points = session[:points] end def game_over session[:round] = nil session[:points] = nil puts "Game Over." end