Class: MasterMind::Tobi::GameLogic

Inherits:
Object
  • Object
show all
Defined in:
lib/mastermind/tobi/logic.rb

Constant Summary collapse

SEQUENCE_TYPES =
['a beginner', 'an intermediate', 'an advanced']
COLORS =
[['r', 'g', 'b', 'y'], ['r', 'g', 'b', 'y', 'o'], ['r', 'g', 'b', 'y', 'o', 'v']]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level) ⇒ GameLogic

Returns a new instance of GameLogic.



10
11
12
13
14
15
# File 'lib/mastermind/tobi/logic.rb', line 10

def initialize(level)
  @level = level
  @color_array = COLORS[level]
  @length = color_array.length
  @sequence_type = SEQUENCE_TYPES[level]
end

Instance Attribute Details

#color_arrayObject (readonly)

Returns the value of attribute color_array.



4
5
6
# File 'lib/mastermind/tobi/logic.rb', line 4

def color_array
  @color_array
end

#lengthObject (readonly)

Returns the value of attribute length.



4
5
6
# File 'lib/mastermind/tobi/logic.rb', line 4

def length
  @length
end

#levelObject (readonly)

Returns the value of attribute level.



4
5
6
# File 'lib/mastermind/tobi/logic.rb', line 4

def level
  @level
end

#sequence_typeObject (readonly)

Returns the value of attribute sequence_type.



4
5
6
# File 'lib/mastermind/tobi/logic.rb', line 4

def sequence_type
  @sequence_type
end

Class Method Details

.check_input(sequence, input) ⇒ Object



21
22
23
# File 'lib/mastermind/tobi/logic.rb', line 21

def self.check_input(sequence, input)
  verify_values(input.split(''), sequence, sequence[0..-1])
end

.verify_values(split_input, sequence, sequence_copy) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mastermind/tobi/logic.rb', line 25

def self.verify_values(split_input, sequence, sequence_copy)
  result_values = {correct_elements: 0, correct_position: 0}
  split_input.each_with_index{ |element, index| 
    result_values[:correct_position] += 1 if split_input[index] == sequence[index] # if element in correct position
    if sequence_copy.include?(element)
      result_values[:correct_elements] += 1
      sequence_copy.delete_at(sequence_copy.index(element))         # if in sequence, delete first occurrence so as to counter duplicates
    end 
  }
  result_values
end

Instance Method Details

#generate_sequenceObject



17
18
19
# File 'lib/mastermind/tobi/logic.rb', line 17

def generate_sequence
  (color_array * (rand() * 1000).to_i).shuffle[0...color_array.length]                  # generate random combination of r/g/b/y
end