Sha256: fcd811d547f4fc5117c39820e5adcdc1ba53b365bfd03aac4621e42194655b9d

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

module Codebreaker
  class Game
    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 loose?
      no_attempts? && !win?
    end

    def win?
      @is_win
    end

    private

    def game_over?
      win? || loose?
    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

2 entries across 2 versions & 1 rubygems

Version Path
cb-core-0.1.1 lib/codebreaker/game.rb
cb-core-0.1.0 lib/codebreaker/game.rb