Topic: Looping actions without saving (guessing game)

Hey guys,

So I`m trying to build a simple game in rails.
It`s premise is to add all the numbers up in your memory and type in the highest sum you can within a predefined time limit.

Right now I able to generate the numbers and check for correct answers, however I don`t know how to loop the method in a way that points are added after each successful round.


class GameController < ApplicationController

  def index
    @games = Game.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @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
  if @round < 0
    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
        game_over
        puts "Wrong anwser."
        highscore
     end
  end
  session[:addition] = @addition
  respond_to do |format|
      format.html 
    end
  end   
end

Last edited by Big_Bird (2011-07-13 11:17:10)

Re: Looping actions without saving (guessing game)

Why don't you use a function in the corresponding  model with a fitting variable? The Rails MVC-paradigma expects the business logic wrapped in the model.

Thomas

Re: Looping actions without saving (guessing game)

Mainly due to lack of knowledge of rails etc.

Second, because this was first written as a ruby script and I`m trying my best to transfer it to rails.

What would be an example of such a function?

::Edit::

Anyone able to help with this one?

Last edited by Big_Bird (2011-07-13 11:14:37)