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)
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(length) ⇒ Object
Method to generate a code.
-
.solution(length) ⇒ Object
Returns a code object containing ‘BBBB’.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #===(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:
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Returns if a code is valid (matches the code pattern (R, Y, G, O, M, P)).
Constructor Details
#initialize(code) ⇒ Code
Returns a new instance of Code.
5 6 7 |
# File 'lib/mastermind_ruby/code.rb', line 5 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
12 13 14 |
# File 'lib/mastermind_ruby/code.rb', line 12 def self.parse(str) new str.upcase.split('') end |
.random(length) ⇒ Object
Method to generate a code
17 18 19 |
# File 'lib/mastermind_ruby/code.rb', line 17 def self.random(length) new length.times.map { AVAILABLE_CHARACTERS.sample } end |
.solution(length) ⇒ Object
Returns a code object containing ‘BBBB’
22 23 24 |
# File 'lib/mastermind_ruby/code.rb', line 22 def self.solution(length) new length.times.map{ 'B' } end |
Instance Method Details
#==(other) ⇒ Object
98 99 100 |
# File 'lib/mastermind_ruby/code.rb', line 98 def ==(other) to_s == other.to_s end |
#===(other) ⇒ Object
86 87 88 |
# File 'lib/mastermind_ruby/code.rb', line 86 def ===(other) other === to_s end |
#[](index) ⇒ Object
90 91 92 |
# File 'lib/mastermind_ruby/code.rb', line 90 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
35 36 37 38 39 40 41 |
# File 'lib/mastermind_ruby/code.rb', line 35 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
102 103 104 |
# File 'lib/mastermind_ruby/code.rb', line 102 def eql?(other) to_s.eql?(other.to_s) end |
#hash ⇒ Object
106 107 108 |
# File 'lib/mastermind_ruby/code.rb', line 106 def hash to_s.hash end |
#map(&block) ⇒ Object
94 95 96 |
# File 'lib/mastermind_ruby/code.rb', line 94 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
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mastermind_ruby/code.rb', line 61 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
47 48 49 50 51 52 53 54 55 |
# File 'lib/mastermind_ruby/code.rb', line 47 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 ‘-’
76 77 78 79 80 81 82 83 84 |
# File 'lib/mastermind_ruby/code.rb', line 76 def mark_unmatched(input) input.map do |char| if %w(B W).include? char char else 'X' end end end |
#to_s ⇒ Object
110 111 112 |
# File 'lib/mastermind_ruby/code.rb', line 110 def to_s @code.join end |
#valid? ⇒ Boolean
Returns if a code is valid (matches the code pattern (R, Y, G, O, M, P))
27 28 29 |
# File 'lib/mastermind_ruby/code.rb', line 27 def valid? self === /\A[#{AVAILABLE_CHARACTERS.join}]+\z/ end |