Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/nick_tac_toe/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#current_player ⇒ Object
readonly
Returns the value of attribute current_player.
-
#player_one ⇒ Object
readonly
Returns the value of attribute player_one.
-
#player_two ⇒ Object
readonly
Returns the value of attribute player_two.
Instance Method Summary collapse
- #draw? ⇒ Boolean
-
#initialize(player_one, player_two) ⇒ Game
constructor
A new instance of Game.
- #make_move(position) ⇒ Object
- #over? ⇒ Boolean
- #player_won?(player) ⇒ Boolean
- #take_next_turn ⇒ Object
- #winner ⇒ Object
- #winner_in_group(group) ⇒ Object
- #won? ⇒ Boolean
Constructor Details
#initialize(player_one, player_two) ⇒ Game
Returns a new instance of Game.
6 7 8 9 10 11 |
# File 'lib/nick_tac_toe/game.rb', line 6 def initialize(player_one, player_two) @board = Board.new(player_one.team, player_two.team) @player_one = player_one @player_two = player_two @current_player = player_one end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
4 5 6 |
# File 'lib/nick_tac_toe/game.rb', line 4 def board @board end |
#current_player ⇒ Object (readonly)
Returns the value of attribute current_player.
4 5 6 |
# File 'lib/nick_tac_toe/game.rb', line 4 def current_player @current_player end |
#player_one ⇒ Object (readonly)
Returns the value of attribute player_one.
4 5 6 |
# File 'lib/nick_tac_toe/game.rb', line 4 def player_one @player_one end |
#player_two ⇒ Object (readonly)
Returns the value of attribute player_two.
4 5 6 |
# File 'lib/nick_tac_toe/game.rb', line 4 def player_two @player_two end |
Instance Method Details
#draw? ⇒ Boolean
26 27 28 29 |
# File 'lib/nick_tac_toe/game.rb', line 26 def draw? return false if won? return board.cells.all? {|cell| cell == @player_one.team || cell == @player_two.team } end |
#make_move(position) ⇒ Object
17 18 19 20 |
# File 'lib/nick_tac_toe/game.rb', line 17 def make_move(position) @board.set_cell(position, current_player.team) toggle_current_player end |
#over? ⇒ Boolean
22 23 24 |
# File 'lib/nick_tac_toe/game.rb', line 22 def over? return true if draw? || won? end |
#player_won?(player) ⇒ Boolean
41 42 43 44 45 |
# File 'lib/nick_tac_toe/game.rb', line 41 def player_won?(player) return true if board.rows.any? { |row| row == [player.team]*3 } return true if board.columns.any? { |column| column == [player.team]*3 } return true if board.diagonals.any? { |diagonal| diagonal == [player.team]*3 } end |
#take_next_turn ⇒ Object
13 14 15 |
# File 'lib/nick_tac_toe/game.rb', line 13 def take_next_turn make_move(current_player.move_for(board)) end |
#winner ⇒ Object
47 48 49 50 |
# File 'lib/nick_tac_toe/game.rb', line 47 def winner return player_one if player_won?(player_one) return player_two if player_won?(player_two) end |
#winner_in_group(group) ⇒ Object
37 38 39 |
# File 'lib/nick_tac_toe/game.rb', line 37 def winner_in_group(group) group == [@player_two.team]*3 || group == [@player_one.team]*3 end |
#won? ⇒ Boolean
31 32 33 34 35 |
# File 'lib/nick_tac_toe/game.rb', line 31 def won? return true if board.rows.any? { |row| winner_in_group(row) } return true if board.columns.any? { |column| winner_in_group(column) } return true if board.diagonals.any? { |diagonal| winner_in_group(diagonal) } end |