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 ⇒ Game
constructor
A new instance of Game.
- #valid?(code) ⇒ Boolean
- #win? ⇒ Boolean
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
9 10 11 12 13 14 15 |
# File 'lib/codeguessing/game.rb', line 9 def initialize @secret_code = random @attempts = MAX_ATTEMPTS @hint_count = MAX_HINT @state = '' @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
73 74 75 76 77 78 |
# File 'lib/codeguessing/game.rb', line 73 def cur_game attributes = instance_variables.map do |var| [var[1..-1].to_sym, instance_variable_get(var)] end attributes.to_h end |
#cur_score(name = 'Anonim') ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/codeguessing/game.rb', line 64 def cur_score(name = 'Anonim') scores = cur_game scores[:name] = name scores[:attempts] = MAX_SIZE - attempts scores[:hint_count] = MAX_HINT - hint_count scores[:date] = Time.now.to_i scores end |
#get_mark(code) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/codeguessing/game.rb', line 26 def get_mark(code) raise 'Invalid data' unless valid?(code) mark = '' secret_codes = secret_code.chars codes = code.chars secret_codes.each_with_index do |char, index| next unless char == codes[index] secret_codes[index] = nil codes[index] = nil mark += '+' end secret_codes.compact.each_with_index do |char, index| next unless code_index = codes.index(char) codes[code_index] = nil mark += '-' end mark end |
#guess(code) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/codeguessing/game.rb', line 17 def guess(code) @state = 'loose' unless natural?(use_attempt) if code == secret_code @state = 'win' return @answer = '+' * MAX_SIZE end @answer = get_mark(code) end |
#hint ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/codeguessing/game.rb', line 48 def hint return '' unless natural?(hint_count) use_hint hint = '*' * MAX_SIZE index = rand(0...MAX_SIZE) hint[index] = secret_code[index] hint end |
#valid?(code) ⇒ Boolean
80 81 82 83 |
# File 'lib/codeguessing/game.rb', line 80 def valid?(code) return true if code =~ /^[1-6]{#{MAX_SIZE}}$/s false end |
#win? ⇒ Boolean
57 58 59 60 61 62 |
# File 'lib/codeguessing/game.rb', line 57 def win? case @state when 'win' then true when 'loose' then false end end |