Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Includes:
- Validations
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- TIME_FORMAT =
'%d %b %Y - %H:%M:%S'
- 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.
32 33 34 35 |
# File 'lib/codebreaker/game.rb', line 32 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
79 80 81 82 83 |
# File 'lib/codebreaker/game.rb', line 79 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
37 38 39 40 41 |
# File 'lib/codebreaker/game.rb', line 37 def guess(input) user_number = input.chars.map(&:to_i) validate(user_number) @user_number = user_number end |
#hint ⇒ Object
61 62 63 64 |
# File 'lib/codebreaker/game.rb', line 61 def hint @hint_keeper << tip = @hints.pop tip end |
#hints_calculations ⇒ Object
85 86 87 88 89 |
# File 'lib/codebreaker/game.rb', line 85 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
66 67 68 |
# File 'lib/codebreaker/game.rb', line 66 def hints_left? @hints.any? end |
#lost? ⇒ Boolean
57 58 59 |
# File 'lib/codebreaker/game.rb', line 57 def lost? @attempts.zero? end |
#set(input) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/codebreaker/game.rb', line 70 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
43 44 45 46 47 48 49 50 51 |
# File 'lib/codebreaker/game.rb', line 43 def start @code = @secret_code.dup @attempts -= 1 attempts_calculations hints_calculations matches_result = check_matches presence_result = check_presence @result = (matches_result + presence_result).join end |
#to_h(name) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/codebreaker/game.rb', line 91 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, time: Time.now.strftime(TIME_FORMAT) } end |
#win? ⇒ Boolean
53 54 55 |
# File 'lib/codebreaker/game.rb', line 53 def win? @result == MATCH * CODE_LENGTH end |