Class: RubyTictactoe::Game
- Inherits:
-
Object
- Object
- RubyTictactoe::Game
- Includes:
- TictactoeConstants
- Defined in:
- lib/game.rb
Constant Summary
Constants included from TictactoeConstants
TictactoeConstants::AI_PLAYER, TictactoeConstants::COMPUTER_PLAYER, TictactoeConstants::EASY_LEVEL, TictactoeConstants::HARD_LEVEL, TictactoeConstants::HUMAN_PLAYER, TictactoeConstants::MARKER_O, TictactoeConstants::MARKER_X
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#player_first_move ⇒ Object
Returns the value of attribute player_first_move.
-
#player_one ⇒ Object
Returns the value of attribute player_one.
-
#player_two ⇒ Object
Returns the value of attribute player_two.
-
#ui ⇒ Object
Returns the value of attribute ui.
Instance Method Summary collapse
- #advance_game ⇒ Object
- #current_player ⇒ Object
- #game_status_check(marker) ⇒ Object
-
#initialize(settings) ⇒ Game
constructor
A new instance of Game.
- #total_markers(marker) ⇒ Object
- #verify_move(cell) ⇒ Object
Constructor Details
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
5 6 7 |
# File 'lib/game.rb', line 5 def board @board end |
#player_first_move ⇒ Object
Returns the value of attribute player_first_move.
5 6 7 |
# File 'lib/game.rb', line 5 def player_first_move @player_first_move end |
#player_one ⇒ Object
Returns the value of attribute player_one.
5 6 7 |
# File 'lib/game.rb', line 5 def player_one @player_one end |
#player_two ⇒ Object
Returns the value of attribute player_two.
5 6 7 |
# File 'lib/game.rb', line 5 def player_two @player_two end |
#ui ⇒ Object
Returns the value of attribute ui.
5 6 7 |
# File 'lib/game.rb', line 5 def ui @ui end |
Instance Method Details
#advance_game ⇒ Object
14 15 16 17 |
# File 'lib/game.rb', line 14 def advance_game game_status_check(current_player.opponent.marker) ui.(current_player.marker) unless board.game_over? end |
#current_player ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/game.rb', line 33 def current_player if total_markers(MARKER_X) > total_markers(MARKER_O) player_two elsif total_markers(MARKER_O) > total_markers(MARKER_X) player_one else player_first_move end end |
#game_status_check(marker) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/game.rb', line 19 def game_status_check(marker) if board.winner?(marker) ui.(marker) elsif !board.moves_remaining? ui. end end |
#total_markers(marker) ⇒ Object
43 44 45 |
# File 'lib/game.rb', line 43 def total_markers(marker) board.all_cells.select { |cell, value| value == marker }.count end |
#verify_move(cell) ⇒ Object
27 28 29 30 31 |
# File 'lib/game.rb', line 27 def verify_move(cell) return false if !board.available_cell?(cell) current_player.add_marker(board, cell) true end |