Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- ZERO_POINTS =
0- TEN_POINTS =
10- TWENTY_POINTS =
20- FIFTY_POINTS =
50- ONE_HUNDRED_POINTS =
100- BONUS_POINTS =
500- RIGHT_ANSWER =
'+'.freeze
- RIGHT_ANSWER_DIFF_INDEX =
'-'.freeze
- WRONG_ANSWER =
' '.freeze
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.
17 18 19 20 21 22 23 |
# File 'lib/codebreaker/game.rb', line 17 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.
15 16 17 |
# File 'lib/codebreaker/game.rb', line 15 def attempts @attempts end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
15 16 17 |
# File 'lib/codebreaker/game.rb', line 15 def configuration @configuration end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
15 16 17 |
# File 'lib/codebreaker/game.rb', line 15 def hints @hints end |
Instance Method Details
#guess_valid?(input) ⇒ Boolean
25 26 27 28 29 |
# File 'lib/codebreaker/game.rb', line 25 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
41 42 43 44 45 46 47 48 49 |
# File 'lib/codebreaker/game.rb', line 41 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
51 52 53 |
# File 'lib/codebreaker/game.rb', line 51 def score calculate_score end |
#to_guess(input) ⇒ Object
31 32 33 34 35 |
# File 'lib/codebreaker/game.rb', line 31 def to_guess(input) raise ['alerts']['no_attempts'] if attempts.zero? @attempts -= 1 @result = fancy_algo(input, secret_code) end |
#won? ⇒ Boolean
37 38 39 |
# File 'lib/codebreaker/game.rb', line 37 def won? result == RIGHT_ANSWER * 4 end |