Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker/game.rb
Constant Summary
Constants included from GameConst
Codebreaker::GameConst::BONUS_POINTS, Codebreaker::GameConst::FIFTY_POINTS, Codebreaker::GameConst::HARD_LEVEL, Codebreaker::GameConst::MIDDLE_LEVEL, Codebreaker::GameConst::ONE_HUNDRED_POINTS, Codebreaker::GameConst::RIGHT_ANSWER, Codebreaker::GameConst::RIGHT_ANSWER_DIFF_INDEX, Codebreaker::GameConst::SIMPLE_LEVEL, Codebreaker::GameConst::TEN_POINTS, Codebreaker::GameConst::TWENTY_POINTS, Codebreaker::GameConst::WRONG_ANSWER, Codebreaker::GameConst::ZERO_POINTS
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
Instance Method Summary collapse
- #guess_valid?(input) ⇒ Boolean
- #hint ⇒ Object
-
#initialize(*config) {|@configuration| ... } ⇒ Game
constructor
A new instance of Game.
- #score ⇒ Object
- #to_guess(input) ⇒ Object
- #won? ⇒ Boolean
Constructor Details
#initialize(*config) {|@configuration| ... } ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 16 |
# File 'lib/codebreaker/game.rb', line 10 def initialize(*config) @locale = Localization.new(:game) @configuration ||= GameConfiguration.new(*config) yield @configuration if block_given? apply_configuration generate_secret_code end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def attempts @attempts end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def configuration @configuration end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def hints @hints end |
Instance Method Details
#guess_valid?(input) ⇒ Boolean
18 19 20 21 22 |
# File 'lib/codebreaker/game.rb', line 18 def guess_valid?(input) raise ['errors']['invalid_input'] unless input.is_a?(String) raise ['alerts']['invalid_input'] unless input[/\A[1-6]{4}\z/] true end |
#hint ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/codebreaker/game.rb', line 34 def hint raise ['alerts']['no_hints'] if hints.zero? @hints -= 1 return secret_code.sample if result.empty? not_guessed = result.chars.map.with_index do |item, index| secret_code[index] unless item == RIGHT_ANSWER end not_guessed.compact.sample end |
#score ⇒ Object
44 45 46 |
# File 'lib/codebreaker/game.rb', line 44 def score calculate_score end |
#to_guess(input) ⇒ Object
24 25 26 27 28 |
# File 'lib/codebreaker/game.rb', line 24 def to_guess(input) raise ['alerts']['no_attempts'] if attempts.zero? @attempts -= 1 @result = fancy_algo(input, secret_code) end |
#won? ⇒ Boolean
30 31 32 |
# File 'lib/codebreaker/game.rb', line 30 def won? result == RIGHT_ANSWER * 4 end |