Class: Codebreaker::Game
- Inherits:
-
Object
- Object
- Codebreaker::Game
- Defined in:
- lib/ep-codebreaker/game.rb
Instance Attribute Summary collapse
-
#hints_left ⇒ Object
readonly
Returns the value of attribute hints_left.
-
#tries_left ⇒ Object
readonly
Returns the value of attribute tries_left.
Instance Method Summary collapse
- #answer ⇒ Object
- #as_json ⇒ Object
- #check_guess(input) ⇒ Object
- #finished? ⇒ Boolean
- #high_scores ⇒ Object
- #hint ⇒ Object
-
#initialize(writer = GameWriter.new, player_class = Player) ⇒ Game
constructor
A new instance of Game.
- #save_score(name) ⇒ Object
- #start ⇒ Object
- #to_json(*options) ⇒ Object
- #won? ⇒ Boolean
Constructor Details
#initialize(writer = GameWriter.new, player_class = Player) ⇒ Game
Returns a new instance of Game.
19 20 21 22 |
# File 'lib/ep-codebreaker/game.rb', line 19 def initialize(writer = GameWriter.new, player_class = Player) @writer = writer @player_class = player_class end |
Instance Attribute Details
#hints_left ⇒ Object (readonly)
Returns the value of attribute hints_left.
15 16 17 |
# File 'lib/ep-codebreaker/game.rb', line 15 def hints_left @hints_left end |
#tries_left ⇒ Object (readonly)
Returns the value of attribute tries_left.
15 16 17 |
# File 'lib/ep-codebreaker/game.rb', line 15 def tries_left @tries_left end |
Instance Method Details
#answer ⇒ Object
53 54 55 |
# File 'lib/ep-codebreaker/game.rb', line 53 def answer @code if finished? end |
#as_json ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ep-codebreaker/game.rb', line 65 def as_json { result: nil, hint: nil, tries_left: @tries_left, hints_left: @hints_left, finished: finished?, won: won?, answer: answer } end |
#check_guess(input) ⇒ Object
32 33 34 35 36 |
# File 'lib/ep-codebreaker/game.rb', line 32 def check_guess(input) verify input @tries_left -= 1 @result = check_input(@code.chars, input.chars) end |
#finished? ⇒ Boolean
45 46 47 |
# File 'lib/ep-codebreaker/game.rb', line 45 def finished? @tries_left.zero? || won? end |
#high_scores ⇒ Object
61 62 63 |
# File 'lib/ep-codebreaker/game.rb', line 61 def high_scores @writer.load_scores end |
#hint ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ep-codebreaker/game.rb', line 38 def hint return false if @hints_left.zero? @tries_left -= 1 @hints_left -= 1 generate_hint end |
#save_score(name) ⇒ Object
57 58 59 |
# File 'lib/ep-codebreaker/game.rb', line 57 def save_score(name) @writer.write @player_class.new(name, @tries_left, @hints_left) end |
#start ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/ep-codebreaker/game.rb', line 24 def start @code = Array.new(LENGTH) { rand(MIN..MAX) }.join @result = '' @tries_left = TRIES @hints_left = HINTS @indexes_for_hint = (0...LENGTH).to_a end |
#to_json(*options) ⇒ Object
77 78 79 |
# File 'lib/ep-codebreaker/game.rb', line 77 def to_json(*) as_json.to_json(*) end |
#won? ⇒ Boolean
49 50 51 |
# File 'lib/ep-codebreaker/game.rb', line 49 def won? @result == ('+' * LENGTH) end |