Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/ruby_ttt.rb
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
#initialize(settings) ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 16 |
# File 'lib/ruby_ttt.rb', line 10 def initialize(settings) @board = settings[:board] @player_one = settings[:player_one] @player_two = settings[:player_two] @player_first_move = settings[:player_first_move] @ui = UI.new end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
9 10 11 |
# File 'lib/ruby_ttt.rb', line 9 def board @board end |
#player_first_move ⇒ Object
Returns the value of attribute player_first_move.
9 10 11 |
# File 'lib/ruby_ttt.rb', line 9 def player_first_move @player_first_move end |
#player_one ⇒ Object
Returns the value of attribute player_one.
9 10 11 |
# File 'lib/ruby_ttt.rb', line 9 def player_one @player_one end |
#player_two ⇒ Object
Returns the value of attribute player_two.
9 10 11 |
# File 'lib/ruby_ttt.rb', line 9 def player_two @player_two end |
#ui ⇒ Object
Returns the value of attribute ui.
9 10 11 |
# File 'lib/ruby_ttt.rb', line 9 def ui @ui end |
Instance Method Details
#advance_game ⇒ Object
18 19 20 21 |
# File 'lib/ruby_ttt.rb', line 18 def advance_game game_status_check(current_player.opponent.marker) ui.(current_player.marker) unless board.game_over? end |
#current_player ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/ruby_ttt.rb', line 37 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
23 24 25 26 27 28 29 |
# File 'lib/ruby_ttt.rb', line 23 def game_status_check(marker) if board.winner?(marker) ui.(marker) elsif !board.moves_remaining? ui. end end |
#total_markers(marker) ⇒ Object
47 48 49 |
# File 'lib/ruby_ttt.rb', line 47 def total_markers(marker) board.all_cells.select { |cell, value| value == marker }.count end |
#verify_move(cell) ⇒ Object
31 32 33 34 35 |
# File 'lib/ruby_ttt.rb', line 31 def verify_move(cell) return false if !board.available_cell?(cell) current_player.add_marker(board, cell) true end |