Class: Codeguessing::Game
- Inherits:
-
Object
- Object
- Codeguessing::Game
- Defined in:
- lib/codeguessing/game.rb
Constant Summary collapse
- MAX_HINT =
2- MAX_ATTEMPTS =
5- MAX_SIZE =
4
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
- #cur_game ⇒ Object
- #cur_score(name = 'Anonim') ⇒ Object
- #get_mark(code) ⇒ Object
- #guess(code) ⇒ Object
- #hint ⇒ Object
-
#initialize(opt = {}) ⇒ Game
constructor
A new instance of Game.
- #use_attempt ⇒ Object
- #use_hint ⇒ Object
- #valid?(code) ⇒ Boolean
- #win? ⇒ Boolean
Constructor Details
#initialize(opt = {}) ⇒ Game
Returns a new instance of Game.
9 10 11 12 13 14 15 |
# File 'lib/codeguessing/game.rb', line 9 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
#cur_game ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/codeguessing/game.rb', line 78 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
49 50 51 52 53 54 55 56 |
# File 'lib/codeguessing/game.rb', line 49 def cur_score(name = 'Anonim') hash = cur_game hash[:name] = name hash[:attempts] = MAX_SIZE - attempts hash[:hint_count] = MAX_HINT - hint_count hash[:date] = Time.now.to_i hash end |
#get_mark(code) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/codeguessing/game.rb', line 23 def get_mark(code) return false unless valid?(code) hash = {} res = '' secret_code.each_char.with_index do |char, i| if code[i] == char res += '+' code[i] = '_' hash.delete(char) elsif code.include?(char) hash[char] = '-' end end res += hash.values.join('') end |
#guess(code) ⇒ Object
17 18 19 20 21 |
# File 'lib/codeguessing/game.rb', line 17 def guess(code) @state = 'loose' unless check?(use_attempt) @state = 'win' if code == secret_code @answer = get_mark(code) end |
#hint ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/codeguessing/game.rb', line 39 def hint return '' unless check?(hint_count) use_hint hint = '*' * MAX_SIZE index = rand(0...MAX_SIZE) code_char = secret_code[index] hint[index] = code_char hint end |
#use_attempt ⇒ Object
63 64 65 |
# File 'lib/codeguessing/game.rb', line 63 def use_attempt @attempts -= 1 end |
#use_hint ⇒ Object
67 68 69 |
# File 'lib/codeguessing/game.rb', line 67 def use_hint @hint_count -= 1 end |
#valid?(code) ⇒ Boolean
58 59 60 61 |
# File 'lib/codeguessing/game.rb', line 58 def valid?(code) return true if code =~ /^[1-6]{#{MAX_SIZE}}$/s false end |
#win? ⇒ Boolean
71 72 73 74 75 76 |
# File 'lib/codeguessing/game.rb', line 71 def win? case @state when 'win' then true when 'loose' then false end end |