Class DonaldKnuthAlgorithm
-
Constructor Summary
ConstructorDescriptionConstructs a new instance of the `DonaldKnuthAlgorithm` class. -
Method Summary
Methods inherited from class mastermind.core.solvers.MastermindSolver
getAttempts, hasExceededMaxGuesses, isInitialGuess
-
Constructor Details
-
DonaldKnuthAlgorithm
public DonaldKnuthAlgorithm()Constructs a new instance of the `DonaldKnuthAlgorithm` class.This constructor initializes the algorithm by generating all possible permutations of the secret code, as required by Donald Knuth's Mastermind solving algorithm.
-
-
Method Details
-
guess
Determines the initial guess for the Mastermind game.- Specified by:
guess
in classMastermindAlgorithm
- Returns:
- The initial guess to be made by the algorithm.
- Throws:
IllegalCallerException
- If this method is invoked for subsequent guesses.
-
guess
Produces the next guess in the Mastermind game based on the feedback from the previous guess.This method is only intended for subsequent guesses after the initial guess. It evaluates the feedback provided in the form of a
Response
object, determines the current game status, and updates the state of the algorithm.The algorithm executes the following steps:
1. Checks if the method is being called correctly for subsequent guesses. Throws anIllegalCallerException
if called for the initial guess.
2. Retrieves the validation counts (e.g., correct positions) from the response.
3. If the number of correct positions equals the secret code length (Mastermind.CODE_LENGTH
), declares the player as a winner by returning aTuple2
containing statusStatus.Win
and the last guess.
4. If the maximum allowable guesses are exceeded and no match is found, declares the player as a loser by returning aTuple2
containing statusStatus.Lose
and the last guess.
5. Filters out invalid permutations that do not align with the response feedback using thereducePermutations
method.
6. Finds the next optimized guess using thefindNextGuess
method.
7. Updates the last guess with the next guess and returns aTuple2
containing statusStatus.Continue
and the next guess.- Specified by:
guess
in classMastermindAlgorithm
- Parameters:
response
- The feedback received for the previous guess, containing the number of correct positions and other match details.- Returns:
- A
Tuple2<Status, Code>
object whereStatus
represents the current game status (e.g., Win, Lose, Continue), andCode
represents the next guess to be made. - Throws:
IllegalCallerException
- If this method is invoked for the initial guess.InvalidHintsException
- If the algorithm encounters an invalid hint during the process.
-