class GameController < ApplicationController
def index
@games = Game.all
respond_to do |format|
format.html
end
end
def start_game
session[:round] ||= 1
session[:points] ||= 0
@round = session[:round]
@points = session[:points]
end
def next_round
session[:round] += 1
session[:points] += 1200
@round = session[:round]
@points = session[:points]
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
if @round == nil
start_game
generate_round
else
generate_round
end
if session[:addition]
if not session[:addition].index(params[:guess].to_i).nil?
puts "Correct."
next_round
else
puts 'Game over.'
end
end
session[:addition] = @addition
respond_to do |format|
format.html
end
end
end
Hey guys,
I`m trying to put together this mini game in ruby that depends on guessing numbers.
After every guess points are added and the level is increased by one.
However with the current code, I`m getting stuck at round 2.
Most likely something is resetting those variables for some reason but I can seem to pin point what it is.
Would be grateful for any kind of help.
::Edit::
Code updated. Problem solved!
Thanks for the help @blackbird07, @robertodecurnex and @fl00r!
You do not increase the counters for session[:round] and session[:points] in your next_round action. Do this and it should work.