Sha256: 3c2c70fc65ba4c9ee3b87fce8d8136a6a41b7afc36e35668dd0617a416491d4d
Contents?: true
Size: 1.58 KB
Versions: 2
Compression:
Stored size: 1.58 KB
Contents
module Codebreaker class Game attr_reader :player, :difficulty_type, :attempts_left DIFFICULTIES = { easy: { attempts: 15, hints: 2, hardness: 1 }, medium: { attempts: 10, hints: 1, hardness: 2 }, hell: { attempts: 5, hints: 1, hardness: 3 } }.freeze def initialize(player, difficulty_type) @player = player @difficulty_type = difficulty_type @attempts_left = difficulty[:attempts] @is_win = false end def secret_code @secret_code ||= SecretCodeGenerator.new.call end def take_hint ensure_game_is_not_over raise Errors::NoHintsError if hints.empty? hints.pop end 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 def lose? attempts_left.zero? && !win? end def win? @is_win end def over? win? || lose? end def attempts_total difficulty[:attempts] end def hints_total difficulty[:hints] end def hints_left hints.count end def save_result raise Errors::GameSaveError unless win? CreateStatService.new(self).call end private def difficulty DIFFICULTIES[@difficulty_type] end def hints @hints ||= secret_code.sample(difficulty[:hints]) end def ensure_game_is_not_over raise Errors::GameOverError if over? end def check_win(guess) @is_win = true if guess == secret_code end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
cb-core-0.1.10 | lib/codebreaker/game.rb |
cb-core-0.1.9 | lib/codebreaker/game.rb |