Sha256: d174362a00d000da6046c0a3cd9f191eacb72b9b3e23b4dcee0f63278844f34d

Contents?: true

Size: 1006 Bytes

Versions: 1

Compression:

Stored size: 1006 Bytes

Contents

require_relative 'services/matching_service'
require_relative 'services/statistic_service'
class Game
  attr_accessor :player_name, :secret_code, :user_code, :attempts, :hints, :difficulty_name
  attr_reader :stats
  def initialize(player_name, difficulty)
    @stats = Statistic.stats
    @player_name = player_name
    @difficulty_name = DIFFICULTIES.key(difficulty).to_s
    @attempts = difficulty[:attempts]
    @hints = difficulty[:hints]
    @db = DB
  end

  def give_hint
    @hints -= 1
    @secret_code.sample
  end

  def run
    @secret_code = generate_code
  end

  def generate_code
    Array.new(4) { rand(1..6) }
  end

  def result(response)
    @user_code = response.each_char.map(&:to_i)
    @attempts -= 1
    return '++++' if @secret_code == user_code

    Matching.create_response(self)
  end

  def save_result
    @stats.push(Statistic.generate_stats(self))
    DbUtils.add(@db, @stats)
  end

  def won?(result)
    result == '++++'
  end

  def lost?
    attempts.zero?
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codebreaker_paratskiy-0.1.3 lib/game.rb