Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/game.rb

Constant Summary collapse

WIN_COMBINATIONS =
[[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player_1 = Players::Human.new("X"), player_2 = Players::Human.new("O"), board = Board.new) ⇒ Game

Returns a new instance of Game.



15
16
17
18
19
# 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

Instance Attribute Details

#boardObject

Returns the value of attribute board.



4
5
6
# File 'lib/game.rb', line 4

def board
  @board
end

#player_1Object

Returns the value of attribute player_1.



4
5
6
# File 'lib/game.rb', line 4

def player_1
  @player_1
end

#player_2Object

Returns the value of attribute player_2.



4
5
6
# File 'lib/game.rb', line 4

def player_2
  @player_2
end

Instance Method Details

#current_playerObject



21
22
23
# File 'lib/game.rb', line 21

def current_player
self.board.turn_count.even? ? self.player_1 : self.player_2
end

#draw?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
# File 'lib/game.rb', line 43

def draw?
    if won?
      false
    elsif !self.board.full?
      false
    else
      true
  end
end

#over?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'lib/game.rb', line 53

def over?
   if draw?
     return true
  elsif won?
    return true
  else
    false
  end
end

#playObject



86
87
88
89
90
91
92
93
# 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

#turnObject



74
75
76
77
78
79
80
81
82
83
# 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

#winnerObject



63
64
65
66
67
68
69
70
71
72
# 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?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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