如果/ elsif / else语句直接跳到其他地方

时间:2014-11-28 19:10:27

标签: ruby arrays if-statement

我有下面的if语句,但不管我给矩阵的什么输入(这是一个数组,而不是我不能改名),控制台只输出“下一轮!”。控制台非常高兴地显示矩阵[0],矩阵[1]和矩阵[2]都是“0”,然后继续直接到else语句。该陈述有什么问题吗?

def checkwinner
            if @matrix[0] == "0" && @matrix[1] == "0" && @matrix[2] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[3] == "0" && @matrix[4] == "0" && @matrix[5] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[6] == "0" && @matrix[7] == "0" && @matrix[8] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[0] == "0" && @matrix[3] == "0" && @matrix[6] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[1] == "0" && @matrix[4] == "0" && @matrix[7] == "0" then
                puts "Player 1 wins!"
                finish
                exit    
            elsif @matrix[2] == "0" && @matrix[5] == "0" && @matrix[8] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[0] == "0" && @matrix[4] == "0" && @matrix[8] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[2] == "0" && @matrix[4] == "0" && @matrix[6] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[0] == "1" && @matrix[1] == "1" && @matrix[2] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[3] == "1" && @matrix[4] == "1" && @matrix[5] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[6] == "1" && @matrix[7] == "1" && @matrix[8] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[0] == "1" && @matrix[3] == "1" && @matrix[6] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[1] == "1" && @matrix[4] == "1" && @matrix[7] == "1" then
                puts "Player 2 wins!"
                finish
                exit    
            elsif @matrix[2] == "1" && @matrix[5] == "1" && @matrix[8] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[0] == "1" && @matrix[4] == "1" && @matrix[8] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[2] == "1" && @matrix[4] == "1" && @matrix[6] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            else puts "Next turn!"      
            end
        end

以下整个代码文件:

# Ruby code file - All your code should be located between the comments provided.

# Add any additional gems and global variables here
# require 'sinatra'     # remove '#' character to run sinatra wen server

# Main class module
module OXs_Game
# Input and output constants processed by subprocesses. MUST NOT change.
NOUGHT = 0
CROSS = 1

class Game
    attr_reader :matrix, :input, :output, :player1, :player2, :winner
    attr_writer :matrix, :input, :output, :player1, :player2, :winner

    def initialize(input, output)
        @input = input
        @output = output
    end

    # Any code/methods aimed at passing the RSpect tests should be added below.
        def start
            @output.puts "Welcome to Noughts and Crosses!"
            @output.puts "Starting game..."
            @output.puts "Created by:Stephen Mitchell"
            @output.puts "Player 1: 0 and Player 2: 1"
        end

        def created_by
            return "Stephen Mitchell"
        end

        def student_id
            return 51441219
        end

        def setplayer1
            @player1 = 0
        end

        def setplayer2
            @player2 = 1
        end

        def clearmatrix
            @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
        end

        def getmatrixvalue(n)
            @matrix[n]
        end

        def setmatrixvalue(i, v)
            @i = 1
            @v = "0"
            @matrix[i] = "0"            
        end

        def displaykey(matrix)
            @matrix = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
            @output.puts "Table key:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
        end

        def displaymatrix
            @matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
            @output.puts "Table status:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
        end

        def finish
            @output.puts "Finishing game..."
        end

        def displaymenu
            @output.puts "Menu: (1)Start | (2)New | (9)Exit\n"
        end

        def checkwinner
            if @matrix[0] == "0" && @matrix[1] == "0" && @matrix[2] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[3] == "0" && @matrix[4] == "0" && @matrix[5] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[6] == "0" && @matrix[7] == "0" && @matrix[8] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[0] == "0" && @matrix[3] == "0" && @matrix[6] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[1] == "0" && @matrix[4] == "0" && @matrix[7] == "0" then
                puts "Player 1 wins!"
                finish
                exit    
            elsif @matrix[2] == "0" && @matrix[5] == "0" && @matrix[8] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[0] == "0" && @matrix[4] == "0" && @matrix[8] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[2] == "0" && @matrix[4] == "0" && @matrix[6] == "0" then
                puts "Player 1 wins!"
                finish
                exit
            elsif @matrix[0] == "1" && @matrix[1] == "1" && @matrix[2] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[3] == "1" && @matrix[4] == "1" && @matrix[5] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[6] == "1" && @matrix[7] == "1" && @matrix[8] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[0] == "1" && @matrix[3] == "1" && @matrix[6] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[1] == "1" && @matrix[4] == "1" && @matrix[7] == "1" then
                puts "Player 2 wins!"
                finish
                exit    
            elsif @matrix[2] == "1" && @matrix[5] == "1" && @matrix[8] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[0] == "1" && @matrix[4] == "1" && @matrix[8] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            elsif @matrix[2] == "1" && @matrix[4] == "1" && @matrix[6] == "1" then
                puts "Player 2 wins!"
                finish
                exit
            else puts "Next turn!"      
            end
        end


    # Any code/methods aimed at passing the RSpect tests should be added above.



end
end

主程序

# Main program
module OXs_Game
@input = STDIN
@output = STDOUT
g = Game.new(@input, @output)
matrixkey = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
playing = true
input = ""
option = 0
turn = 0

# Any code added to output the activity messages to the command line window should be added below.

g.start
g.displaykey(matrixkey)
g.displaymatrix
puts "Please select one of the following:"
g.displaymenu
menu_input = gets.chomp.to_s


while matrix.include? ("_") do
    puts "Table status:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
    g.checkwinner
    turn += 1
    puts "player1 turn"
    x = gets.chomp.to_s
    case x
        when "1"
            matrix[0] = "0"
        when "2"
            matrix[1] = "0"
        when "3"
            matrix[2] = "0"
        when "4"
            matrix[3] = "0"
        when "5"
            matrix[4] = "0" 
        when "6"
            matrix[5] = "0"
        when "7"
            matrix[6] = "0" 
        when "8"
            matrix[7] = "0" 
        when "9"
            matrix[8] = "0" 
    end 
    puts "Table status:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
    g.checkwinner
    turn +=1
    puts "player2 turn"
    y = gets.chomp.to_s
    case y
        when "1"
            matrix[0] = "1"
        when "2"
            matrix[1] = "1"
        when "3"
            matrix[2] = "1"
        when "4"
            matrix[3] = "1"
        when "5"
            matrix[4] = "1" 
        when "6"
            matrix[5] = "1"
        when "7"
            matrix[6] = "1" 
        when "8"
            matrix[7] = "1" 
        when "9"
            matrix[8] = "1" 
    end 
end

# Any code added to output the activity messages to the command line window should be added above.

end

2 个答案:

答案 0 :(得分:3)

您正在游戏环中修改matrix的本地版本(while matrix.include?循环)。

但是,在checkwinner实例Game上调用了{p> gGame#checkwinner正在检查实例变量@matrix,它 与您在游戏循环中操作的matrix相同。

您不会修改您检查的矩阵,因此checkwinner将始终查看相同的数据。

这里有一个非常混乱的本地变量和实例变量,这表明你对你正在做的事情或者你应该如何做这件事的重大误解。

转弯(玩家'移动)应该操纵Game的{​​{1}}实例变量。

答案 1 :(得分:0)

这不是你问题的真正答案,而是提示如何改进你的编码风格。

您的代码中有很多重复,如果您试图避免这种情况,您的代码将会受益。例如,你的``checkwinner`方法可以简化为:

PATTERNS = [
  [1,2,3], [4,5,6], [7,8,9],  # rows
  [1,4,6], [2,5,8], [3,6,9],  # columns
  [1,5,9], [3,5,7]            # diagonal
]

def checkwinner
  PATTERNS.each do |pattern|
    values = pattern.map { |position| @matrix[position] }.uniq

    winner = 1 if values == ['0']
    winner = 2 if values == ['1']

    if winner
      puts "Player #{winner} wins!"
      finish
      exit
    end
  end

  puts "Next turn!"
end

或者代替case循环中的长while块,请考虑以下内容:

if ('1'..'9').include?(x)
  matrix[x.to_i - 1] = '0'
end

...

if ('1'..'9').include?(y)
  matrix[y.to_i - 1] = '1'
end