Class: Codeguessing::Game
- Inherits:
-
Object
- Object
- Codeguessing::Game
- Defined in:
- lib/codeguessing/game.rb
Constant Summary collapse
- MAX_HINT =
2- MAX_ATTEMPTS =
5
Instance Attribute Summary collapse
-
#answer ⇒ Object
Returns the value of attribute answer.
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#hint_count ⇒ Object
Returns the value of attribute hint_count.
-
#secret_code ⇒ Object
Returns the value of attribute secret_code.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #check?(varible) ⇒ Boolean
- #cur_game ⇒ Object
- #cur_score(name = 'Anonim') ⇒ Object
- #guess(code) ⇒ Object
- #hint ⇒ Object
-
#initialize(opt = {}) ⇒ Game
constructor
A new instance of Game.
- #loose ⇒ Object
- #random ⇒ Object
- #use_attempt ⇒ Object
- #use_hint ⇒ Object
- #valid?(code) ⇒ Boolean
- #win ⇒ Object
Constructor Details
#initialize(opt = {}) ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 13 14 |
# File 'lib/codeguessing/game.rb', line 8 def initialize(opt = {}) @secret_code = opt[:secret_code] || random @attempts = opt[:attempts] || MAX_ATTEMPTS @hint_count = opt[:hint_count] || MAX_HINT @state = opt[:state] || '' @answer = opt[:answer] || '' end |
Instance Attribute Details
#answer ⇒ Object
Returns the value of attribute answer.
3 4 5 |
# File 'lib/codeguessing/game.rb', line 3 def answer @answer end |
#attempts ⇒ Object
Returns the value of attribute attempts.
3 4 5 |
# File 'lib/codeguessing/game.rb', line 3 def attempts @attempts end |
#hint_count ⇒ Object
Returns the value of attribute hint_count.
3 4 5 |
# File 'lib/codeguessing/game.rb', line 3 def hint_count @hint_count end |
#secret_code ⇒ Object
Returns the value of attribute secret_code.
3 4 5 |
# File 'lib/codeguessing/game.rb', line 3 def secret_code @secret_code end |
#state ⇒ Object
Returns the value of attribute state.
3 4 5 |
# File 'lib/codeguessing/game.rb', line 3 def state @state end |
Instance Method Details
#check?(varible) ⇒ Boolean
71 72 73 74 |
# File 'lib/codeguessing/game.rb', line 71 def check?(varible) return false if varible == 0 true end |
#cur_game ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/codeguessing/game.rb', line 49 def cur_game hash = {} self.instance_variables.each do |k, v| new_k = k.to_s.gsub('@','').to_sym hash[new_k] = self.instance_variable_get(k) end hash end |
#cur_score(name = 'Anonim') ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/codeguessing/game.rb', line 58 def cur_score(name = 'Anonim') hash = cur_game hash[:name] = name hash.delete(:answer) hash.delete(:state) hash end |
#guess(code) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/codeguessing/game.rb', line 16 def guess(code) loose unless check?(use_attempt) hash = {} res = '' code.each_char.with_index do |char, i| case when char == secret_code[i] res += '+' when secret_code.count(char) == 1 && code.count(char) == 1 hash[char] = '-' end end res += hash.values.join('') win if res == '++++' @answer = res end |
#hint ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/codeguessing/game.rb', line 34 def hint res = '' need_index = rand(0...4) secret_code.each_char.with_index do |char, index| if index == need_index res += char else res += '*' end end return '' unless check?(hint_count) use_hint res end |
#loose ⇒ Object
88 89 90 |
# File 'lib/codeguessing/game.rb', line 88 def loose @state = 'false' end |
#random ⇒ Object
92 93 94 95 96 |
# File 'lib/codeguessing/game.rb', line 92 def random code = '' 4.times { code += rand(1..6).to_s } code end |
#use_attempt ⇒ Object
76 77 78 |
# File 'lib/codeguessing/game.rb', line 76 def use_attempt @attempts -= 1 end |
#use_hint ⇒ Object
80 81 82 |
# File 'lib/codeguessing/game.rb', line 80 def use_hint @hint_count -= 1 end |
#valid?(code) ⇒ Boolean
66 67 68 69 |
# File 'lib/codeguessing/game.rb', line 66 def valid?(code) return true if code =~ /^[1-6]{4}$/s false end |
#win ⇒ Object
84 85 86 |
# File 'lib/codeguessing/game.rb', line 84 def win @state = 'true' end |