variable initialize attribute ruby oop hash attributes attr

initialize - Ruby OOP tic-tac-toe hash/condicional/itiration



ruby attribute reader (0)

Estoy tratando de crear un OOP clásico de game_results() en Ruby pero estoy teniendo problemas con mi método game_results() .

Me doy cuenta de que esto no está completo y que necesita algunas funcionalidades más, pero por ahora estoy tratando de llenar mi tablero con los objetos ingresados ​​por el usuario y sacar el tablero lleno y el ganador.

Cuando puedo ver en mi tablero que tenemos un ganador, llamo por el método game_results() y me da el ganador correcto, pero cada vez que ocurre un juego de empate, recibo un error.

¿Alguien tiene alguna idea o solución sobre lo que estoy haciendo mal con los operadores? Por cierto, sé que todo esto es bastante complicado, pero soy un principiante.

#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