Class: Codebreaker::Game
Instance Attribute Summary collapse
Attributes inherited from BaseClass
#errors
Instance Method Summary
collapse
Methods inherited from BaseClass
#clear_errors, #handle_errors, #show_errors
Methods included from Validator
#valid?
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
7
8
9
10
11
|
# File 'lib/codebreaker/game.rb', line 7
def initialize
super()
@secret_code = generate_code
@stat = Statistic.new
end
|
Instance Attribute Details
#level ⇒ Object
Returns the value of attribute level.
5
6
7
|
# File 'lib/codebreaker/game.rb', line 5
def level
@level
end
|
#secret_code ⇒ Object
Returns the value of attribute secret_code.
5
6
7
|
# File 'lib/codebreaker/game.rb', line 5
def secret_code
@secret_code
end
|
#stats ⇒ Object
Returns the value of attribute stats.
5
6
7
|
# File 'lib/codebreaker/game.rb', line 5
def stats
@stats
end
|
#total_attempts ⇒ Object
Returns the value of attribute total_attempts.
5
6
7
|
# File 'lib/codebreaker/game.rb', line 5
def total_attempts
@total_attempts
end
|
#total_hints ⇒ Object
Returns the value of attribute total_hints.
5
6
7
|
# File 'lib/codebreaker/game.rb', line 5
def total_hints
@total_hints
end
|
#used_attempts ⇒ Object
Returns the value of attribute used_attempts.
5
6
7
|
# File 'lib/codebreaker/game.rb', line 5
def used_attempts
@used_attempts
end
|
#used_hints ⇒ Object
Returns the value of attribute used_hints.
5
6
7
|
# File 'lib/codebreaker/game.rb', line 5
def used_hints
@used_hints
end
|
Instance Method Details
#accept_level(level) ⇒ Object
49
50
51
52
53
|
# File 'lib/codebreaker/game.rb', line 49
def accept_level(level)
current_level = Constants::LEVELS[level.to_sym]
handle_errors('entered level is not present') unless current_level
current_level
end
|
#add_level(choice_level) ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/codebreaker/game.rb', line 66
def add_level(choice_level)
@level = level
@used_hints = choice_level[:hints]
@total_hints = choice_level[:hints]
@total_attempts = choice_level[:attempts]
@used_attempts = choice_level[:attempts]
end
|
#check_guess(numbers) ⇒ Object
74
75
76
77
78
79
80
81
82
|
# File 'lib/codebreaker/game.rb', line 74
def check_guess(numbers)
return show_errors unless guess_valid?(numbers)
array_choose_numbers = numbers.to_s.each_char.map(&:to_i)
return win if secret_code == array_choose_numbers
decrease_attempts
codebreaker_result(array_choose_numbers)
end
|
#create_level(level) ⇒ Object
55
56
57
58
59
60
|
# File 'lib/codebreaker/game.rb', line 55
def create_level(level)
choice_level = accept_level(level)
return if choice_level.nil?
add_level(choice_level)
end
|
#create_settings(name, level) ⇒ Object
35
36
37
38
39
40
|
# File 'lib/codebreaker/game.rb', line 35
def create_settings(name, level)
player_name(name)
create_level(level)
show_errors unless valid?
end
|
#decrease_attempts ⇒ Object
13
14
15
16
17
|
# File 'lib/codebreaker/game.rb', line 13
def decrease_attempts
return secret_code && 'lose' if @used_attempts.zero?
@used_attempts -= 1
end
|
#difficulties ⇒ Object
62
63
64
|
# File 'lib/codebreaker/game.rb', line 62
def difficulties
Constants::LEVELS.keys
end
|
#load_stats ⇒ Object
31
32
33
|
# File 'lib/codebreaker/game.rb', line 31
def load_stats
@stat.show_statistic
end
|
#player_name(name) ⇒ Object
42
43
44
45
46
47
|
# File 'lib/codebreaker/game.rb', line 42
def player_name(name)
user = User.new(name)
return @player_name = user.name if user.valid?
errors << user.errors
end
|
#save_stats ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/codebreaker/game.rb', line 19
def save_stats
hash = {
user_name: @player_name,
difficulty: level,
attempts_total: total_attempts,
attempts_used: used_attempts,
hints_total: total_hints,
hints_used: used_hints
}
@stat.collect_statistic(hash)
end
|
#use_hint ⇒ Object
84
85
86
87
88
89
|
# File 'lib/codebreaker/game.rb', line 84
def use_hint
return 'Hints are over' if @used_hints.zero?
@used_hints -= 1
secret_code.sample
end
|
#win ⇒ Object
91
92
93
|
# File 'lib/codebreaker/game.rb', line 91
def win
"#{Constants::PLUS * Constants::WIN_NUMBER}(win)"
end
|