class MastermindRuby::Code
Constants
- AVAILABLE_CHARACTERS
Public Class Methods
new(code)
click to toggle source
# File lib/mastermind_ruby/code.rb, line 5 def initialize(code) @code = code end
parse(str)
click to toggle source
Parse a string to a code object Params:
str
-
the string which is to parse to a code object
# File lib/mastermind_ruby/code.rb, line 12 def self.parse(str) new str.upcase.split('') end
random(length)
click to toggle source
Method to generate a code
# File lib/mastermind_ruby/code.rb, line 17 def self.random(length) new length.times.map { AVAILABLE_CHARACTERS.sample } end
solution(length)
click to toggle source
Returns a code object containing 'BBBB'
# File lib/mastermind_ruby/code.rb, line 22 def self.solution(length) new length.times.map{ 'B' } end
Public Instance Methods
==(other)
click to toggle source
# File lib/mastermind_ruby/code.rb, line 102 def ==(other) to_s == other.to_s end
===(other)
click to toggle source
# File lib/mastermind_ruby/code.rb, line 90 def ===(other) other === to_s end
[](index)
click to toggle source
# File lib/mastermind_ruby/code.rb, line 94 def [](index) @code[index] end
assessment_for_solution(solution)
click to toggle source
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
# 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
count(obj)
click to toggle source
# File lib/mastermind_ruby/code.rb, line 86 def count(obj) @code.count(obj) end
eql?(other)
click to toggle source
# File lib/mastermind_ruby/code.rb, line 106 def eql?(other) to_s.eql?(other.to_s) end
hash()
click to toggle source
# File lib/mastermind_ruby/code.rb, line 110 def hash to_s.hash end
map(&block)
click to toggle source
# File lib/mastermind_ruby/code.rb, line 98 def map(&block) @code.map(&block) end
mark_right_color(input, solution)
click to toggle source
Search for character contained in solution Params:
input
-
the input
solution
-
the solution to compare with
# 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)
click to toggle source
Search for same character at same index and mark it with 'B' Params:
input
-
the input
solution
-
the solution to compare with
# 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)
click to toggle source
Mark unmatched with an '-' Params:
input
-
the input which should be filled with '-'
# 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()
click to toggle source
# File lib/mastermind_ruby/code.rb, line 114 def to_s @code.join end
valid?()
click to toggle source
Returns if a code is valid (matches the code pattern (R, Y, G, O, M, P))
# File lib/mastermind_ruby/code.rb, line 27 def valid? self === /\A[#{AVAILABLE_CHARACTERS.join}]+\z/ end