Class: Shared::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/games/shared/game.rb

Direct Known Subclasses

MM::Game, TTT::Game

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#boardObject

Returns the value of attribute board.



7
8
9
# File 'lib/games/shared/game.rb', line 7

def board
  @board
end

#board_builderObject

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_presenterObject

Returns the value of attribute board_presenter.



7
8
9
# File 'lib/games/shared/game.rb', line 7

def board_presenter
  @board_presenter
end

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/games/shared/game.rb', line 9

def config
  @config
end

#game_moduleObject (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_resetterObject

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_changerObject

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_helperObject (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

#ioObject

Returns the value of attribute io.



13
14
15
# File 'lib/games/shared/game.rb', line 13

def io
  @io
end

#move_generatorObject

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_takenObject

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

#playersObject

Returns the value of attribute players.



8
9
10
# File 'lib/games/shared/game.rb', line 8

def players
  @players
end

#players_factoryObject

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_playerObject



80
81
82
# File 'lib/games/shared/game.rb', line 80

def current_player
  players[current_turn_player_index]
end

#current_player_computer?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


88
89
90
# File 'lib/games/shared/game.rb', line 88

def current_player_human?
  current_player.type == :human
end

#current_player_nameObject



84
85
86
# File 'lib/games/shared/game.rb', line 84

def current_player_name
  current_player.name
end

#display_valuesObject



116
117
118
# File 'lib/games/shared/game.rb', line 116

def display_values
  board.display_values
end

#every_time_setupObject



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_setupObject

to be implemented by subclasses



45
46
# File 'lib/games/shared/game.rb', line 45

def local_setup
end

#move_forward_one_turnObject



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_setupObject



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

#playObject



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
      custom_final_message(self)
    end

    game_resetter.reset_game(self)
    new_game_starting_graphic
  end
end

#winnerObject



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