Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Includes:
- Storage
- Defined in:
- lib/entities/game.rb
Constant Summary collapse
- WIN =
:win- LOSE =
:lose- LEVELS =
{ easy: 'easy', medium: 'medium', hard: 'hard' }.freeze
- COMPLEXITIES =
{ easy: { hints: 2, attempts: 15, level: LEVELS[:easy] }, medium: { hints: 1, attempts: 10, level: LEVELS[:medium] }, hard: { hints: 1, attempts: 5, level: LEVELS[:hard] } }.freeze
Constants included from Storage
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#cb_numbers ⇒ Object
readonly
Returns the value of attribute cb_numbers.
-
#cb_numbers_copy ⇒ Object
readonly
Returns the value of attribute cb_numbers_copy.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #calculate_attempts ⇒ Object
- #calculate_hints ⇒ Object
- #generate_hints_attempts(level) ⇒ Object
- #guess(user_guess) ⇒ Object
- #hint ⇒ Object
-
#initialize(name, complexity) ⇒ Game
constructor
A new instance of Game.
- #take_level_hash ⇒ Object
Methods included from Storage
Constructor Details
#initialize(name, complexity) ⇒ Game
Returns a new instance of Game.
14 15 16 17 18 19 20 |
# File 'lib/entities/game.rb', line 14 def initialize(name, complexity) @name = name @level = complexity.to_sym generate_hints_attempts(@level) @cb_numbers = Core.new.generate_secret_code @cb_numbers_copy = @cb_numbers.clone end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
4 5 6 |
# File 'lib/entities/game.rb', line 4 def attempts @attempts end |
#cb_numbers ⇒ Object (readonly)
Returns the value of attribute cb_numbers.
4 5 6 |
# File 'lib/entities/game.rb', line 4 def cb_numbers @cb_numbers end |
#cb_numbers_copy ⇒ Object (readonly)
Returns the value of attribute cb_numbers_copy.
4 5 6 |
# File 'lib/entities/game.rb', line 4 def cb_numbers_copy @cb_numbers_copy end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
4 5 6 |
# File 'lib/entities/game.rb', line 4 def hints @hints end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
4 5 6 |
# File 'lib/entities/game.rb', line 4 def level @level end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/entities/game.rb', line 4 def name @name end |
Instance Method Details
#calculate_attempts ⇒ Object
47 48 49 |
# File 'lib/entities/game.rb', line 47 def calculate_attempts take_level_hash[:attempts] - @attempts end |
#calculate_hints ⇒ Object
43 44 45 |
# File 'lib/entities/game.rb', line 43 def calculate_hints take_level_hash[:hints] - @hints end |
#generate_hints_attempts(level) ⇒ Object
22 23 24 25 26 |
# File 'lib/entities/game.rb', line 22 def generate_hints_attempts(level) level_hash = take_level_hash @hints = level_hash[:hints] @attempts = level_hash[:attempts] end |
#guess(user_guess) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/entities/game.rb', line 28 def guess(user_guess) @attempts -= 1 user_guess_array = user_guess.split('').map(&:to_i) return WIN if win? user_guess_array return LOSE if lose? Core.new.check(@cb_numbers, user_guess_array) end |
#hint ⇒ Object
36 37 38 39 40 41 |
# File 'lib/entities/game.rb', line 36 def hint return nil if @hints.zero? @hints -= 1 @cb_numbers_copy.shuffle.pop end |
#take_level_hash ⇒ Object
52 53 54 |
# File 'lib/entities/game.rb', line 52 def take_level_hash COMPLEXITIES[@level] end |