Class: TTT::Game
- Inherits:
-
Object
- Object
- TTT::Game
- Defined in:
- lib/tictactoe/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#board_presenter ⇒ Object
Returns the value of attribute board_presenter.
-
#config ⇒ Object
Returns the value of attribute config.
-
#input_helper ⇒ Object
readonly
Returns the value of attribute input_helper.
-
#io ⇒ Object
Returns the value of attribute io.
-
#number_of_turns_taken ⇒ Object
Returns the value of attribute number_of_turns_taken.
-
#players ⇒ Object
Returns the value of attribute players.
Instance Method Summary collapse
- #available_choices ⇒ Object
- #change_square(display_value, new_value) ⇒ Object
- #current_player ⇒ Object
- #draw? ⇒ Boolean
- #generate_empty_board ⇒ Object
- #generate_players ⇒ Object
- #get_square_choice ⇒ Object
-
#initialize(args = {}) ⇒ Game
constructor
A new instance of Game.
- #move_forward_one_turn ⇒ Object
- #over? ⇒ Boolean
- #perfect_move ⇒ Object
- #play ⇒ Object
- #print_board ⇒ Object
- #random_move ⇒ Object
- #reset_board ⇒ Object
- #reset_game ⇒ Object
- #winner ⇒ Object
- #won? ⇒ Boolean
Constructor Details
#initialize(args = {}) ⇒ Game
Returns a new instance of Game.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tictactoe/game.rb', line 11 def initialize(args = {}) @io = args.fetch(:io, IOTerminal.new) @board_presenter = args.fetch(:board_presenter, BoardPresenterTerminal.new) @input_helper = InputHelper.new(@io) @number_of_turns_taken = 0 # below finds the user needed input needed for initial players and board configuration and stores in config object @config = GameConfig.new(@input_helper) @players = generate_players @board = generate_empty_board end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
8 9 10 |
# File 'lib/tictactoe/game.rb', line 8 def board @board end |
#board_presenter ⇒ Object
Returns the value of attribute board_presenter.
8 9 10 |
# File 'lib/tictactoe/game.rb', line 8 def board_presenter @board_presenter end |
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/tictactoe/game.rb', line 8 def config @config end |
#input_helper ⇒ Object (readonly)
Returns the value of attribute input_helper.
9 10 11 |
# File 'lib/tictactoe/game.rb', line 9 def input_helper @input_helper end |
#io ⇒ Object
Returns the value of attribute io.
9 10 11 |
# File 'lib/tictactoe/game.rb', line 9 def io @io end |
#number_of_turns_taken ⇒ Object
Returns the value of attribute number_of_turns_taken.
8 9 10 |
# File 'lib/tictactoe/game.rb', line 8 def number_of_turns_taken @number_of_turns_taken end |
#players ⇒ Object
Returns the value of attribute players.
8 9 10 |
# File 'lib/tictactoe/game.rb', line 8 def players @players end |
Instance Method Details
#available_choices ⇒ Object
126 127 128 |
# File 'lib/tictactoe/game.rb', line 126 def available_choices board.available_choices end |
#change_square(display_value, new_value) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/tictactoe/game.rb', line 89 def change_square(display_value, new_value) board.change_square(display_value, new_value) if !won? move_forward_one_turn end end |
#current_player ⇒ Object
134 135 136 |
# File 'lib/tictactoe/game.rb', line 134 def current_player players[current_turn_player_index] end |
#draw? ⇒ Boolean
100 101 102 |
# File 'lib/tictactoe/game.rb', line 100 def draw? board.full? && !board.won? end |
#generate_empty_board ⇒ Object
34 35 36 37 |
# File 'lib/tictactoe/game.rb', line 34 def generate_empty_board number_of_rows_cols = config.number_of_rows_cols Board.new(rows_and_cols: number_of_rows_cols, squares: SquaresFactory.build_empty_squares(number_of_rows_cols) ) end |
#generate_players ⇒ Object
28 29 30 31 32 |
# File 'lib/tictactoe/game.rb', line 28 def generate_players player_1 = Player.new(value: config.player_1_value, name: config.player_1_name, type: :human) player_2 = Player.new(value: config.player_2_value, name: config.player_2_name, type: config.player_2_type, difficulty_level: config.computer_difficulty_level) [player_1, player_2] end |
#get_square_choice ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tictactoe/game.rb', line 76 def get_square_choice if current_player.type == :human input_helper.get_square_to_change(current_player.name, available_choices) elsif current_player.type == :computer input_helper.computer_choosing_graphic if current_player.difficulty_level == :difficult perfect_move elsif current_player.difficulty_level == :easy random_move end end end |
#move_forward_one_turn ⇒ Object
138 139 140 |
# File 'lib/tictactoe/game.rb', line 138 def move_forward_one_turn self.number_of_turns_taken += 1 end |
#over? ⇒ Boolean
96 97 98 |
# File 'lib/tictactoe/game.rb', line 96 def over? board.full? || board.won? end |
#perfect_move ⇒ Object
116 117 118 119 120 |
# File 'lib/tictactoe/game.rb', line 116 def perfect_move move_generator = MoveGenerator.new(self) move_generator.minimax move_generator.choice end |
#play ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tictactoe/game.rb', line 48 def play while true while !over? print_board square_choice = get_square_choice #note that change_square moves current_player forward only if game is not won if square_choice == :exit break else change_square(square_choice, current_player.value) end end #need to print_final_iteration of board print_board if won? winning_prompt elsif draw? draw_prompt end reset_game new_game_starting_graphic end end |
#print_board ⇒ Object
130 131 132 |
# File 'lib/tictactoe/game.rb', line 130 def print_board board_presenter.present_board(board) end |
#random_move ⇒ Object
122 123 124 |
# File 'lib/tictactoe/game.rb', line 122 def random_move available_choices.sample end |
#reset_board ⇒ Object
39 40 41 |
# File 'lib/tictactoe/game.rb', line 39 def reset_board self.board = generate_empty_board end |
#reset_game ⇒ Object
43 44 45 46 |
# File 'lib/tictactoe/game.rb', line 43 def reset_game reset_board self.number_of_turns_taken = 0 end |
#winner ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/tictactoe/game.rb', line 108 def winner #each move is immediately proceeded by an increment to number_of_selections_made; therefore, need to rewind won to find winner if !won? return nil end current_player end |
#won? ⇒ Boolean
104 105 106 |
# File 'lib/tictactoe/game.rb', line 104 def won? board.won? end |