class Codebreaker::Game
Constants
- DIFFICULTIES
Attributes
attempts[R]
attempts_total[R]
hints_available[R]
hints_given[R]
hints_used[R]
level_name[R]
result[R]
secret[R]
Public Class Methods
new(level)
click to toggle source
# File lib/codebraker_ov/entities/game.rb, line 20 def initialize(level) @secret = Array.new(4) { rand(1..6).to_s } @level_name = level.to_s @attempts_total = DIFFICULTIES[level][:attempts] @attempts = DIFFICULTIES[level][:attempts] @hints_available = @secret.sample(DIFFICULTIES[level][:hints]) @hints_given = [] end
Public Instance Methods
compare_with(guess)
click to toggle source
# File lib/codebraker_ov/entities/game.rb, line 52 def compare_with(guess) @guess = guess.chars @result = '' rest = check_full_matches check_partial_matches(rest) @attempts -= 1 @result end
lose?()
click to toggle source
# File lib/codebraker_ov/entities/game.rb, line 74 def lose? !@attempts.positive? end
receive_hint()
click to toggle source
# File lib/codebraker_ov/entities/game.rb, line 62 def receive_hint return unless @hints_available.length.positive? @hints_given << @hints_available.pop @hints_given.last end
win?(guess)
click to toggle source
# File lib/codebraker_ov/entities/game.rb, line 70 def win?(guess) @secret == guess.chars end
Private Instance Methods
check_full_matches()
click to toggle source
# File lib/codebraker_ov/entities/game.rb, line 31 def check_full_matches @secret.map.with_index do |number, index| next number unless number == @guess[index] @result += '+' @guess[index] = nil end end
check_partial_matches(rest)
click to toggle source
# File lib/codebraker_ov/entities/game.rb, line 40 def check_partial_matches(rest) @guess.compact.each do |number| index = rest.index(number) next unless index @result += '-' rest[index] = nil end end