Sha256: 3d7fd6ddae97ae6afa99dfc99a3fc4227457867a2bab4d1fd8ca3d49ffe00a5f

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 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.call
    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

    private

    def game_over?
      win? || lose?
    end

    def no_attempts?
      @difficulty.attempts == @attempts_used
    end

    def ensure_game_is_not_over
      raise Errors::GameOverError if game_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.2 lib/codebreaker/game.rb