Sha256: 7ace50a10bc2e06ac33fd6de98ce7a6e9382f00c60b69ca367e6ff85e215b989

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module Codebreaker
  class Game
    attr_reader :secret_code

    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

    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.4 lib/codebreaker/game.rb