Class: CodebreakerSmn::Game
- Inherits:
-
Object
- Object
- CodebreakerSmn::Game
- Includes:
- ValidationHelper
- Defined in:
- lib/codebreaker_smn.rb
Constant Summary collapse
- DIFFICULTIES =
{ hell: { attempts: 5, hints: 1 }, medium: { attempts: 10, hints: 1 }, easy: { attempts: 15, hints: 2 } }.freeze
- CODE_RULES =
{ size: 4, digits: 1..6 }.freeze
- USERNAME_RULES =
{ min_length: 3, max_length: 20 }
- WIN_RESULT =
'++++'.freeze
Instance Attribute Summary collapse
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#difficulty ⇒ Object
Returns the value of attribute difficulty.
-
#hints ⇒ Object
Returns the value of attribute hints.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #get_hint ⇒ Object
- #guess_code(input) ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #new_game ⇒ Object
- #start ⇒ Object
- #statistics ⇒ Object
Methods included from ValidationHelper
#not_empty_string, #positive_integer, #positive_integers, #valid_digit, #valid_digits, #valid_length
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
28 29 30 |
# File 'lib/codebreaker_smn.rb', line 28 def initialize new_game end |
Instance Attribute Details
#attempts ⇒ Object
Returns the value of attribute attempts.
26 27 28 |
# File 'lib/codebreaker_smn.rb', line 26 def attempts @attempts end |
#code ⇒ Object
Returns the value of attribute code.
25 26 27 |
# File 'lib/codebreaker_smn.rb', line 25 def code @code end |
#difficulty ⇒ Object
Returns the value of attribute difficulty.
25 26 27 |
# File 'lib/codebreaker_smn.rb', line 25 def difficulty @difficulty end |
#hints ⇒ Object
Returns the value of attribute hints.
26 27 28 |
# File 'lib/codebreaker_smn.rb', line 26 def hints @hints end |
#state ⇒ Object
Returns the value of attribute state.
25 26 27 |
# File 'lib/codebreaker_smn.rb', line 25 def state @state end |
#username ⇒ Object
Returns the value of attribute username.
25 26 27 |
# File 'lib/codebreaker_smn.rb', line 25 def username @username end |
Instance Method Details
#get_hint ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/codebreaker_smn.rb', line 62 def get_hint result = hint_code if @hints.positive? && @state == :started @hints -= 1 result.pop else 'No hints left!' end end |
#guess_code(input) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/codebreaker_smn.rb', line 53 def guess_code(input) return unless valid_guess?(input) CodeHandler.process_guess(@code, input) do |result| select_winner(result) take_attempt end end |
#new_game ⇒ Object
32 33 34 35 |
# File 'lib/codebreaker_smn.rb', line 32 def new_game reset_params @state = :new end |
#start ⇒ Object
37 38 39 40 |
# File 'lib/codebreaker_smn.rb', line 37 def start generate_code @state = :started end |
#statistics ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/codebreaker_smn.rb', line 42 def statistics attempts_total = DIFFICULTIES[@difficulty][:attempts] attempts_used = attempts_total - @attempts hints_total = DIFFICULTIES[@difficulty][:hints] hints_used = hints_total - @hints { name: @username, difficulty: @difficulty.to_s, attempts_total: attempts_total, attempts_used: attempts_used, hints_total: hints_total, hints_used: hints_used, date: Date.today } end |