Class: Mastermind::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/mastermind/game.rb,
lib/mastermind/game/code.rb,
lib/mastermind/game/turn.rb,
lib/mastermind/game/piece.rb

Defined Under Namespace

Classes: Code, Piece, Turn

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#codebreakerObject (readonly)

Returns the value of attribute codebreaker.



3
4
5
# File 'lib/mastermind/game.rb', line 3

def codebreaker
  @codebreaker
end

#codemakerObject (readonly)

Returns the value of attribute codemaker.



3
4
5
# File 'lib/mastermind/game.rb', line 3

def codemaker
  @codemaker
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



3
4
5
# File 'lib/mastermind/game.rb', line 3

def max_attempts
  @max_attempts
end

#turnsObject (readonly)

Returns the value of attribute turns.



3
4
5
# File 'lib/mastermind/game.rb', line 3

def turns
  @turns
end

Instance Method Details

#attemptsObject



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

Returns:

  • (Boolean)


29
30
31
# File 'lib/mastermind/game.rb', line 29

def over?
  max_attempts_reached? || code_guessed?
end

#secret_lengthObject



17
18
19
# File 'lib/mastermind/game.rb', line 17

def secret_length
  @secret.length
end

#winnerObject



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