Class DonaldKnuthAlgorithm
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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:
guessin 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
Responseobject, 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 anIllegalCallerExceptionif 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 aTuple2containing statusStatus.Winand the last guess.
4. If the maximum allowable guesses are exceeded and no match is found, declares the player as a loser by returning aTuple2containing statusStatus.Loseand the last guess.
5. Filters out invalid permutations that do not align with the response feedback using thereducePermutationsmethod.
6. Finds the next optimized guess using thefindNextGuessmethod.
7. Updates the last guess with the next guess and returns aTuple2containing statusStatus.Continueand the next guess.- Specified by:
guessin 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 whereStatusrepresents the current game status (e.g., Win, Lose, Continue), andCoderepresents 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.
-