Class: CodebreakerVk::Game
- Inherits:
-
Object
- Object
- CodebreakerVk::Game
- Defined in:
- lib/codebreaker_vk/game.rb
Constant Summary collapse
- INCLUDE_IN_GAME_NUMBERS =
(1..6).freeze
- CODE_SIZE =
4- GUESS_PLACE =
'+'- GUESS_PRESENCE =
'-'
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#breaker_numbers ⇒ Object
readonly
Returns the value of attribute breaker_numbers.
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#player_name ⇒ Object
readonly
Returns the value of attribute player_name.
-
#showed_hints ⇒ Object
readonly
Returns the value of attribute showed_hints.
Instance Method Summary collapse
- #hint ⇒ Object
-
#initialize(difficulty, user) ⇒ Game
constructor
A new instance of Game.
- #lose?(result) ⇒ Boolean
- #start_round(user_input) ⇒ Object
- #to_h ⇒ Object
- #win?(result) ⇒ Boolean
Constructor Details
#initialize(difficulty, user) ⇒ Game
Returns a new instance of Game.
13 14 15 16 17 18 19 20 21 |
# File 'lib/codebreaker_vk/game.rb', line 13 def initialize(difficulty, user) @breaker_numbers = generate_random_code @breaker_numbers_copy = @breaker_numbers.clone.shuffle @hints = difficulty.level[:hints] @attempts = difficulty.level[:attempts] @difficulty = difficulty.level @player_name = user.name @showed_hints = [] end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
5 6 7 |
# File 'lib/codebreaker_vk/game.rb', line 5 def attempts @attempts end |
#breaker_numbers ⇒ Object (readonly)
Returns the value of attribute breaker_numbers.
5 6 7 |
# File 'lib/codebreaker_vk/game.rb', line 5 def breaker_numbers @breaker_numbers end |
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
5 6 7 |
# File 'lib/codebreaker_vk/game.rb', line 5 def difficulty @difficulty end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
5 6 7 |
# File 'lib/codebreaker_vk/game.rb', line 5 def hints @hints end |
#player_name ⇒ Object (readonly)
Returns the value of attribute player_name.
5 6 7 |
# File 'lib/codebreaker_vk/game.rb', line 5 def player_name @player_name end |
#showed_hints ⇒ Object (readonly)
Returns the value of attribute showed_hints.
5 6 7 |
# File 'lib/codebreaker_vk/game.rb', line 5 def showed_hints @showed_hints end |
Instance Method Details
#hint ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/codebreaker_vk/game.rb', line 29 def hint return if @hints.zero? @hints -= 1 @showed_hints << @breaker_numbers_copy.pop @showed_hints.last end |
#lose?(result) ⇒ Boolean
41 42 43 |
# File 'lib/codebreaker_vk/game.rb', line 41 def lose?(result) @attempts == 1 && @breaker_numbers != result end |
#start_round(user_input) ⇒ Object
23 24 25 26 27 |
# File 'lib/codebreaker_vk/game.rb', line 23 def start_round(user_input) @attempts -= 1 @game_numbers = { code: @breaker_numbers.clone, input: user_input } collect_place_guess + collect_presence_guess end |
#to_h ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/codebreaker_vk/game.rb', line 45 def to_h { player_name: @player_name, level: @difficulty[:level], all_hints: @difficulty[:hints], all_attempts: @difficulty[:attempts], left_hints: @hints, left_attempts: @attempts, date: Time.now } end |
#win?(result) ⇒ Boolean
37 38 39 |
# File 'lib/codebreaker_vk/game.rb', line 37 def win?(result) @breaker_numbers == result end |