class Codebreaker::Game
Constants
- DIFFICULTY_HASH
Attributes
attempts_used[RW]
difficulty[R]
hints_used[R]
secret_code[R]
user[R]
Public Class Methods
new(user, difficulty, secret_code)
click to toggle source
# File lib/codebreaker/game.rb, line 16 def initialize(user, difficulty, secret_code) @user = user @difficulty = difficulty @secret_code = secret_code @attempts_used = 0 @hints_used = [] validate! end
Public Instance Methods
any_hints_left?()
click to toggle source
# File lib/codebreaker/game.rb, line 36 def any_hints_left? DIFFICULTY_HASH[difficulty][:hints] > hints_used.count end
check(guess)
click to toggle source
# File lib/codebreaker/game.rb, line 25 def check(guess) self.attempts_used += 1 guess_array = guess.digits.reverse secret_code_array = secret_code.digits.reverse codechecker = Codechecker.new(guess_array, secret_code_array) codechecker.call codechecker.response end
lose?()
click to toggle source
# File lib/codebreaker/game.rb, line 54 def lose? attempts_used >= DIFFICULTY_HASH[difficulty][:attempts] end
take_hint()
click to toggle source
# File lib/codebreaker/game.rb, line 40 def take_hint secret_code_array = secret_code.digits.reverse hints_used.each do |hint_used| secret_code_array.delete_at(secret_code_array.index(hint_used)) end hint = secret_code_array.sample hints_used << hint hint end
win?(guess)
click to toggle source
# File lib/codebreaker/game.rb, line 50 def win?(guess) guess == secret_code end