Class: IcodebreakerGem::Game
- Inherits:
-
Object
- Object
- IcodebreakerGem::Game
- Includes:
- Storage, Validation
- Defined in:
- lib/game.rb
Constant Summary collapse
- STATUS =
%i[go_on over].freeze
- RESULT =
%i[win lose].freeze
- DIFFICULTIES =
{ easy: { attempts_total: 15, hints_total: 2 }, medium: { attempts_total: 10, hints_total: 1 }, hell: { attempts_total: 5, hints_total: 1 } }.freeze
Constants included from Storage
Storage::DATA_FILE, Storage::STORAGE_PATH
Constants included from Validation
Validation::CODE, Validation::USERNAME
Instance Attribute Summary collapse
-
#attempts_used ⇒ Object
Returns the value of attribute attempts_used.
-
#code ⇒ Object
Returns the value of attribute code.
-
#difficulty ⇒ Object
Returns the value of attribute difficulty.
-
#hints_used ⇒ Object
Returns the value of attribute hints_used.
-
#name ⇒ Object
Returns the value of attribute name.
-
#result ⇒ Object
Returns the value of attribute result.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #attempt(attempt_code) ⇒ Object
- #attempts_total ⇒ Object
- #game_data ⇒ Object
- #hint ⇒ Object
- #hints_total ⇒ Object
-
#initialize(name = 'User1', difficulty = :easy, secret = GameCore.random) ⇒ Game
constructor
A new instance of Game.
Methods included from Storage
load_storage, save_storage, sort_codebreakers
Methods included from Validation
#validate_code, #validate_difficulty, #validate_name
Constructor Details
#initialize(name = 'User1', difficulty = :easy, secret = GameCore.random) ⇒ Game
Returns a new instance of Game.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/game.rb', line 27 def initialize(name = 'User1', difficulty = :easy, secret = GameCore.random) validate_name name validate_difficulty difficulty @name = name.to_s @difficulty = difficulty.to_sym @attempts_used = 0 @hints_used = 0 @status = :go_on @result = nil @code = GameCore.new(secret) end |
Instance Attribute Details
#attempts_used ⇒ Object
Returns the value of attribute attempts_used.
25 26 27 |
# File 'lib/game.rb', line 25 def attempts_used @attempts_used end |
#code ⇒ Object
Returns the value of attribute code.
25 26 27 |
# File 'lib/game.rb', line 25 def code @code end |
#difficulty ⇒ Object
Returns the value of attribute difficulty.
25 26 27 |
# File 'lib/game.rb', line 25 def difficulty @difficulty end |
#hints_used ⇒ Object
Returns the value of attribute hints_used.
25 26 27 |
# File 'lib/game.rb', line 25 def hints_used @hints_used end |
#name ⇒ Object
Returns the value of attribute name.
25 26 27 |
# File 'lib/game.rb', line 25 def name @name end |
#result ⇒ Object
Returns the value of attribute result.
25 26 27 |
# File 'lib/game.rb', line 25 def result @result end |
#status ⇒ Object
Returns the value of attribute status.
25 26 27 |
# File 'lib/game.rb', line 25 def status @status end |
Instance Method Details
#attempt(attempt_code) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/game.rb', line 51 def attempt(attempt_code) case @status when :go_on @attempts_used += 1 answer = @code.compare(attempt_code) check_win(answer) check_lose answer when :over I18n.t('message.lose') end end |
#attempts_total ⇒ Object
43 44 45 |
# File 'lib/game.rb', line 43 def attempts_total DIFFICULTIES[difficulty][:attempts_total] end |
#game_data ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/game.rb', line 76 def game_data { name: name, difficulty: difficulty.to_s, attempts_total: attempts_total, attempts_used: attempts_used, hints_total: hints_total, hints_used: hints_used } end |
#hint ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/game.rb', line 64 def hint case @status when :go_on return I18n.t('message.no_hints') if @hints_used >= hints_total @hints_used += 1 @code.hint when :over I18n.t('message.lose') end end |
#hints_total ⇒ Object
47 48 49 |
# File 'lib/game.rb', line 47 def hints_total DIFFICULTIES[difficulty][:hints_total] end |