Class: CLI::PlayerSelection

Inherits:
Selection show all
Defined in:
lib/ttt/interfaces/cli/lib/cli/player_selection.rb

Instance Attribute Summary collapse

Attributes inherited from Selection

#boards

Instance Method Summary collapse

Methods inherited from Selection

#input

Constructor Details

#initialize(players, presenter) ⇒ PlayerSelection

Returns a new instance of PlayerSelection.



6
7
8
9
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 6

def initialize(players, presenter)
  self.presenter = presenter
  self.players   = players
end

Instance Attribute Details

#player1Object

Returns the value of attribute player1.



5
6
7
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 5

def player1
  @player1
end

#player2Object

Returns the value of attribute player2.



5
6
7
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 5

def player2
  @player2
end

#playersObject

Returns the value of attribute players.



5
6
7
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 5

def players
  @players
end

#presenterObject

Returns the value of attribute presenter.



5
6
7
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 5

def presenter
  @presenter
end

Instance Method Details

#_player(selection) ⇒ Object



35
36
37
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 35

def _player(selection)
  players[selection - 1]
end

#player_selection_valid?(selection) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 39

def player_selection_valid?(selection)
  0 < selection.to_i && selection <= players.length
end

#processObject



11
12
13
14
15
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 11

def process
  process_player_type_input(1, presenter.player_type_prompt(1, players))
  process_player_type_input(2, presenter.player_type_prompt(2, players))
  self
end

#process_player_type_input(num, selection) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 17

def process_player_type_input(num, selection)
  if player_selection_valid? selection.chomp.to_i
    set_player_string(num, selection.chomp.to_i)
  else
    presenter.error
    process_player_type_input(num, presenter.player_type_prompt(num, players))
  end
end

#set_player_string(num, selection) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/ttt/interfaces/cli/lib/cli/player_selection.rb', line 26

def set_player_string(num, selection)
  case num
  when 1
    self.player1 = _player(selection)
  when 2
    self.player2 = _player(selection)
  end
end