Sha256: 10d1da0965e3edba33f3fdd4a29a4ee203baafb8a68e477da19afe07554eb14a

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 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)
        decrease_attempts!
        @process.secret_code_proc(code.join, command)
      end

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

      def decrease_attempts!
        @attempts -= 1 if @attempts != 0
      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

3 entries across 3 versions & 1 rubygems

Version Path
codebreaker_marian-0.3.4 lib/codebreaker_marian/entities/game.rb
codebreaker_marian-0.3.3 lib/codebreaker_marian/entities/game.rb
codebreaker_marian-0.3.2 lib/codebreaker_marian/entities/game.rb