Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Includes:
- FileStore, Validation
- Defined in:
- lib/codebreaker/game.rb
Constant Summary
Constants included from FileStore
FileStore::FILE_DIRECTORY, FileStore::FILE_NAME
Constants included from Validation
Instance Attribute Summary collapse
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#secret_code ⇒ Object
readonly
Returns the value of attribute secret_code.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #attempts? ⇒ Boolean
- #generate_matrix(input) ⇒ Object
- #hints? ⇒ Boolean
-
#initialize(user) ⇒ Game
constructor
A new instance of Game.
- #lose? ⇒ Boolean
- #start ⇒ Object
- #use_hint ⇒ Object
- #win?(result) ⇒ Boolean
Methods included from FileStore
#load_file, #save_file, statistics
Methods included from Validation
#validate_guess, #validate_name, #validate_state
Constructor Details
#initialize(user) ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 |
# File 'lib/codebreaker/game.rb', line 10 def initialize(user) @user = user @state = Constants::START_GAME @secret_code = SecretCodeGenerator.call end |
Instance Attribute Details
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def hints @hints end |
#secret_code ⇒ Object (readonly)
Returns the value of attribute secret_code.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def secret_code @secret_code end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def state @state end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def user @user end |
Class Method Details
.show_difficulties ⇒ Object
51 52 53 |
# File 'lib/codebreaker/game.rb', line 51 def self.show_difficulties Constants::DIFFICULTIES end |
Instance Method Details
#attempts? ⇒ Boolean
33 34 35 |
# File 'lib/codebreaker/game.rb', line 33 def attempts? (user.attempts <= Constants::DIFFICULTIES[@user.difficulty][:attempts]) && user.attempts.positive? end |
#generate_matrix(input) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/codebreaker/game.rb', line 22 def generate_matrix(input) validate_state(@state, Constants::IN_GAME) validate_guess(input) user.decrement_attemts MatrixGenerator.new(input, @secret_code).call end |
#hints? ⇒ Boolean
29 30 31 |
# File 'lib/codebreaker/game.rb', line 29 def hints? (user.hints <= Constants::DIFFICULTIES[@user.difficulty][:hints]) && user.hints.positive? end |
#lose? ⇒ Boolean
44 45 46 47 48 49 |
# File 'lib/codebreaker/game.rb', line 44 def lose? return unless user.attempts.zero? @state = Constants::LOSE true end |
#start ⇒ Object
16 17 18 19 20 |
# File 'lib/codebreaker/game.rb', line 16 def start validate_state(@state, Constants::START_GAME) @copied_code = @secret_code.dup @state = Constants::IN_GAME end |
#use_hint ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/codebreaker/game.rb', line 55 def use_hint return if @copied_code.empty? hint = @copied_code.sample @copied_code.join.sub!(hint.to_s, "") user.decrement_hints hint end |