Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/codebreaker.rb
Constant Summary collapse
- STATE =
{ continue: 'continue', win: 'win', lose: 'lose' }.freeze
- VALID_NUMBERS =
(1..6).freeze
- LENGTH_CODE =
4
- CORRECT_SYMBOL =
'+'
- INCORRECT_SYMBOL =
'-'
Instance Attribute Summary collapse
-
#attempts ⇒ Object
readonly
Returns the value of attribute attempts.
-
#difficulties ⇒ Object
readonly
Returns the value of attribute difficulties.
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#player ⇒ Object
readonly
Returns the value of attribute player.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #check_code(code) ⇒ Object
- #decrease_attempts! ⇒ Object
- #hint ⇒ Object
- #hints? ⇒ Boolean
-
#initialize(player, difficulties) ⇒ Game
constructor
A new instance of Game.
- #lose? ⇒ Boolean
- #to_h ⇒ Object
- #win?(code) ⇒ Boolean
Constructor Details
#initialize(player, difficulties) ⇒ Game
Returns a new instance of Game.
18 19 20 21 22 23 24 25 26 |
# File 'lib/codebreaker.rb', line 18 def initialize(player, difficulties) @player = player @difficulties = difficulties @attempts = @difficulties.attempts @hints = @difficulties.hints @number = generator_secret_code @available_hints = @number.clone @state = STATE[:continue] end |
Instance Attribute Details
#attempts ⇒ Object (readonly)
Returns the value of attribute attempts.
17 18 19 |
# File 'lib/codebreaker.rb', line 17 def attempts @attempts end |
#difficulties ⇒ Object (readonly)
Returns the value of attribute difficulties.
17 18 19 |
# File 'lib/codebreaker.rb', line 17 def difficulties @difficulties end |
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
17 18 19 |
# File 'lib/codebreaker.rb', line 17 def hints @hints end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
17 18 19 |
# File 'lib/codebreaker.rb', line 17 def number @number end |
#player ⇒ Object (readonly)
Returns the value of attribute player.
17 18 19 |
# File 'lib/codebreaker.rb', line 17 def player @player end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
17 18 19 |
# File 'lib/codebreaker.rb', line 17 def state @state end |
Instance Method Details
#check_code(code) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/codebreaker.rb', line 33 def check_code(code) return nil unless valid_code?(code) temp_code = code.chars guess_number = Array.new(LENGTH_CODE) { 0 } calc_and_mark_correct(temp_code, guess_number) + calc_and_mark_incorrect(temp_code, guess_number) end |
#decrease_attempts! ⇒ Object
71 72 73 |
# File 'lib/codebreaker.rb', line 71 def decrease_attempts! @attempts -= 1 end |
#hint ⇒ Object
28 29 30 31 |
# File 'lib/codebreaker.rb', line 28 def hint @hints -= 1 @available_hints.delete_at(rand(@available_hints.size)) end |
#hints? ⇒ Boolean
60 61 62 |
# File 'lib/codebreaker.rb', line 60 def hints? !@hints.zero? end |
#lose? ⇒ Boolean
64 65 66 67 68 69 |
# File 'lib/codebreaker.rb', line 64 def lose? return false unless @attempts.zero? @state = STATE[:lose] true end |
#to_h ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/codebreaker.rb', line 41 def to_h { player: @player.name, difficulties: @difficulties.level, attempts_total: @difficulties.attempts, hints_total: @difficulties.hints, attempts_used: @difficulties.attempts - @attempts, hints_used: @difficulties.hints - @hints, state: @state } end |
#win?(code) ⇒ Boolean
53 54 55 56 57 58 |
# File 'lib/codebreaker.rb', line 53 def win?(code) return false unless code == @number.join @state = STATE[:win] true end |