Sha256: 5b3437174b12be33387c3e0e47b0a374554d713bda53a2777378ee5f65046ea4
Contents?: true
Size: 1.22 KB
Versions: 1
Compression:
Stored size: 1.22 KB
Contents
module Codebreaker class Game attr_reader :secret_code, :player, :difficulty def initialize(player, difficulty_type) @player = player @secret_code = SecretCodeGenerator.new.call @difficulty = DifficultyFactory.build(difficulty_type) @hint = Hint.new(@difficulty, @secret_code) @attempts_used = 0 @is_win = false end def take_hint ensure_game_is_not_over @hint.take end def guess_secret_code(guess) ensure_game_is_not_over @attempts_used += 1 matching = SecretCodeMatcher.new(@secret_code, guess).call check_win(matching) matching end def lose? no_attempts? && !win? end def win? @is_win end def over? win? || lose? end def hints_left @hint.count end def attempts_left @difficulty.attempts - @attempts_used end def save_result Stats.new.save_game(self) end private def no_attempts? @difficulty.attempts == @attempts_used end def ensure_game_is_not_over raise Errors::GameOverError if over? end def check_win(combination) @is_win = true if SecretCodeMatcher.win_combination?(combination) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
cb-core-0.1.6 | lib/codebreaker/game.rb |