Class: MasterMind::Tobi::GameLogic
- Inherits:
-
Object
- Object
- MasterMind::Tobi::GameLogic
- 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
-
#color_array ⇒ Object
readonly
Returns the value of attribute color_array.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#sequence_type ⇒ Object
readonly
Returns the value of attribute sequence_type.
Class Method Summary collapse
- .check_input(sequence, input) ⇒ Object
- .verify_values(split_input, sequence, sequence_copy) ⇒ Object
Instance Method Summary collapse
- #generate_sequence ⇒ Object
-
#initialize(level) ⇒ GameLogic
constructor
A new instance of GameLogic.
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_array ⇒ Object (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 |
#length ⇒ Object (readonly)
Returns the value of attribute length.
4 5 6 |
# File 'lib/mastermind/tobi/logic.rb', line 4 def length @length end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
4 5 6 |
# File 'lib/mastermind/tobi/logic.rb', line 4 def level @level end |
#sequence_type ⇒ Object (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_sequence ⇒ Object
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 |