Class: CodebreakerDiz::Game
- Inherits:
-
Object
- Object
- CodebreakerDiz::Game
- Defined in:
- lib/codebreaker_diz/game.rb
Instance Attribute Summary collapse
-
#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
- #difficulty(value) ⇒ Object
- #generate_hint ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #jsonify ⇒ Object
- #lose? ⇒ Boolean
- #prepare_data ⇒ Object
- #win? ⇒ Boolean
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/codebreaker_diz/game.rb', line 10 def initialize @difficulty = :kid @secret = [] @tries_count = 0 @hints_count = 0 @tries_used = 0 @hints_used = 0 @guess_result = '' @hint = nil @hint_indexes = [] @game_iteration_count = 0 end |
Instance Attribute Details
#hints_count ⇒ Object (readonly)
Returns the value of attribute hints_count.
8 9 10 |
# File 'lib/codebreaker_diz/game.rb', line 8 def hints_count @hints_count end |
#tries_count ⇒ Object (readonly)
Returns the value of attribute tries_count.
8 9 10 |
# File 'lib/codebreaker_diz/game.rb', line 8 def tries_count @tries_count end |
Instance Method Details
#check_guess(input) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/codebreaker_diz/game.rb', line 48 def check_guess(input) input = to_array(input) return @guess_result = 'no tries left' if lose? return @guess_result = 'wrong format' unless valid? input make_results Matcher.calculate_matches(@secret, input) end |
#data ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/codebreaker_diz/game.rb', line 81 def data { difficulty: DIFFICULTIES.keys.index(@difficulty), secret: @secret, tries_total: DIFFICULTIES[@difficulty][:tries], hints_total: DIFFICULTIES[@difficulty][:hints], tries_used: @tries_used, hints_used: @hints_used, guess_result: @guess_result, hint: @hint } end |
#difficulty(value) ⇒ Object
29 30 31 32 33 |
# File 'lib/codebreaker_diz/game.rb', line 29 def difficulty(value) raise ArgumentError unless DIFFICULTIES.include? value.to_sym @difficulty = value.to_sym end |
#generate_hint ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/codebreaker_diz/game.rb', line 58 def generate_hint return @hint = 'no hints left' if @hints_count.zero? index = @hint_indexes.sample @hint = @secret[index] @hint_indexes.delete index @hints_count -= 1 @hints_used += 1 @hint end |
#jsonify ⇒ Object
94 95 96 |
# File 'lib/codebreaker_diz/game.rb', line 94 def jsonify data.to_json end |
#lose? ⇒ Boolean
77 78 79 |
# File 'lib/codebreaker_diz/game.rb', line 77 def lose? @tries_count.zero? end |
#prepare_data ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/codebreaker_diz/game.rb', line 35 def prepare_data @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] @game_iteration_count += 1 clear_old_data if @game_iteration_count > 1 end |
#win? ⇒ Boolean
73 74 75 |
# File 'lib/codebreaker_diz/game.rb', line 73 def win? @guess_result == Array.new(CODE_LENGTH, EXACT_MATCH_SIGN).join end |