class Codebreaker::Game

Constants

DIFFICULTIES

Attributes

attempts_left[R]
difficulty_type[R]
player[R]

Public Class Methods

new(player, difficulty_type) click to toggle source
# File lib/codebreaker/game.rb, line 11
def initialize(player, difficulty_type)
  @player = player
  @difficulty_type = difficulty_type
  @attempts_left = difficulty[:attempts]
  @is_win = false
end

Public Instance Methods

attempts_total() click to toggle source
# File lib/codebreaker/game.rb, line 49
def attempts_total
  difficulty[:attempts]
end
guess_secret_code(guess) click to toggle source
# File lib/codebreaker/game.rb, line 29
def guess_secret_code(guess)
  ensure_game_is_not_over
  @attempts_left -= 1
  matching = SecretCodeMatcher.new(secret_code, guess).call
  check_win(guess)
  matching
end
hints_left() click to toggle source
# File lib/codebreaker/game.rb, line 57
def hints_left
  hints.count
end
hints_total() click to toggle source
# File lib/codebreaker/game.rb, line 53
def hints_total
  difficulty[:hints]
end
lose?() click to toggle source
# File lib/codebreaker/game.rb, line 37
def lose?
  attempts_left.zero? && !win?
end
over?() click to toggle source
# File lib/codebreaker/game.rb, line 45
def over?
  win? || lose?
end
save_result() click to toggle source
# File lib/codebreaker/game.rb, line 61
def save_result
  raise Errors::GameSaveError unless win?

  CreateStatService.new(self).call
end
secret_code() click to toggle source
# File lib/codebreaker/game.rb, line 18
def secret_code
  @secret_code ||= SecretCodeGenerator.new.call
end
take_hint() click to toggle source
# File lib/codebreaker/game.rb, line 22
def take_hint
  ensure_game_is_not_over
  raise Errors::NoHintsError if hints.empty?

  hints.pop
end
win?() click to toggle source
# File lib/codebreaker/game.rb, line 41
def win?
  @is_win
end

Private Instance Methods

check_win(guess) click to toggle source
# File lib/codebreaker/game.rb, line 81
def check_win(guess)
  @is_win = true if guess == secret_code
end
difficulty() click to toggle source
# File lib/codebreaker/game.rb, line 69
def difficulty
  DIFFICULTIES[@difficulty_type]
end
ensure_game_is_not_over() click to toggle source
# File lib/codebreaker/game.rb, line 77
def ensure_game_is_not_over
  raise Errors::GameOverError if over?
end
hints() click to toggle source
# File lib/codebreaker/game.rb, line 73
def hints
  @hints ||= secret_code.sample(difficulty[:hints])
end