class Codebreaker::Game
Constants
- LENGTH_CODE
- RANGE_SECRET_NUMBER
Attributes
attempts[R]
count_minus[R]
count_plus[R]
difficulty[R]
hints[R]
secret_hash[R]
user[R]
Public Class Methods
new()
click to toggle source
# File lib/codebreaker/game.rb, line 25 def initialize @secret_hash = generate_secret @number_for_hint = @secret_hash.values.shuffle end
Public Instance Methods
change_secret_hash(hash)
click to toggle source
# File lib/codebreaker/game.rb, line 34 def change_secret_hash(hash) @secret_hash = hash end
check_attempt(user_string)
click to toggle source
# File lib/codebreaker/game.rb, line 55 def check_attempt(user_string) Validate.code_length?(user_string) Validate.code_range?(user_string) @user_hash = Hash[(0..LENGTH_CODE).zip user_string.split('').map(&:to_i)] @attempts -= 1 compare_hashes(@secret_hash) end
choose_difficulty(difficulty)
click to toggle source
# File lib/codebreaker/game.rb, line 42 def choose_difficulty(difficulty) @difficulty = Difficulty.new(difficulty) @attempts = @difficulty.attempts @hints = @difficulty.hints end
compare_hashes(secret_hash)
click to toggle source
# File lib/codebreaker/game.rb, line 63 def compare_hashes(secret_hash) @count_plus = 0 @count_minus = 0 secret_hash.merge(@user_hash) do |_k, o, n| @user_hash.reject! { |_key, val| val == n } && @count_plus += 1 if o == n end secret_hash.select { |_, value| @user_hash.value? value }.size.times { @count_minus += 1 } end
create_user(name)
click to toggle source
# File lib/codebreaker/game.rb, line 38 def create_user(name) @user = User.new(name) end
generate_secret()
click to toggle source
# File lib/codebreaker/game.rb, line 30 def generate_secret Hash[(0...LENGTH_CODE).zip Array.new(LENGTH_CODE) { rand(1...RANGE_SECRET_NUMBER) }] end
hint!()
click to toggle source
# File lib/codebreaker/game.rb, line 48 def hint! return false if @hints.zero? @hints -= 1 @number_for_hint.pop end
save()
click to toggle source
# File lib/codebreaker/game.rb, line 78 def save Storage.new.save_to_file(to_h) end
to_h()
click to toggle source
# File lib/codebreaker/game.rb, line 72 def to_h { name: @user.name, difficult: @difficulty.title, total_attempts: @difficulty.attempts, attempts: @attempts, total_hints: @difficulty.hints, hints: @hints } end