Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game.rb
Instance Attribute Summary collapse
-
#ai ⇒ Object
readonly
Returns the value of attribute ai.
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#player_1 ⇒ Object
Returns the value of attribute player_1.
-
#player_2 ⇒ Object
Returns the value of attribute player_2.
-
#runner ⇒ Object
readonly
Returns the value of attribute runner.
-
#ui ⇒ Object
readonly
Returns the value of attribute ui.
-
#unbeatable_ai ⇒ Object
readonly
Returns the value of attribute unbeatable_ai.
Instance Method Summary collapse
- #declare_turn(marker) ⇒ Object
- #find_winner ⇒ Object
- #game_over(final_game_message) ⇒ Object
-
#initialize(board, ui, ai, player_1, player_2) ⇒ Game
constructor
A new instance of Game.
- #place_marker(square, marker) ⇒ Object
- #play_game ⇒ Object
- #restart?(input) ⇒ Boolean
- #select_square ⇒ Object
Constructor Details
#initialize(board, ui, ai, player_1, player_2) ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 13 14 15 16 |
# File 'lib/game.rb', line 8 def initialize(board, ui, ai, player_1, player_2) @player_1 = player_1 @player_2 = player_2 @board = board @ui = ui @ai = ai @unbeatable_ai = ai @runner = Runner.new(ui) end |
Instance Attribute Details
#ai ⇒ Object (readonly)
Returns the value of attribute ai.
6 7 8 |
# File 'lib/game.rb', line 6 def ai @ai end |
#board ⇒ Object (readonly)
Returns the value of attribute board.
6 7 8 |
# File 'lib/game.rb', line 6 def board @board end |
#player_1 ⇒ Object
Returns the value of attribute player_1.
5 6 7 |
# File 'lib/game.rb', line 5 def player_1 @player_1 end |
#player_2 ⇒ Object
Returns the value of attribute player_2.
5 6 7 |
# File 'lib/game.rb', line 5 def player_2 @player_2 end |
#runner ⇒ Object (readonly)
Returns the value of attribute runner.
6 7 8 |
# File 'lib/game.rb', line 6 def runner @runner end |
#ui ⇒ Object (readonly)
Returns the value of attribute ui.
6 7 8 |
# File 'lib/game.rb', line 6 def ui @ui end |
#unbeatable_ai ⇒ Object (readonly)
Returns the value of attribute unbeatable_ai.
6 7 8 |
# File 'lib/game.rb', line 6 def unbeatable_ai @unbeatable_ai end |
Instance Method Details
#declare_turn(marker) ⇒ Object
18 19 20 21 22 |
# File 'lib/game.rb', line 18 def declare_turn(marker) @ui.puts_turn(marker) @ui.print_board(@board.display_board) @ui.ask_for_square_to_mark end |
#find_winner ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/game.rb', line 49 def find_winner winner = @board.winner_on_board? open_board = @board.board_open? if winner == false and open_board select_square elsif winner != false game_over(@ui.puts_winner(winner)) else game_over(@ui.puts_tie) end end |
#game_over(final_game_message) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/game.rb', line 63 def game_over() @ui.print_board(@board.display_board) @ui.ask_to_restart choice = @ui.get_input restart?(choice) end |
#place_marker(square, marker) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/game.rb', line 38 def place_marker(square, marker) if @board.square_empty?(square) == false @ui.marker_error select_square end @board.set_square(square, marker) find_winner end |
#play_game ⇒ Object
75 76 77 |
# File 'lib/game.rb', line 75 def play_game select_square end |
#restart?(input) ⇒ Boolean
71 72 73 |
# File 'lib/game.rb', line 71 def restart?(input) input == 1 ? @runner.call : exit end |
#select_square ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/game.rb', line 24 def select_square square = nil marker = @board.whose_turn declare_turn(marker) if marker == player_2.marker and @ai.opponent == true square = @unbeatable_ai.make_move(@board, marker) else square = @ui.get_square_to_mark end place_marker(square, marker) end |