Class: MastermindRuby::Code
- Inherits:
-
Object
- Object
- MastermindRuby::Code
- Defined in:
- lib/mastermind_ruby/code.rb
Constant Summary collapse
- AVAILABLE_CHARACTERS =
%w(R Y G O M P)- CODE_PATTERN =
/\A[#{AVAILABLE_CHARACTERS.join("")}]{4}\z/
Class Method Summary collapse
-
.parse(str) ⇒ Object
- Parse a string to a code object Params:
str -
the string which is to parse to a code object.
- Parse a string to a code object Params:
-
.random ⇒ Object
Method to generate a code.
-
.solution ⇒ Object
Returns a code object containing ‘BBBB’.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](index) ⇒ Object
-
#assessment_for_solution(solution) ⇒ Object
Get assesment for solution e.g.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(code) ⇒ Code
constructor
A new instance of Code.
- #map(&block) ⇒ Object
-
#mark_right_color(input, solution) ⇒ Object
- Search for character contained in solution Params:
input - the input
solution -
the solution to compare with.
- the input
- Search for character contained in solution Params:
-
#mark_right_position(input, solution) ⇒ Object
- Search for same character at same index and mark it with ‘B’ Params:
input - the input
solution -
the solution to compare with.
- the input
- Search for same character at same index and mark it with ‘B’ Params:
-
#mark_unmatched(input) ⇒ Object
- Mark unmatched with an ‘-’ Params:
input -
the input which should be filled with ‘-’.
- Mark unmatched with an ‘-’ Params:
-
#solution? ⇒ Boolean
Returns true if code is ‘BBBB’.
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Checks if a code is valid (must be 4 characters long and can only contain the available characters).
Constructor Details
#initialize(code) ⇒ Code
6 7 8 |
# File 'lib/mastermind_ruby/code.rb', line 6 def initialize(code) @code = code end |
Class Method Details
.parse(str) ⇒ Object
Parse a string to a code object Params:
str-
the string which is to parse to a code object
13 14 15 |
# File 'lib/mastermind_ruby/code.rb', line 13 def self.parse(str) new str.split('') end |
.random ⇒ Object
Method to generate a code
18 19 20 |
# File 'lib/mastermind_ruby/code.rb', line 18 def self.random new 4.times.map { AVAILABLE_CHARACTERS.sample } end |
.solution ⇒ Object
Returns a code object containing ‘BBBB’
23 24 25 |
# File 'lib/mastermind_ruby/code.rb', line 23 def self.solution @solution ||= Code.parse('BBBB') end |
Instance Method Details
#==(other) ⇒ Object
100 101 102 |
# File 'lib/mastermind_ruby/code.rb', line 100 def ==(other) to_s == other.to_s end |
#[](index) ⇒ Object
92 93 94 |
# File 'lib/mastermind_ruby/code.rb', line 92 def [](index) @code[index] end |
#assessment_for_solution(solution) ⇒ Object
Get assesment for solution e.g. self is YYRG and solution is MMGR it will return WW– (Code Object) Params:
solution-
the solution which an assessment should be made with
41 42 43 44 45 46 47 |
# File 'lib/mastermind_ruby/code.rb', line 41 def assessment_for_solution(solution) tmp = @code.dup tmp = mark_right_position(tmp, solution) tmp = mark_right_color(tmp, solution) tmp = mark_unmatched(tmp) Code.parse(tmp.sort.join('').tr('X', '-')) end |
#eql?(other) ⇒ Boolean
104 105 106 |
# File 'lib/mastermind_ruby/code.rb', line 104 def eql?(other) to_s.eql?(other.to_s) end |
#hash ⇒ Object
108 109 110 |
# File 'lib/mastermind_ruby/code.rb', line 108 def hash to_s.hash end |
#map(&block) ⇒ Object
96 97 98 |
# File 'lib/mastermind_ruby/code.rb', line 96 def map(&block) @code.map(&block) end |
#mark_right_color(input, solution) ⇒ Object
Search for character contained in solution Params:
input-
the input
solution-
the solution to compare with
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mastermind_ruby/code.rb', line 67 def mark_right_color(input, solution) rest_solution = solution.map.with_index { |char, index| input[index] == 'B' ? nil : char } input.each.with_index.map do |char, index| if index = rest_solution.index(char) rest_solution[index] = nil 'W' else char end end end |
#mark_right_position(input, solution) ⇒ Object
Search for same character at same index and mark it with ‘B’ Params:
input-
the input
solution-
the solution to compare with
53 54 55 56 57 58 59 60 61 |
# File 'lib/mastermind_ruby/code.rb', line 53 def mark_right_position(input, solution) input.each.with_index.map do |char, index| if solution[index] == char 'B' else char end end end |
#mark_unmatched(input) ⇒ Object
Mark unmatched with an ‘-’ Params:
input-
the input which should be filled with ‘-’
82 83 84 85 86 87 88 89 90 |
# File 'lib/mastermind_ruby/code.rb', line 82 def mark_unmatched(input) input.map do |char| if %w(B W).include? char char else 'X' end end end |
#solution? ⇒ Boolean
Returns true if code is ‘BBBB’
33 34 35 |
# File 'lib/mastermind_ruby/code.rb', line 33 def solution? self == Code.solution end |
#to_s ⇒ Object
112 113 114 |
# File 'lib/mastermind_ruby/code.rb', line 112 def to_s @code.join end |
#valid? ⇒ Boolean
Checks if a code is valid (must be 4 characters long and can only contain the available characters)
28 29 30 |
# File 'lib/mastermind_ruby/code.rb', line 28 def valid? @code.join =~ CODE_PATTERN end |