class NewSuperCodebreaker2021::Game
Constants
- AFTER_GAME_COMMANDS
- DIFFICULTY_COMMANDS
- GUESS_COMMANDS
- START_COMMANDS
- YES_NO_COMMANDS
Attributes
code[R]
Public Class Methods
new()
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 11 def initialize @code = generate_code @code_copy = @code.dup end
Public Instance Methods
after_game_commands(command)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 53 def after_game_commands(command) check_input(command, AFTER_GAME_COMMANDS) end
attempt_to_start(command)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 57 def attempt_to_start(command) check_input(command, YES_NO_COMMANDS) end
chose_command(command)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 22 def chose_command(command) check_input(command, START_COMMANDS) end
chose_difficulty(difficulty)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 34 def chose_difficulty(difficulty) check_input(difficulty, DIFFICULTY_COMMANDS) end
compare_codes(user_code)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 61 def compare_codes(user_code) matches, user_code, code_copy = number_on_right_place(user_code) number_in_secret_code(user_code, matches, code_copy) end
take_hint(user, used_hints)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 45 def take_hint(user, used_hints) return unless user.hints_total > user.hints_used user.hints_used += 1 used_hints.each { |hint| @code_copy.delete(hint) } @code_copy.sample end
take_name(input_name)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 26 def take_name(input_name) if input_name == 'exit' :exit else validate_name(input_name) end end
user_guess(code)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 38 def user_guess(code) return validate_user_code(code) unless code.to_i.zero? symbol_code = code.to_sym GUESS_COMMANDS.include?(symbol_code) ? symbol_code : nil end
Private Instance Methods
generate_code()
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 94 def generate_code Array.new(4) { rand(1..6) } end
number_in_secret_code(user_code, matches, code_copy)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 81 def number_in_secret_code(user_code, matches, code_copy) amount_numbers_in_secret_code = Hash.new(0) amount_numbers_in_user_code = Hash.new(0) code_copy.each { |number| amount_numbers_in_secret_code[number] += 1 } user_code.each do |element| if code_copy.include?(element) && amount_numbers_in_user_code[element] < amount_numbers_in_secret_code[element] matches.push('-') amount_numbers_in_user_code[element] += 1 end end matches end
number_on_right_place(user_code)
click to toggle source
# File lib/new_super_codebreaker_2021/game.rb, line 68 def number_on_right_place(user_code) code_copy = @code.dup matches = [] user_code.each_index do |i| next unless @code[i] == user_code[i] matches.unshift('+') user_code[i] = nil code_copy[i] = false end [matches, user_code, code_copy] end