Class: TicTacToe::Game
- Inherits:
-
Object
- Object
- TicTacToe::Game
- Defined in:
- lib/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#interface ⇒ Object
readonly
Returns the value of attribute interface.
-
#player_marks ⇒ Object
readonly
Returns the value of attribute player_marks.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
Instance Method Summary collapse
- #get_valid_move(player) ⇒ Object
- #handle_game_over ⇒ Object
- #handle_one_turn(current_player) ⇒ Object
- #handle_turns ⇒ Object
-
#initialize(parameters) ⇒ Game
constructor
A new instance of Game.
- #over? ⇒ Boolean
- #run ⇒ Object
- #set_up ⇒ Object
Constructor Details
#initialize(parameters) ⇒ Game
Returns a new instance of Game.
9 10 11 12 13 14 |
# File 'lib/game.rb', line 9 def initialize(parameters) @player_marks = parameters[:player_marks] || [:x, :o] @board = parameters[:board] || create_default_board @interface = parameters[:interface] || create_default_interface @players = [] end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
7 8 9 |
# File 'lib/game.rb', line 7 def board @board end |
#interface ⇒ Object (readonly)
Returns the value of attribute interface.
7 8 9 |
# File 'lib/game.rb', line 7 def interface @interface end |
#player_marks ⇒ Object (readonly)
Returns the value of attribute player_marks.
7 8 9 |
# File 'lib/game.rb', line 7 def player_marks @player_marks end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
7 8 9 |
# File 'lib/game.rb', line 7 def players @players end |
Instance Method Details
#get_valid_move(player) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/game.rb', line 44 def get_valid_move(player) loop do coordinates = player.move if @board.out_of_bounds?(coordinates) || @board.marked?(coordinates) @interface.report_invalid_move(coordinates) else return coordinates end end end |
#handle_game_over ⇒ Object
55 56 57 58 59 |
# File 'lib/game.rb', line 55 def handle_game_over @interface.show_game_board(@board) winning_player_mark = @board.has_winning_line? ? @board.last_mark_made : :none @interface.report_game_over(winning_player_mark) end |
#handle_one_turn(current_player) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/game.rb', line 37 def handle_one_turn(current_player) @interface.show_game_board(@board) coordinates = get_valid_move(current_player) @board.mark_cell(current_player.player_mark, *coordinates) @interface.report_move(current_player.player_mark, coordinates) end |
#handle_turns ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/game.rb', line 28 def handle_turns catch(:game_over) do @players.cycle do |current_player| handle_one_turn(current_player) throw :game_over if over? end end end |
#over? ⇒ Boolean
61 62 63 |
# File 'lib/game.rb', line 61 def over? @board.has_winning_line? || @board.all_marked? end |
#run ⇒ Object
16 17 18 19 20 |
# File 'lib/game.rb', line 16 def run set_up handle_turns handle_game_over end |
#set_up ⇒ Object
22 23 24 25 26 |
# File 'lib/game.rb', line 22 def set_up player_types = @interface.game_setup_interaction(@player_marks) @players = @player_marks.zip(player_types).map { |mark, type| create_player(mark, type) } end |