Class: CodebreakerVk::Game
- Inherits:
-
Object
- Object
- CodebreakerVk::Game
- Defined in:
- lib/codebreaker_vk/game.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#hints_count ⇒ Object
readonly
Returns the value of attribute hints_count.
-
#tries_count ⇒ Object
readonly
Returns the value of attribute tries_count.
Instance Method Summary collapse
- #check_guess(input) ⇒ Object
- #data ⇒ Object
- #generate_hint ⇒ Object
-
#initialize(difficulty: :kid) ⇒ Game
constructor
A new instance of Game.
- #lose? ⇒ Boolean
- #win? ⇒ Boolean
Constructor Details
#initialize(difficulty: :kid) ⇒ Game
Returns a new instance of Game.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/codebreaker_vk/game.rb', line 9 def initialize(difficulty: :kid) validate_difficulty(difficulty) @difficulty = difficulty @hint_indexes = (0...CODE_LENGTH).to_a @secret = Array.new(CODE_LENGTH) { rand(MIN_CODE_NUMBER..MAX_CODE_NUMBER) } @tries_count = DIFFICULTIES[@difficulty][:tries] @hints_count = DIFFICULTIES[@difficulty][:hints] @errors = [] @matches = '' end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/codebreaker_vk/game.rb', line 7 def errors @errors end |
#hints_count ⇒ Object (readonly)
Returns the value of attribute hints_count.
7 8 9 |
# File 'lib/codebreaker_vk/game.rb', line 7 def hints_count @hints_count end |
#tries_count ⇒ Object (readonly)
Returns the value of attribute tries_count.
7 8 9 |
# File 'lib/codebreaker_vk/game.rb', line 7 def tries_count @tries_count end |
Instance Method Details
#check_guess(input) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/codebreaker_vk/game.rb', line 26 def check_guess(input) input = to_array(input) return add_error(FormatError) unless valid? input @tries_count -= 1 resolver = GameProcess.new(@secret, input) @matches = resolver.matches end |
#data ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/codebreaker_vk/game.rb', line 60 def data { difficulty: DIFFICULTIES.keys.index(@difficulty), secret: @secret, tries_total: DIFFICULTIES[@difficulty][:tries], hints_total: DIFFICULTIES[@difficulty][:hints], tries_used: DIFFICULTIES[@difficulty][:tries] - @tries_count, hints_used: DIFFICULTIES[@difficulty][:hints] - @hints_count } end |
#generate_hint ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/codebreaker_vk/game.rb', line 38 def generate_hint return add_error(MaxHintError) if @hints_count.zero? index = @hint_indexes.sample hint = @secret[index] @hint_indexes.delete index @hints_count -= 1 hint end |
#lose? ⇒ Boolean
56 57 58 |
# File 'lib/codebreaker_vk/game.rb', line 56 def lose? @tries_count.zero? end |
#win? ⇒ Boolean
52 53 54 |
# File 'lib/codebreaker_vk/game.rb', line 52 def win? @matches == Array.new(CODE_LENGTH, EXACT_MATCH_SIGN).join end |