Class: IcodebreakerGem::GameCore
- Inherits:
-
Object
- Object
- IcodebreakerGem::GameCore
- Includes:
- Validation
- Defined in:
- lib/game_core.rb
Constant Summary
Constants included from Validation
Validation::CODE, Validation::USERNAME
Instance Attribute Summary collapse
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Class Method Summary collapse
Instance Method Summary collapse
- #compare(guess) ⇒ Object
- #hint ⇒ Object
-
#initialize(secret) ⇒ GameCore
constructor
A new instance of GameCore.
Methods included from Validation
#validate_code, #validate_difficulty, #validate_name
Constructor Details
#initialize(secret) ⇒ GameCore
Returns a new instance of GameCore.
8 9 10 11 12 13 |
# File 'lib/game_core.rb', line 8 def initialize(secret) validate_code secret @secret = secret.to_s @shuffle = @secret.chars.shuffle end |
Instance Attribute Details
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
6 7 8 |
# File 'lib/game_core.rb', line 6 def secret @secret end |
Class Method Details
.random ⇒ Object
30 31 32 |
# File 'lib/game_core.rb', line 30 def self.random (0..3).map { rand(1..6) }.join end |
Instance Method Details
#compare(guess) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/game_core.rb', line 15 def compare(guess) return '++++' if @secret == guess return '' if (@secret.chars & guess.chars).empty? pluses1, minuses1 = pluses_and_minuses(@secret, guess) pluses2, minuses2 = pluses_and_minuses(guess, @secret) '+' * [pluses1, pluses2].min + '-' * [minuses1, minuses2].min end |
#hint ⇒ Object
26 27 28 |
# File 'lib/game_core.rb', line 26 def hint @shuffle.pop end |