class Mastermind::Game
Attributes
codebreaker[R]
codemaker[R]
max_attempts[R]
turns[R]
Public Class Methods
new(secret: nil, codemaker: nil, codebreaker: nil)
click to toggle source
# 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
Public Instance Methods
attempts()
click to toggle source
# File lib/mastermind/game.rb, line 13 def attempts turns.length end
guess(code)
click to toggle source
# 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?()
click to toggle source
# File lib/mastermind/game.rb, line 29 def over? max_attempts_reached? || code_guessed? end
secret_length()
click to toggle source
# File lib/mastermind/game.rb, line 17 def secret_length @secret.length end
winner()
click to toggle source
# File lib/mastermind/game.rb, line 33 def winner return codebreaker if code_guessed? return codemaker if max_attempts_reached? end
Private Instance Methods
code_guessed?()
click to toggle source
# File lib/mastermind/game.rb, line 45 def code_guessed? turns.first(max_attempts).any? { |turn| turn.guess == @secret } end
max_attempts_reached?()
click to toggle source
# File lib/mastermind/game.rb, line 41 def max_attempts_reached? attempts >= max_attempts end