Class: MastermindGenerator::Guess
- Inherits:
-
Object
- Object
- MastermindGenerator::Guess
- Extended by:
- Forwardable
- Defined in:
- lib/mastermind_generator/guess.rb
Overview
Responsible for guessing
Instance Attribute Summary collapse
-
#sequence ⇒ Object
readonly
Returns the value of attribute sequence.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #assign_target(target) ⇒ Object
- #correct_element_count ⇒ Object
- #correct_position_count ⇒ Object
- #correct_position_hints(mark = "_") ⇒ Object
-
#initialize(sequence) ⇒ Guess
constructor
A new instance of Guess.
- #statistics ⇒ Object
- #succeed? ⇒ Boolean
Constructor Details
#initialize(sequence) ⇒ Guess
Returns a new instance of Guess.
10 11 12 13 |
# File 'lib/mastermind_generator/guess.rb', line 10 def initialize(sequence) @sequence = sequence @target = nil end |
Instance Attribute Details
#sequence ⇒ Object (readonly)
Returns the value of attribute sequence.
6 7 8 |
# File 'lib/mastermind_generator/guess.rb', line 6 def sequence @sequence end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
6 7 8 |
# File 'lib/mastermind_generator/guess.rb', line 6 def target @target end |
Instance Method Details
#assign_target(target) ⇒ Object
15 16 17 |
# File 'lib/mastermind_generator/guess.rb', line 15 def assign_target(target) @target = target end |
#correct_element_count ⇒ Object
23 24 25 |
# File 'lib/mastermind_generator/guess.rb', line 23 def correct_element_count sequence_codes.reduce(0) { |sum, code| sum + [target_value.count(code), value.count(code)].min } end |
#correct_position_count ⇒ Object
27 28 29 |
# File 'lib/mastermind_generator/guess.rb', line 27 def correct_position_count seuence_target_packs.count { _1 == _2 } end |
#correct_position_hints(mark = "_") ⇒ Object
31 32 33 |
# File 'lib/mastermind_generator/guess.rb', line 31 def correct_position_hints(mark = "_") seuence_target_packs.map { _1 == _2 ? _1 : mark }.join end |
#statistics ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mastermind_generator/guess.rb', line 35 def statistics { value: value, target: target_value, status: status, element_count: correct_element_count, position_count: correct_position_count, position_hints: correct_position_hints } end |
#succeed? ⇒ Boolean
19 20 21 |
# File 'lib/mastermind_generator/guess.rb', line 19 def succeed? target == sequence end |