Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game.rb
Instance Attribute Summary collapse
-
#ai ⇒ Object
Returns the value of attribute ai.
-
#board ⇒ Object
Returns the value of attribute board.
-
#difficulty_level ⇒ Object
Returns the value of attribute difficulty_level.
-
#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(cell, player) ⇒ Object
- #current_player ⇒ Object
- #exit_game ⇒ Object
- #game_status_check ⇒ Object
- #get_next_move ⇒ Object
-
#initialize(board, player_one, player_two, difficulty_level) ⇒ Game
constructor
A new instance of Game.
- #invalid_move(cell) ⇒ Object
- #play! ⇒ Object
Constructor Details
#initialize(board, player_one, player_two, difficulty_level) ⇒ Game
3 4 5 6 7 8 9 10 |
# File 'lib/game.rb', line 3 def initialize(board, player_one, player_two, difficulty_level) @board = board @ui = UI.new(@board) @player_one = player_one @player_two = player_two @ai = AI.new @difficulty_level = difficulty_level end |
Instance Attribute Details
#ai ⇒ Object
Returns the value of attribute ai.
2 3 4 |
# File 'lib/game.rb', line 2 def ai @ai end |
#board ⇒ Object
Returns the value of attribute board.
2 3 4 |
# File 'lib/game.rb', line 2 def board @board end |
#difficulty_level ⇒ Object
Returns the value of attribute difficulty_level.
2 3 4 |
# File 'lib/game.rb', line 2 def difficulty_level @difficulty_level end |
#player_one ⇒ Object
Returns the value of attribute player_one.
2 3 4 |
# File 'lib/game.rb', line 2 def player_one @player_one end |
#player_two ⇒ Object
Returns the value of attribute player_two.
2 3 4 |
# File 'lib/game.rb', line 2 def player_two @player_two end |
#ui ⇒ Object
Returns the value of attribute ui.
2 3 4 |
# File 'lib/game.rb', line 2 def ui @ui end |
Instance Method Details
#advance_game(cell, player) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/game.rb', line 26 def advance_game(cell, player) board.add_marker(player.marker, cell) game_status_check player.next_player_turn ui.(current_player) unless board.game_over? end |
#current_player ⇒ Object
45 46 47 |
# File 'lib/game.rb', line 45 def current_player player_one.current_player? ? player_one : player_two end |
#exit_game ⇒ Object
49 50 51 52 |
# File 'lib/game.rb', line 49 def exit_game ui.display_board ui.io.exit end |
#game_status_check ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/game.rb', line 33 def game_status_check if board.winner?(current_player.marker) ui.(current_player) elsif !board.moves_remaining? ui. end end |
#get_next_move ⇒ Object
21 22 23 24 |
# File 'lib/game.rb', line 21 def get_next_move return ui.request_human_move if current_player.player_type == HUMAN_PLAYER difficulty_level == HARD_LEVEL ? ai.computer_move(board, current_player) : board.random_cell end |
#invalid_move(cell) ⇒ Object
41 42 43 |
# File 'lib/game.rb', line 41 def invalid_move(cell) board.valid_cell?(cell) ? ui.(cell) : ui.(cell) end |
#play! ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/game.rb', line 12 def play! until board.game_over? ui.display_board move = get_next_move board.available_cell?(move) ? advance_game(move, current_player) : invalid_move(move) end exit_game end |