Class: Shared::Game
- Inherits:
-
Object
- Object
- Shared::Game
- Defined in:
- lib/games/shared/game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#board_builder ⇒ Object
Returns the value of attribute board_builder.
-
#board_presenter ⇒ Object
Returns the value of attribute board_presenter.
-
#config ⇒ Object
Returns the value of attribute config.
-
#game_module ⇒ Object
readonly
Returns the value of attribute game_module.
-
#game_resetter ⇒ Object
Returns the value of attribute game_resetter.
-
#game_state_changer ⇒ Object
Returns the value of attribute game_state_changer.
-
#input_helper ⇒ Object
readonly
Returns the value of attribute input_helper.
-
#io ⇒ Object
Returns the value of attribute io.
-
#move_generator ⇒ Object
Returns the value of attribute move_generator.
-
#number_of_turns_taken ⇒ Object
Returns the value of attribute number_of_turns_taken.
-
#players ⇒ Object
Returns the value of attribute players.
-
#players_factory ⇒ Object
Returns the value of attribute players_factory.
Instance Method Summary collapse
- #change_game_state(available_choice, game) ⇒ Object
- #current_player ⇒ Object
- #current_player_computer? ⇒ Boolean
- #current_player_human? ⇒ Boolean
- #current_player_name ⇒ Object
- #display_values ⇒ Object
- #every_time_setup ⇒ Object
- #generate_empty_board(config) ⇒ Object
-
#initialize(args = {}) ⇒ Game
constructor
A new instance of Game.
-
#local_setup ⇒ Object
to be implemented by subclasses.
- #move_forward_one_turn ⇒ Object
- #one_time_setup ⇒ Object
- #play ⇒ Object
- #winner ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Game
Returns a new instance of Game.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/games/shared/game.rb', line 15 def initialize(args = {}) @game_module = args.fetch(:game_module, nil) @io = args.fetch(:io, IOTerminal.new) @board_presenter = args.fetch(:board_presenter, nil) @input_helper = game_module::InputHelper.new(@io) @board_builder = game_module::BoardBuilder.new @game_state_changer = game_module::GameStateChanger.new @game_resetter = game_module::GameResetter.new @move_generator = game_module::MoveGenerator.new @players_factory = game_module::PlayersFactory.new @config = game_module::GameConfig.new(@input_helper) @number_of_turns_taken = 0 end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
7 8 9 |
# File 'lib/games/shared/game.rb', line 7 def board @board end |
#board_builder ⇒ Object
Returns the value of attribute board_builder.
7 8 9 |
# File 'lib/games/shared/game.rb', line 7 def board_builder @board_builder end |
#board_presenter ⇒ Object
Returns the value of attribute board_presenter.
7 8 9 |
# File 'lib/games/shared/game.rb', line 7 def board_presenter @board_presenter end |
#config ⇒ Object
Returns the value of attribute config.
9 10 11 |
# File 'lib/games/shared/game.rb', line 9 def config @config end |
#game_module ⇒ Object (readonly)
Returns the value of attribute game_module.
13 14 15 |
# File 'lib/games/shared/game.rb', line 13 def game_module @game_module end |
#game_resetter ⇒ Object
Returns the value of attribute game_resetter.
11 12 13 |
# File 'lib/games/shared/game.rb', line 11 def game_resetter @game_resetter end |
#game_state_changer ⇒ Object
Returns the value of attribute game_state_changer.
9 10 11 |
# File 'lib/games/shared/game.rb', line 9 def game_state_changer @game_state_changer end |
#input_helper ⇒ Object (readonly)
Returns the value of attribute input_helper.
13 14 15 |
# File 'lib/games/shared/game.rb', line 13 def input_helper @input_helper end |
#io ⇒ Object
Returns the value of attribute io.
13 14 15 |
# File 'lib/games/shared/game.rb', line 13 def io @io end |
#move_generator ⇒ Object
Returns the value of attribute move_generator.
12 13 14 |
# File 'lib/games/shared/game.rb', line 12 def move_generator @move_generator end |
#number_of_turns_taken ⇒ Object
Returns the value of attribute number_of_turns_taken.
10 11 12 |
# File 'lib/games/shared/game.rb', line 10 def number_of_turns_taken @number_of_turns_taken end |
#players ⇒ Object
Returns the value of attribute players.
8 9 10 |
# File 'lib/games/shared/game.rb', line 8 def players @players end |
#players_factory ⇒ Object
Returns the value of attribute players_factory.
8 9 10 |
# File 'lib/games/shared/game.rb', line 8 def players_factory @players_factory end |
Instance Method Details
#change_game_state(available_choice, game) ⇒ Object
104 105 106 |
# File 'lib/games/shared/game.rb', line 104 def change_game_state(available_choice, game) game_state_changer.change_game_state(available_choice, game) end |
#current_player ⇒ Object
80 81 82 |
# File 'lib/games/shared/game.rb', line 80 def current_player players[current_turn_player_index] end |
#current_player_computer? ⇒ Boolean
92 93 94 |
# File 'lib/games/shared/game.rb', line 92 def current_player_computer? current_player.type == :computer end |
#current_player_human? ⇒ Boolean
88 89 90 |
# File 'lib/games/shared/game.rb', line 88 def current_player_human? current_player.type == :human end |
#current_player_name ⇒ Object
84 85 86 |
# File 'lib/games/shared/game.rb', line 84 def current_player_name current_player.name end |
#display_values ⇒ Object
116 117 118 |
# File 'lib/games/shared/game.rb', line 116 def display_values board.display_values end |
#every_time_setup ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/games/shared/game.rb', line 36 def every_time_setup #setup gets necessary info from user and stores it in config object config.every_time_setup self.players = players_factory.generate_players(config) self.board = board_builder.generate_empty_board(config) local_setup end |
#generate_empty_board(config) ⇒ Object
100 101 102 |
# File 'lib/games/shared/game.rb', line 100 def generate_empty_board(config) board_builder.generate_empty_board(config) end |
#local_setup ⇒ Object
to be implemented by subclasses
45 46 |
# File 'lib/games/shared/game.rb', line 45 def local_setup end |
#move_forward_one_turn ⇒ Object
96 97 98 |
# File 'lib/games/shared/game.rb', line 96 def move_forward_one_turn self.number_of_turns_taken += 1 end |
#one_time_setup ⇒ Object
31 32 33 34 |
# File 'lib/games/shared/game.rb', line 31 def one_time_setup #setup gets necessary info from user and stores it in config object config.one_time_setup end |
#play ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/games/shared/game.rb', line 54 def play initial_instructions one_time_setup while true every_time_setup while !over? print_board move = move_generator.get_player_choice(self) game_state_changer.change_game_state(move, self) end #need to print_final_iteration of board print_board if won? winning_prompt elsif over_with_no_winner? no_winner_prompt (self) end game_resetter.reset_game(self) new_game_starting_graphic end end |
#winner ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/games/shared/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 |