Sha256: b5b42569c56437b33c6e8cb330e74b410a10b3d3b932661f235987f4ccaaebfe

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 Bytes

Contents

module Mastermind
  class Player
    class Human < Player
      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

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

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

        choice
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mastermind-game-0.0.2 lib/mastermind/player/human.rb