Class: Rochambeau::Cli

Inherits:
Thor
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rochambeau/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_players(count) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rochambeau/cli.rb', line 60

def self.get_players(count)
  case count
  when 1
    return [Player::UnnamedHuman.new, Player::Robot.new]
  when 2
    return [
      Player::NamedHuman.new('Player 1'),
      Player::NamedHuman.new('Player 2'),
    ]
  end

  raise ArgumentError, "Unexpected player count: #{count}"
end

Instance Method Details

#playObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rochambeau/cli.rb', line 30

def play
  system 'clear'

  game_mode =
    options.advanced ? GameMode::ADVANCED : GameMode::BASIC
  p1, p2 = Cli.get_players(options.players)

  puts game_mode.options.map(&:label).join(' ยท ')
  p1_choice = p1.choose_option(game_mode.options)
  p2_choice = p2.choose_option(game_mode.options)

  puts <<~CHOICES
    ------
    #{p1.name}: #{p1_choice}
    #{p2.name}: #{p2_choice}
    ------
  CHOICES

  puts Option.explain(p1_choice, p2_choice) unless p1_choice == p2_choice

  case p1_choice <=> p2_choice
  when 1 then puts p1.victory_message
  when -1 then puts p2.victory_message
  else puts 'Match draw.'
  end
end

#versionObject



10
11
12
13
14
15
# File 'lib/rochambeau/cli.rb', line 10

def version
  puts <<~VERSION
    Rochambeau #{Rochambeau::VERSION}
    GitHub: https://github.com/jigarius/rochambeau
  VERSION
end