module Codebreaker::Marker

Public Instance Methods

check_win() click to toggle source
# File lib/codebreaker/modules/marker.rb, line 3
def check_win
  return 'Congratulation! You win!' if win?
  if @attempts.zero?
    @hint = 0
    @game_start = false
    "Game over! Secret code is #{@secret_code}."
  else
    marking
  end
end
win?() click to toggle source
# File lib/codebreaker/modules/marker.rb, line 14
def win?
  @secret_code == @player_code
end

Private Instance Methods

convert_to_a(code) click to toggle source
# File lib/codebreaker/modules/marker.rb, line 36
def convert_to_a(code)
  code.to_s.chars.to_a
end
marking() click to toggle source
# File lib/codebreaker/modules/marker.rb, line 20
def marking
  @player_code = convert_to_a(@player_code)
  @attempts -= 1
  pluses + minuses
end
minuses() click to toggle source
# File lib/codebreaker/modules/marker.rb, line 30
def minuses
  secret = convert_to_a(@secret_code)
  array = @player_code.map { |num| secret[secret.find_index(num)] = '-' if secret.include?(num) }
  '-' * (array.count('-') - pluses.length)
end
pluses() click to toggle source
# File lib/codebreaker/modules/marker.rb, line 26
def pluses
  @player_code.zip(@secret_code.chars).map {|array| '+' if array[0] == array[1]}.compact.join
end