Sha256: c05ac0ead3a86c5ff8b2f77f4ac7dafa315b6c6e8294f1983be9cdc8db896ac5
Contents?: true
Size: 1.37 KB
Versions: 3
Compression:
Stored size: 1.37 KB
Contents
require_relative '../constants' require_relative 'user' require_relative 'difficulty' module CodebreakerKarolina class Game include CodebreakerConstants attr_accessor :user, :difficulty attr_reader :secret_code def start @secret_code = generate_code.freeze @hint = @secret_code.chars.uniq end def compare_code(guess) return unless valid_code?(guess) @user.used_attempts += 1 comparison_result(@secret_code.chars, guess.chars) end def win?(guess) return true if @secret_code == guess false end def lose? return true if @user.used_attempts >= @difficulty.attempts false end def hint return unless hints_left? @user.used_hints += 1 @hint.shuffle!.pop end private def valid_code?(code) return true if code =~ (/[1-6]{4}/) false end def generate_code Array.new(CODE_SIZE) { rand(NUM_RANGE) }.join end def comparison_result(secret_code, user_code) result = '' user_code.each_with_index do |num, index| next unless secret_code.include? num result += num == secret_code[index] ? PLUS : MINUS secret_code[secret_code.index(num)] = nil end result.chars.sort.join end def hints_left? return true if @difficulty.hints > @user.used_hints false end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
codebreaker_karolina-0.1.4 | lib/modules/game.rb |
codebreaker_karolina-0.1.3 | lib/modules/game.rb |
codebreaker_karolina-0.1.2 | lib/modules/game.rb |