Class: Mastermind::Game
- Inherits:
-
Object
- Object
- Mastermind::Game
- Defined in:
- lib/mastermind/game.rb,
lib/mastermind/game/code.rb,
lib/mastermind/game/turn.rb,
lib/mastermind/game/piece.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#codebreaker ⇒ Object
readonly
Returns the value of attribute codebreaker.
-
#codemaker ⇒ Object
readonly
Returns the value of attribute codemaker.
-
#max_attempts ⇒ Object
readonly
Returns the value of attribute max_attempts.
-
#turns ⇒ Object
readonly
Returns the value of attribute turns.
Instance Method Summary collapse
- #attempts ⇒ Object
- #guess(code) ⇒ Object
-
#initialize(secret: nil, codemaker: nil, codebreaker: nil) ⇒ Game
constructor
A new instance of Game.
- #over? ⇒ Boolean
- #secret_length ⇒ Object
- #winner ⇒ Object
Constructor Details
#initialize(secret: nil, codemaker: nil, codebreaker: nil) ⇒ Game
Returns a new instance of Game.
5 6 7 8 9 10 11 |
# File 'lib/mastermind/game.rb', line 5 def initialize(secret: nil, codemaker: nil, codebreaker: nil) @secret = secret || Code.random @turns = [] @codemaker = codemaker || Player.new(name: "AbstractCodemaker") @codebreaker = codebreaker || Player.new(name: "AbstractCodebreaker") @max_attempts = 12 end |
Instance Attribute Details
#codebreaker ⇒ Object (readonly)
Returns the value of attribute codebreaker.
3 4 5 |
# File 'lib/mastermind/game.rb', line 3 def codebreaker @codebreaker end |
#codemaker ⇒ Object (readonly)
Returns the value of attribute codemaker.
3 4 5 |
# File 'lib/mastermind/game.rb', line 3 def codemaker @codemaker end |
#max_attempts ⇒ Object (readonly)
Returns the value of attribute max_attempts.
3 4 5 |
# File 'lib/mastermind/game.rb', line 3 def max_attempts @max_attempts end |
#turns ⇒ Object (readonly)
Returns the value of attribute turns.
3 4 5 |
# File 'lib/mastermind/game.rb', line 3 def turns @turns end |
Instance Method Details
#attempts ⇒ Object
13 14 15 |
# File 'lib/mastermind/game.rb', line 13 def attempts turns.length end |
#guess(code) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/mastermind/game.rb', line 21 def guess(code) @turns << Turn.new( guess: code, number: attempts + 1, exact: @secret.exact_matches_with(code), partial: @secret.partial_matches_with(code) ) end |
#over? ⇒ Boolean
29 30 31 |
# File 'lib/mastermind/game.rb', line 29 def over? max_attempts_reached? || code_guessed? end |
#secret_length ⇒ Object
17 18 19 |
# File 'lib/mastermind/game.rb', line 17 def secret_length @secret.length end |
#winner ⇒ Object
33 34 35 36 |
# File 'lib/mastermind/game.rb', line 33 def winner return codebreaker if code_guessed? return codemaker if max_attempts_reached? end |