class Game
Constants
- WIN_COMBINATIONS
Attributes
board[RW]
player_1[RW]
player_2[RW]
Public Class Methods
new(player_1 = Players::Human.new("X"), player_2 = Players::Human.new("O"), board = Board.new)
click to toggle source
# File lib/game.rb, line 15 def initialize(player_1 = Players::Human.new("X"), player_2 = Players::Human.new("O"), board = Board.new) @board = board @player_1 = player_1 @player_2 = player_2 end
Public Instance Methods
current_player()
click to toggle source
# File lib/game.rb, line 21 def current_player self.board.turn_count.even? ? self.player_1 : self.player_2 end
draw?()
click to toggle source
# File lib/game.rb, line 43 def draw? if won? false elsif !self.board.full? false else true end end
over?()
click to toggle source
# File lib/game.rb, line 53 def over? if draw? return true elsif won? return true else false end end
play()
click to toggle source
# File lib/game.rb, line 86 def play turn until over? if draw? puts "\n>>>Cat's Game!<<<".red elsif over? puts "\n>>>Congratulations #{winner}!<<<".blue end end
turn()
click to toggle source
# File lib/game.rb, line 74 def turn input = self.current_player.move(self.board).to_i player = current_player if self.board.valid_move?(input) self.board.update(input, player) self.board.display else turn end end
winner()
click to toggle source
# File lib/game.rb, line 63 def winner sub_array = won? if won? == nil nil elsif self.board.cells[sub_array[1]] == "X" "X" elsif self.board.cells[sub_array[1]] == "O" "O" end end
won?()
click to toggle source
# File lib/game.rb, line 25 def won? #binding.pry result = nil WIN_COMBINATIONS.each do |sub_array| index_1 = sub_array[0] index_2 = sub_array[1] index_3 = sub_array[2] board_index_1 = self.board.cells[index_1] board_index_2 = self.board.cells[index_2] board_index_3 = self.board.cells[index_3] if (board_index_1 == "X" && board_index_2 == "X" && board_index_3 == "X") || (board_index_1 == "O" && board_index_2 == "O" && board_index_3 == "O") result = sub_array end end result end