Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Includes:
- Validation
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- DIFFICULTIES =
{ easy: { attempts: 15, hints: 2 }, medium: { attempts: 10, hints: 1 }, hell: { attempts: 5, hints: 1 } }.freeze
- CODE_LENGTH =
4- RANGE_GUESS_CODE =
(1..6).freeze
Constants included from Validation
Instance Attribute Summary collapse
-
#attempts_used ⇒ Object
readonly
Returns the value of attribute attempts_used.
-
#clues ⇒ Object
readonly
Returns the value of attribute clues.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#hints_used ⇒ Object
readonly
Returns the value of attribute hints_used.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#very_secret_code ⇒ Object
readonly
Returns the value of attribute very_secret_code.
Instance Method Summary collapse
- #attempts ⇒ Object
- #guess(user_guess) ⇒ Object
-
#initialize(difficulty:, user:, date: Date.today) ⇒ Game
constructor
A new instance of Game.
- #lost? ⇒ Boolean
- #number_of_hints ⇒ Object
- #save_game ⇒ Object
- #show_hint ⇒ Object
- #start_new_game ⇒ Object
- #won? ⇒ Boolean
Methods included from Validation
#raise_error, #validate_difficulty, #validate_file_existens, #validate_guess, #validate_hints, #validate_user_name
Constructor Details
#initialize(difficulty:, user:, date: Date.today) ⇒ Game
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/codebreaker/game.rb', line 20 def initialize(difficulty:, user:, date: Date.today) validate_difficulty(difficulty, DIFFICULTIES) @user = user @difficulty = difficulty @date = date attempts number_of_hints end |
Instance Attribute Details
#attempts_used ⇒ Object (readonly)
Returns the value of attribute attempts_used.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def attempts_used @attempts_used end |
#clues ⇒ Object (readonly)
Returns the value of attribute clues.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def clues @clues end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def date @date end |
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def difficulty @difficulty end |
#hints_used ⇒ Object (readonly)
Returns the value of attribute hints_used.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def hints_used @hints_used end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def user @user end |
#very_secret_code ⇒ Object (readonly)
Returns the value of attribute very_secret_code.
7 8 9 |
# File 'lib/codebreaker/game.rb', line 7 def very_secret_code @very_secret_code end |
Instance Method Details
#attempts ⇒ Object
67 68 69 |
# File 'lib/codebreaker/game.rb', line 67 def attempts @attempts ||= DIFFICULTIES[difficulty.to_sym][:attempts] end |
#guess(user_guess) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/codebreaker/game.rb', line 40 def guess(user_guess) guess = user_guess.each_char.map(&:to_i) validate_guess(guess, CODE_LENGTH, RANGE_GUESS_CODE) check_guess(guess, very_secret_code) increase_attempts end |
#lost? ⇒ Boolean
59 60 61 |
# File 'lib/codebreaker/game.rb', line 59 def lost? @attempts_used >= @attempts end |
#number_of_hints ⇒ Object
71 72 73 |
# File 'lib/codebreaker/game.rb', line 71 def number_of_hints @number_of_hints ||= DIFFICULTIES[difficulty.to_sym][:hints] end |
#save_game ⇒ Object
63 64 65 |
# File 'lib/codebreaker/game.rb', line 63 def save_game SaveRes.new.save(self) end |
#show_hint ⇒ Object
48 49 50 51 52 53 |
# File 'lib/codebreaker/game.rb', line 48 def show_hint validate_hints(hints_used, number_of_hints) @hints_used += 1 @hints.shuffle!.pop end |
#start_new_game ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/codebreaker/game.rb', line 31 def start_new_game @very_secret_code = generate_random_code @hints = @very_secret_code.clone @attempts_used = 0 @hints_used = 0 @user_guess = [] @clues = [] end |
#won? ⇒ Boolean
55 56 57 |
# File 'lib/codebreaker/game.rb', line 55 def won? @user_guess.nil? end |