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

2019-08-12 11:58发布

Trying to create a classic tic tac toe OOP in Ruby but am having trouble with my game_results() method.

I realize this is not really complete and needs some more functionality but for now im just trying to fill my board with the inputted objects from the user and outputting the filled board and winner.

When I can see on my board that we have a winner I call for the game_results() method and it gives me the correct winner but whenever a tie game occurs, I receive a error.

Anybody got any ideas or solutions to what I'm doing wrong with the operators? Btw I know all this is pretty messy, but im a beginner.

#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

0条回答
登录 后发表回答