简体   繁体   English

这个循环有什么问题?

[英]What's wrong with this loop?

    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.嘿伙计们,我正在尝试将这个迷你游戏放在 ruby 中,这取决于猜测数字。 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.但是,对于当前的代码,我被困在第 2 轮。很可能由于某种原因正在重置这些变量,但我似乎可以指出它是什么。

Would be grateful for any kind of help.将不胜感激任何帮助。

::Edit:: ::编辑::

Code updated.代码已更新。 Problem solved, Thanks for the help @blackbird07, @robertodecurnex and @fl00r!问题已解决,感谢@blackbird07、@robertodecurnex 和@fl00r 的帮助!

Controller is stateless so of course all variables are reseted each time you call it. Controller 是无状态的,所以每次调用它时都会重置所有变量。

You should use some datastorage (database, filesystem) to store your current state.您应该使用一些数据存储(数据库、文件系统)来存储您当前的 state。

And another your problem is that all this code shouldn't belong to controller at all.你的另一个问题是,所有这些代码根本不应该属于 controller。

Since you are keeping the round on the session you should use the session value instead of the instance variable or at least create a filter to set this variable on every request.由于您在 session 上保持回合,因此您应该使用 session 值而不是实例变量,或者至少创建一个过滤器以在每个请求上设置此变量。

Using the filter:使用过滤器:

before_filter :set_round

private

def set_round
  @round = session[:round]
end

The new method (action) is making the following:新方法(动作)正在执行以下操作:

if @round = 0 then

It's assigning 0 to @round instead of compare it.它将0分配给@round而不是比较它。

Try if @round == 0 or just if @round.zero?试试if @round == 0还是if @round.zero?

You will need also to increase the session value and not just the @round variable one.您还需要增加 session 值,而不仅仅是@round变量之一。

Remember:记住:

=     #=> Assignation
==    #=> Equality, usually overwritten by the classes to return true base on the equality of the state/attributes of two objects.
===   #=> Equality, usually defined to compare the identity of two objects (to return true only if both objects are actually the same object).

You do not increase the counters for session[:round] and session[:points] in your next_round action.您不会在 next_round 操作中增加 session[:round] 和 session[:points] 的计数器。 Do this and it should work.这样做,它应该工作。

session[:round] += 1
session[:points] += 1200
@round = session[:round]
@points = session[:points]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM