Class: CodebreakerGem::Game
- Inherits:
-
Object
- Object
- CodebreakerGem::Game
- Includes:
- Loader
- Defined in:
- lib/codebreaker_gem.rb
Constant Summary collapse
- HINTS =
3- ATTEMPTS =
15
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#guess ⇒ Object
Returns the value of attribute guess.
-
#hint ⇒ Object
readonly
Returns the value of attribute hint.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
Instance Method Summary collapse
- #check ⇒ Object
- #generate_code ⇒ Object
- #get_hint ⇒ Object
- #get_scores ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
Methods included from Loader
#read_achievements, #save_achievement
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
17 18 19 20 21 |
# File 'lib/codebreaker_gem.rb', line 17 def initialize @response = [] @hints = HINTS @attempts = ATTEMPTS end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
14 15 16 |
# File 'lib/codebreaker_gem.rb', line 14 def attempts @attempts end |
#guess ⇒ Object
Returns the value of attribute guess.
15 16 17 |
# File 'lib/codebreaker_gem.rb', line 15 def guess @guess end |
#hint ⇒ Object (readonly)
Returns the value of attribute hint.
14 15 16 |
# File 'lib/codebreaker_gem.rb', line 14 def hint @hint end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
14 15 16 |
# File 'lib/codebreaker_gem.rb', line 14 def hints @hints end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
14 15 16 |
# File 'lib/codebreaker_gem.rb', line 14 def response @response end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
14 15 16 |
# File 'lib/codebreaker_gem.rb', line 14 def secret_code @secret_code end |
Instance Method Details
#check ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/codebreaker_gem.rb', line 27 def check return @response = '+' * @secret_code.length if @secret_code == @guess code, guess = @secret_code.split('').zip(@guess.split('')).delete_if { |item| item[0] == item[1] }.transpose if !code || !guess @response = ['+'] * @secret_code.length else @response = ['+'] * (@secret_code.length - code.length) @response.concat(get_minuses(code, guess)) end @response = @response.join.to_s @attempts -= 1 end |
#generate_code ⇒ Object
23 24 25 |
# File 'lib/codebreaker_gem.rb', line 23 def generate_code() @secret_code = 4.times.map{ Random.rand(1...6) }.join.to_s end |
#get_hint ⇒ Object
40 41 42 43 44 |
# File 'lib/codebreaker_gem.rb', line 40 def get_hint @hint = @hints >= 1 ? @secret_code.split('').sample : false @hints -= 1 if @hints > 0 @attempts -= 1 end |
#get_scores ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/codebreaker_gem.rb', line 46 def get_scores scores = Hash.new scores[:hints] = HINTS - @hints scores[:attempts] = ATTEMPTS - @attempts scores[:secret_code] = @secret_code scores end |