Class: Player

Inherits:
Object
  • Object
show all
Defined in:
lib/mastermind_cli/player.rb

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ Player

Returns a new instance of Player.



3
4
5
6
# File 'lib/mastermind_cli/player.rb', line 3

def initialize(board)
  @board = board
  @move = nil
end

Instance Method Details

#get_inputObject



16
17
18
19
20
21
22
23
24
# File 'lib/mastermind_cli/player.rb', line 16

def get_input
  puts "Enter move: "
  arr = gets.chomp.split('')
  if input_valid?(arr)
    @move = arr
  else
    get_input
  end
end

#input_valid?(arr) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/mastermind_cli/player.rb', line 27

def input_valid?(arr)
  if arr.length != 4
    puts "Please input four letters"
    return false
  else
    arr.all? {|chr| @board.class::COLORS.include?(chr)}
  end
end

#make_move(turn) ⇒ Object



9
10
11
12
13
# File 'lib/mastermind_cli/player.rb', line 9

def make_move(turn)
  row = turn
  get_input
  @board.add_move(@move, row)
end