Sha256: 177fb9498d5d961a635a85bfac4a7835cbb80ef7d8eba3aebd1e4ad19a96e434
Contents?: true
Size: 1.81 KB
Versions: 2
Compression:
Stored size: 1.81 KB
Contents
# frozen_string_literal: true module Codebreaker # Game of bulls and cows. class Game attr_reader :storage, :matcher, :hints, :status attr_accessor :user, :number_of_attempts, :number_of_hints, :attempts_left, :hints_left, :difficulty, :secret_code def initialize(matcher: Matcher.new) @storage = storage @matcher = matcher end def start generate_secret_code generate_hints @attempts_left = @number_of_attempts = 0 @hints_left = @number_of_hints = 0 matcher.secret_code = @secret_code @status = :in_progress @difficulty end def attempts_available? return false if won? || lost? attempts_left.positive? || lost end def guess(guess) @attempts_left -= 1 matcher.match?(guess) && won end def marks matcher.marks end def hint! if hints_left.positive? @hints_left -= 1 hints.shift end end def won? status == :won end def lost? status == :lost end def in_progress? status == :in_progress end def save_stat(stat_file) File.open(stat_file, 'a+') do |f| f.write({ user: @user, difficulty: @difficulty, attempts: @number_of_attempts, attempts_used: @number_of_attempts - @attempts_left, hints: @number_of_hints, hints_used: @number_of_hints - @hints_left }.to_yaml) end end private def generate_secret_code @secret_code = Array.new(4) { roll_dice }.join end def roll_dice rand(1..6) end def generate_hints @hints = @secret_code.split('').shuffle end def won @status = :won true end def lost @status = :lost false end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
codebreaker_mats-0.1.4 | lib/codebreaker_mats/game.rb |
codebreaker_mats-0.1.3 | lib/codebreaker_mats/game.rb |