Class: Mastermind::Player::Human

Inherits:
Mastermind::Player show all
Defined in:
lib/mastermind/player/human.rb

Instance Attribute Summary

Attributes inherited from Mastermind::Player

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mastermind::Player

#initialize

Constructor Details

This class inherits a constructor from Mastermind::Player

Class Method Details

.get_choice(choices:) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/mastermind/player/human.rb', line 22

def self.get_choice(choices:)
  begin
    choice = STDIN.getch
    exit if choice.start_with?('q') && !puts
  end until choices.include? choice

  choice
end

.get_inputObject



31
32
33
34
35
# File 'lib/mastermind/player/human.rb', line 31

def self.get_input
  input = gets.chomp
  exit if input.downcase.start_with?('q')
  input
end

Instance Method Details

#get_code(length: 4, attempt: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mastermind/player/human.rb', line 4

def get_code(length: 4, attempt: nil)
  sequence = []

  length.times do
    turn = Game::Turn.new(guess: Game::Code.from(sequence), number: attempt)
    print "\r" + Console::View.attempt_line(turn, width: length) if attempt
    color = Game::Piece::COLORS[Human.get_choice(choices: ("1".."6")).to_i - 1]
    sequence << color
  end

  Game::Code.from(sequence)
end

#get_guess_for(game) ⇒ Object



17
18
19
20
# File 'lib/mastermind/player/human.rb', line 17

def get_guess_for(game)
  length = game.secret_length
  get_code(length: length, attempt: game.attempts + 1)
end