Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Includes:
- Validations
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- CODE_DIGITS =
(1..6).freeze
- CODE_LENGTH =
4- MATCH =
'+'- PRESENCE =
'-'- LEVELS =
{ silly: { attempts: 15, hints: 2, command: 'silly' }, moderate: { attempts: 10, hints: 1, command: 'moderate' }, clever: { attempts: 5, hints: 1, command: 'clever' } }.freeze
Instance Attribute Summary collapse
-
#attempts_available ⇒ Object
readonly
Returns the value of attribute attempts_available.
-
#attempts_total ⇒ Object
readonly
Returns the value of attribute attempts_total.
-
#attempts_used ⇒ Object
readonly
Returns the value of attribute attempts_used.
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#hint_keeper ⇒ Object
readonly
Returns the value of attribute hint_keeper.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#hints_available ⇒ Object
readonly
Returns the value of attribute hints_available.
-
#hints_total ⇒ Object
readonly
Returns the value of attribute hints_total.
-
#hints_used ⇒ Object
readonly
Returns the value of attribute hints_used.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
Instance Method Summary collapse
- #attempts_calculations ⇒ Object
- #guess(input) ⇒ Object
- #hint ⇒ Object
- #hints_calculations ⇒ Object
- #hints_left? ⇒ Boolean
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #lost? ⇒ Boolean
- #set(input) ⇒ Object
- #start ⇒ Object
- #to_h(name) ⇒ Object
- #win? ⇒ Boolean
Methods included from Validations
#match_checker, #range_checker
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
31 32 33 34 |
# File 'lib/codebreaker/game.rb', line 31 def initialize @secret_code = Array.new(CODE_LENGTH) { rand(CODE_DIGITS) } @errors = [] end |
Instance Attribute Details
#attempts_available ⇒ Object (readonly)
Returns the value of attribute attempts_available.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def attempts_available @attempts_available end |
#attempts_total ⇒ Object (readonly)
Returns the value of attribute attempts_total.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def attempts_total @attempts_total end |
#attempts_used ⇒ Object (readonly)
Returns the value of attribute attempts_used.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def attempts_used @attempts_used end |
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def difficulty @difficulty end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def errors @errors end |
#hint_keeper ⇒ Object (readonly)
Returns the value of attribute hint_keeper.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def hint_keeper @hint_keeper end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def hints @hints end |
#hints_available ⇒ Object (readonly)
Returns the value of attribute hints_available.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def hints_available @hints_available end |
#hints_total ⇒ Object (readonly)
Returns the value of attribute hints_total.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def hints_total @hints_total end |
#hints_used ⇒ Object (readonly)
Returns the value of attribute hints_used.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def hints_used @hints_used end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def result @result end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
6 7 8 |
# File 'lib/codebreaker/game.rb', line 6 def secret_code @secret_code end |
Instance Method Details
#attempts_calculations ⇒ Object
76 77 78 79 80 |
# File 'lib/codebreaker/game.rb', line 76 def attempts_calculations @attempts_total = LEVELS.dig(@difficulty.to_sym, :attempts) @attempts_used = LEVELS.dig(@difficulty.to_sym, :attempts) - @attempts @attempts_available = @attempts_total - @attempts_used end |
#guess(input) ⇒ Object
36 37 38 39 40 |
# File 'lib/codebreaker/game.rb', line 36 def guess(input) user_number = input.chars.map(&:to_i) validate(user_number) @user_number = user_number end |
#hint ⇒ Object
58 59 60 61 |
# File 'lib/codebreaker/game.rb', line 58 def hint @hint_keeper << tip = @hints.pop tip end |
#hints_calculations ⇒ Object
82 83 84 85 86 |
# File 'lib/codebreaker/game.rb', line 82 def hints_calculations @hints_total = LEVELS.dig(@difficulty.to_sym, :hints) @hints_used = LEVELS.dig(@difficulty.to_sym, :hints) - @hints.size @hints_available = @hints_total - @hints_used end |
#hints_left? ⇒ Boolean
63 64 65 |
# File 'lib/codebreaker/game.rb', line 63 def hints_left? @hints.any? end |
#lost? ⇒ Boolean
54 55 56 |
# File 'lib/codebreaker/game.rb', line 54 def lost? @attempts.zero? end |
#set(input) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/codebreaker/game.rb', line 67 def set(input) return unless LEVELS.key?(input.to_sym) @difficulty = LEVELS.dig(input.to_sym, :command) @hints = @secret_code.sample(LEVELS.dig(@difficulty.to_sym, :hints)) @attempts = LEVELS.dig(@difficulty.to_sym, :attempts) @hint_keeper = [] end |
#start ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/codebreaker/game.rb', line 42 def start @code = @secret_code.dup @attempts -= 1 matches_result = check_matches presence_result = check_presence @result = (matches_result + presence_result).join end |
#to_h(name) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/codebreaker/game.rb', line 88 def to_h(name) { name: name, difficulty: @difficulty, attempts_total: LEVELS.dig(@difficulty.to_sym, :attempts), attempts_used: LEVELS.dig(@difficulty.to_sym, :attempts) - @attempts, hints_total: LEVELS.dig(@difficulty.to_sym, :hints), hints_used: LEVELS.dig(@difficulty.to_sym, :hints) - @hints.size } end |
#win? ⇒ Boolean
50 51 52 |
# File 'lib/codebreaker/game.rb', line 50 def win? @result == MATCH * CODE_LENGTH end |