class CodebreakerGame::Game
Constants
- NOT_STRONG_INTERSECTION
- SECRET_LENGTH
- STATUS_IN_PROGRESS
- STATUS_LOSE
- STATUS_WIN
- STRONG_INTERSECTION
Attributes
user[R]
Public Class Methods
new()
click to toggle source
# File lib/codebreaker_game.rb, line 25 def initialize @secret_number = generate_secret(SECRET_LENGTH) @status = STATUS_IN_PROGRESS @hints = @secret_number.to_s.chars.shuffle @storage = CodebreakerGame::Game.new_store save_storage unless CodebreakerGame::Game.storage_exists? synchronize_storage end
Public Instance Methods
attempt(user_number)
click to toggle source
# File lib/codebreaker_game.rb, line 56 def attempt(user_number) return compare(@secret_number, user_number) if user.attempts? @status = STATUS_LOSE nil end
create_difficulty(difficulty:)
click to toggle source
# File lib/codebreaker_game.rb, line 52 def create_difficulty(difficulty:) CodebreakerGame::DifficultyFactory.new.difficulty_by_name(difficulty) end
create_user(name:, difficulty:)
click to toggle source
# File lib/codebreaker_game.rb, line 42 def create_user(name:, difficulty:) @user = User.new(name, difficulty) sample_hints(difficulty.hints) @user end
hint()
click to toggle source
# File lib/codebreaker_game.rb, line 71 def hint user.hint if user.hints? @hints.pop end
lose?()
click to toggle source
# File lib/codebreaker_game.rb, line 38 def lose? @status == STATUS_LOSE end
sample_hints(hints)
click to toggle source
# File lib/codebreaker_game.rb, line 48 def sample_hints(hints) @hints = @hints.sample(hints) end
save_storage()
click to toggle source
# File lib/codebreaker_game.rb, line 77 def save_storage @storage.transaction do @storage[:winners] = @winners || [] end end
sorted_winners()
click to toggle source
# File lib/codebreaker_game.rb, line 63 def sorted_winners @winners.sort_by do |winner| [winner.user.difficulty.attempts, winner.user.attempts_used, winner.user.hints_used] end end
win?()
click to toggle source
# File lib/codebreaker_game.rb, line 34 def win? @status == STATUS_WIN end
Private Instance Methods
compare(secret_number, user_number)
click to toggle source
# File lib/codebreaker_game.rb, line 90 def compare(secret_number, user_number) user.attempt compare_numbers(secret_number, user_number) end
compare_numbers(secret_number, user_number)
click to toggle source
# File lib/codebreaker_game.rb, line 95 def compare_numbers(secret_number, user_number) secret_digits = secret_number.to_s.chars user_digits = user_number.to_s.chars response = strong_intersections(secret_digits, user_digits) response += not_strong_intersections(secret_digits, user_digits) win if response == STRONG_INTERSECTION * SECRET_LENGTH response end
not_strong_intersections(secret_digits, user_digits)
click to toggle source
# File lib/codebreaker_game.rb, line 116 def not_strong_intersections(secret_digits, user_digits) response = '' secret_digits.each do |digit| next unless digit && user_digits.include?(digit) response += NOT_STRONG_INTERSECTION user_digits.delete_at(user_digits.index(digit)) end response end
strong_intersections(secret_digits, user_digits)
click to toggle source
# File lib/codebreaker_game.rb, line 104 def strong_intersections(secret_digits, user_digits) response = '' secret_digits.each_with_index do |digit, index| next unless digit == user_digits[index] response += STRONG_INTERSECTION secret_digits[index] = nil user_digits[index] = nil end response end
synchronize_storage()
click to toggle source
# File lib/codebreaker_game.rb, line 127 def synchronize_storage @storage.transaction(true) do @winners = @storage[:winners] end end
win()
click to toggle source
# File lib/codebreaker_game.rb, line 85 def win @winners << CodebreakerGame::Winner.new(user) @status = STATUS_WIN end