Class: NewSuperCodebreaker2021::Game
Constant Summary
collapse
- GUESS_COMMANDS =
%i[hint rules exit].freeze
- START_COMMANDS =
%i[start rules stats exit].freeze
- DIFFICULTY_COMMANDS =
%i[easy medium hell exit].freeze
- AFTER_GAME_COMMANDS =
%i[start save exit].freeze
- YES_NO_COMMANDS =
%i[yes no].freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Validate
#check_input, #validate_name, #validate_user_code
#show_stats
Methods included from DBMethods
#load_file, #save
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
9
10
11
12
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 9
def initialize
@code = generate_code
@code_copy = @code.dup
end
|
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
7
8
9
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 7
def code
@code
end
|
Instance Method Details
#after_game_commands(command) ⇒ Object
51
52
53
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 51
def after_game_commands(command)
check_input(command, AFTER_GAME_COMMANDS)
end
|
#attempt_to_start(command) ⇒ Object
55
56
57
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 55
def attempt_to_start(command)
check_input(command, YES_NO_COMMANDS)
end
|
#chose_command(command) ⇒ Object
20
21
22
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 20
def chose_command(command)
check_input(command, START_COMMANDS)
end
|
#chose_difficulty(difficulty) ⇒ Object
32
33
34
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 32
def chose_difficulty(difficulty)
check_input(difficulty, DIFFICULTY_COMMANDS)
end
|
#compare_codes(user_code) ⇒ Object
59
60
61
62
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 59
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) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 43
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) ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 24
def take_name(input_name)
if input_name == 'exit'
:exit
else
validate_name(input_name)
end
end
|
#user_guess(code) ⇒ Object
36
37
38
39
40
41
|
# File 'lib/new_super_codebreaker_2021/game.rb', line 36
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
|