Class: Lekanmastermind::Computer

Inherits:
Object
  • Object
show all
Defined in:
lib/lekanmastermind/computer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_of_colors:, num_of_char:) ⇒ Computer

Returns a new instance of Computer.



4
5
6
7
8
9
# File 'lib/lekanmastermind/computer.rb', line 4

def initialize(num_of_colors:, num_of_char:)
  @number_of_colors = num_of_colors
  @number_of_character = num_of_char
  colors = %w(Red Blue Green Yellow Orange Indigo Violet Cyan Purple)
  @level_colors = colors.shuffle[0...@number_of_colors]
end

Instance Attribute Details

#level_colorsObject (readonly)

Returns the value of attribute level_colors.



3
4
5
# File 'lib/lekanmastermind/computer.rb', line 3

def level_colors
  @level_colors
end

#number_of_characterObject (readonly)

Returns the value of attribute number_of_character.



3
4
5
# File 'lib/lekanmastermind/computer.rb', line 3

def number_of_character
  @number_of_character
end

#number_of_colorsObject (readonly)

Returns the value of attribute number_of_colors.



3
4
5
# File 'lib/lekanmastermind/computer.rb', line 3

def number_of_colors
  @number_of_colors
end

Instance Method Details

#computer_guessObject



11
12
13
14
15
16
17
# File 'lib/lekanmastermind/computer.rb', line 11

def computer_guess
  computer_colors = ''
  number_of_character.times do
    computer_colors << random_colour_char
  end
  computer_colors.downcase
end

#random_colour_charObject



19
20
21
22
# File 'lib/lekanmastermind/computer.rb', line 19

def random_colour_char
  colour_index = Random.new.rand(number_of_colors)
  @level_colors[colour_index][0]
end