Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebraker_ov/entities/game.rb
Constant Summary collapse
- DIFFICULTIES =
{ low: { attempts: 15, hints: 2 }, medium: { attempts: 10, hints: 2 }, hell: { attempts: 5, hints: 1 } }.freeze
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#attempts_total ⇒ Object
readonly
Returns the value of attribute attempts_total.
-
#hints_available ⇒ Object
readonly
Returns the value of attribute hints_available.
-
#hints_given ⇒ Object
readonly
Returns the value of attribute hints_given.
-
#hints_used ⇒ Object
readonly
Returns the value of attribute hints_used.
-
#level_name ⇒ Object
readonly
Returns the value of attribute level_name.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
- #compare_with(guess) ⇒ Object
-
#initialize(level) ⇒ Game
constructor
A new instance of Game.
- #lose? ⇒ Boolean
- #receive_hint ⇒ Object
- #win?(guess) ⇒ Boolean
Constructor Details
#initialize(level) ⇒ Game
Returns a new instance of Game.
20 21 22 23 24 25 26 27 |
# File 'lib/codebraker_ov/entities/game.rb', line 20 def initialize(level) @secret = Array.new(4) { rand(1..6).to_s } @level_name = level.to_s @attempts_total = DIFFICULTIES[level][:attempts] @attempts = DIFFICULTIES[level][:attempts] @hints_available = @secret.sample(DIFFICULTIES[level][:hints]) @hints_given = [] end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
3 4 5 |
# File 'lib/codebraker_ov/entities/game.rb', line 3 def attempts @attempts end |
#attempts_total ⇒ Object (readonly)
Returns the value of attribute attempts_total.
3 4 5 |
# File 'lib/codebraker_ov/entities/game.rb', line 3 def attempts_total @attempts_total end |
#hints_available ⇒ Object (readonly)
Returns the value of attribute hints_available.
3 4 5 |
# File 'lib/codebraker_ov/entities/game.rb', line 3 def hints_available @hints_available end |
#hints_given ⇒ Object (readonly)
Returns the value of attribute hints_given.
3 4 5 |
# File 'lib/codebraker_ov/entities/game.rb', line 3 def hints_given @hints_given end |
#hints_used ⇒ Object (readonly)
Returns the value of attribute hints_used.
3 4 5 |
# File 'lib/codebraker_ov/entities/game.rb', line 3 def hints_used @hints_used end |
#level_name ⇒ Object (readonly)
Returns the value of attribute level_name.
3 4 5 |
# File 'lib/codebraker_ov/entities/game.rb', line 3 def level_name @level_name end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
3 4 5 |
# File 'lib/codebraker_ov/entities/game.rb', line 3 def result @result end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
3 4 5 |
# File 'lib/codebraker_ov/entities/game.rb', line 3 def secret @secret end |
Instance Method Details
#compare_with(guess) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/codebraker_ov/entities/game.rb', line 52 def compare_with(guess) @guess = guess.chars @result = '' rest = check_full_matches check_partial_matches(rest) @attempts -= 1 @result end |
#lose? ⇒ Boolean
74 75 76 |
# File 'lib/codebraker_ov/entities/game.rb', line 74 def lose? !@attempts.positive? end |
#receive_hint ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/codebraker_ov/entities/game.rb', line 62 def receive_hint return unless @hints_available.length.positive? @hints_given << @hints_available.pop @hints_given.last end |
#win?(guess) ⇒ Boolean
70 71 72 |
# File 'lib/codebraker_ov/entities/game.rb', line 70 def win?(guess) @secret == guess.chars end |