Sha256: c7f0f893548b25aeacac7b1f0f73d0355315282a4c609167a291de3ac2d1de6b

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Codebreaker
  module Entities
    class Game
      DIGITS_COUNT = 4
      DIFFICULTIES = {
        easy: {
          attempts: 15,
          hints: 2
        },
        medium: {
          attempts: 10,
          hints: 1
        },
        hell: {
          attempts: 5,
          hints: 1
        }
      }.freeze
      RANGE = (1..6).freeze

      attr_reader :attempts, :hints, :code

      def initialize
        @process = Processor.new
      end

      def generate(difficulty)
        @difficulty = difficulty
        @code = generate_secret_code
        @hints = @code.sample(difficulty[:hints])
        @attempts = difficulty[:attempts]
      end

      def start_process(command)
        @process.secret_code_proc(code.join, command)
      end

      def win?(guess)
        code.join == guess
      end

      def decrease_attempts!
        @attempts -= 1
      end

      def to_h(name)
        {
          name: name,
          difficulty: DIFFICULTIES.key(@difficulty),
          all_attempts: @difficulty[:attempts],
          all_hints: @difficulty[:hints],
          attempts_used: @difficulty[:attempts] - @attempts,
          hints_used: @difficulty[:hints] - @hints.length,
          date: Time.now
        }
      end

      def hints_spent?
        hints.empty?
      end

      def take_a_hint!
        hints.pop
      end

      private

      def generate_secret_code
        Array.new(DIGITS_COUNT) { rand(RANGE) }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
codebreaker_marian-0.1.8 lib/codebreaker_marian/entities/game.rb
codebreaker_marian-0.1.7 lib/codebreaker_marian/entities/game.rb
codebreaker_marian-0.1.6 lib/codebreaker_marian/entities/game.rb
codebreaker_marian-0.1.5 lib/codebreaker/entities/game.rb
codebreaker-yeroshek-0.1.5 lib/codebreaker/entities/game.rb
codebreaker-yeroshek-0.1.4 lib/codebreaker/entities/game.rb