Ruby OOP tic-tac-toe hash / conditional / itiration

试图在Ruby中创建一个经典的tic tac toe OOP但是我的game_results()方法遇到了麻烦。

我意识到这不是很完整,需要更多的function,但是现在我只是试图用我从用户输入的对象填充我的电路板并输出填充板和赢家。

当我在我的主板上看到我们有一个胜利者时,我呼吁使用game_results()方法并且它给了我正确的赢家,但是每当发生平局游戏时,我都会收到错误。

有人对操作员的错误有什么想法或解决方案吗? 顺便说一下,我知道这一切都很混乱,但我是初学者。

 #require "pry" class Game attr_reader :turn def initialize @turn = 1 @board = { a1: " ", a2: " ", a3: " ", b1: " ", b2: " ", b3: " ", c1: " ", c2: " ", c3: " " } end # def start_game # x = Game.new # x.player_symbol # x.player_turn # x.check_game # end def intro puts "ULTIMATE TIC TAC TOE .. in ruby!\n" display_board end def player_symbol puts "Player 1, choose your marker. X or O?\n" i = gets.chomp.upcase if i == "X" @p1 = "X" @p2 = "O" elsif i == "O" @p1 = "O" @p2 = "X" else puts "That is not a valid player!" end end def player_turn if @turn == 1 puts "Player 1 turn." @turn = 2 player_move(@p1) else puts "Player 2 turn." @turn = 1 player_move(@p2) end end def display_board # Displays board grid using hash values. puts " 1 2 3 " puts "A #{@board[:a1]} | #{@board[:a2]} | #{@board[:a3]} " puts " ---+---+---" puts "B #{@board[:b1]} | #{@board[:b2]} | #{@board[:b3]} " puts " ---+---+---" puts "C #{@board[:c1]} | #{@board[:c2]} | #{@board[:c3]} " end def player_move(n) # Player picks square to claim. # Player symbol populates hash. puts "pick move" x = gets.chomp.downcase.to_sym @board[x] = "#{n}" # Directs to player X/O display_board end def game_results # Determines winner, loser, or tie game if ((@board[:a1] == @p1) && (@board[:a2] == @p1) && (@board[:a3] == @p1)) || ((@board[:b1] == @p1) && (@board[:b2] == @p1) && (@board[:b3] == @p1)) || ((@board[:c1] == @p1) && (@board[:c2] == @p1) && (@board[:c3] == @p1)) || ((@board[:a1] == @p1) && (@board[:b1] == @p1) && (@board[:c1] == @p1)) || ((@board[:a2] == @p1) && (@board[:b2] == @p1) && (@board[:c2] == @p1)) || ((@board[:a3] == @p1) && (@board[:b3] == @p1) && (@board[:c3] == @p1)) || ((@board[:a1] == @p1) && (@board[:b2] == @p1) && (@board[:c3] == @p1)) || ((@board[:a3] == @p1) && (@board[:b2] == @p1) && (@board[:c1] == @p1)) puts "Player #{@p1} is the winner!" elsif ((@board[:a1] == @p2) && (@board[:a2] == @p2) && (@board[:a3] == @p2)) || ((@board[:b1] == @p2) && (@board[:b2] == @p2) && (@board[:b3] == @p2)) || ((@board[:c1] == @p2) && (@board[:c2] == @p2) && (@board[:c3] == @p2)) || ((@board[:a1] == @p2) && (@board[:b1] == @p2) && (@board[:b1] == @p2)) || ((@board[:a2] == @p2) && (@board[:b2] == @p2) && (@board[:c2] == @p2)) || ((@board[:a3] == @p2) && (@board[:b3] == @p2) && (@board[:c3] == @p2)) || ((@board[:a1] == @p2) && (@board[:b2] == @p2) && (@board[:c3] == @p2)) || ((@board[:a3] == @p2) && (@board[:b2] == @p2) && (@board[:c1] == @p2)) puts "Player #{@p2} is the winner!" elsif ((@board[:a1] == ("X" || "O")) && (@board[:a2] == ("X" || "O")) && (@board[:a3] == ("X" || "O")) && (@board[:b1] == ("X" || "O")) && (@board[:b2] == ("X" || "O")) && (@board[:b3] == ("X" || "O")) && (@board[:c1] == ("X" || "O")) && (@board[:c2] == ("X" || "O")) && (@board[:c3] == ("X" || "O"))) # This should represent that empty elements in the hash are now filled not making a winning pair puts "Tie Game" else player_turn end end end 

Interesting Posts