class Mastermind::Player::Human

Public Class Methods

get_choice(choices:) click to toggle source
# 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_input() click to toggle source
# File lib/mastermind/player/human.rb, line 31
def self.get_input
  input = gets.chomp
  exit if input.downcase.start_with?('q')
  input
end

Public Instance Methods

get_code(length: 4, attempt: nil) click to toggle source
# 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) click to toggle source
# 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