Sha256: 70a55e09fb593db1cc4300de61c079d8f296f0aa3dd7e492b0d2c82ebfaf1711

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

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
    }
  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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
codebreaker-yeroshek-0.1.1 lib/app/entities/game.rb
codebreaker_rg-0.1.2 lib/app/entities/game.rb
codebreaker_rg-0.1.1 lib/app/entities/game.rb
codebreaker_rg-0.1.0 lib/app/entities/game.rb