Class: CodebrekerManfly::Game
Instance Attribute Summary collapse
#errors
Class Method Summary
collapse
Instance Method Summary
collapse
#valid?
Methods included from Validator
#valid_class?, #valid_non_empty_string?, #valid_non_negative_integer?, #valid_only_numeric_string?, #valid_positive_integer?, #valid_range?, #valid_string_max_length?, #valid_string_min_length?
Constructor Details
#initialize(difficulty, user) ⇒ Game
Returns a new instance of Game.
7
8
9
10
11
|
# File 'lib/codebreker_manfly/entities/game.rb', line 7
def initialize(difficulty, user)
super()
@difficulty = difficulty
@user = user
end
|
Instance Attribute Details
#attempts_amount ⇒ Object
Returns the value of attribute attempts_amount.
5
6
7
|
# File 'lib/codebreker_manfly/entities/game.rb', line 5
def attempts_amount
@attempts_amount
end
|
#code ⇒ Object
Returns the value of attribute code.
5
6
7
|
# File 'lib/codebreker_manfly/entities/game.rb', line 5
def code
@code
end
|
#difficulty ⇒ Object
Returns the value of attribute difficulty.
5
6
7
|
# File 'lib/codebreker_manfly/entities/game.rb', line 5
def difficulty
@difficulty
end
|
#hints_amount ⇒ Object
Returns the value of attribute hints_amount.
5
6
7
|
# File 'lib/codebreker_manfly/entities/game.rb', line 5
def hints_amount
@hints_amount
end
|
#user ⇒ Object
Returns the value of attribute user.
5
6
7
|
# File 'lib/codebreker_manfly/entities/game.rb', line 5
def user
@user
end
|
Class Method Details
.user_statistic ⇒ Object
17
18
19
20
|
# File 'lib/codebreker_manfly/entities/game.rb', line 17
def self.user_statistic
store = CodebrekerManfly::CodebreakerStore.new
store.data[:user_statistics].sort_by { |stats| [stats.difficulty, stats.attempts, stats.hints] }
end
|
Instance Method Details
#lose? ⇒ Boolean
45
46
47
|
# File 'lib/codebreker_manfly/entities/game.rb', line 45
def lose?
attempts_amount < 1 && !win?
end
|
#make_turn(guess) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/codebreker_manfly/entities/game.rb', line 33
def make_turn(guess)
@guess = guess
@attempts_amount -= 1
matcher = CodebrekerManfly::CodeMatcher.new(code, guess.code)
matcher.match_codes
end
|
#restart ⇒ Object
49
50
51
|
# File 'lib/codebreker_manfly/entities/game.rb', line 49
def restart
prepare_game
end
|
#save_statistic ⇒ Object
22
23
24
25
26
|
# File 'lib/codebreker_manfly/entities/game.rb', line 22
def save_statistic
store = CodebrekerManfly::CodebreakerStore.new
store.data[:user_statistics] << current_statistic
store.save
end
|
#start ⇒ Object
13
14
15
|
# File 'lib/codebreker_manfly/entities/game.rb', line 13
def start
prepare_game
end
|
#take_hint ⇒ Object
28
29
30
31
|
# File 'lib/codebreker_manfly/entities/game.rb', line 28
def take_hint
@hints_amount -= 1
@hints.pop
end
|
#win? ⇒ Boolean
41
42
43
|
# File 'lib/codebreker_manfly/entities/game.rb', line 41
def win?
code == @guess&.code
end
|