Package mastermind.core.solvers
Class MediumAlgorithm
java.lang.Object
mastermind.core.solvers.MastermindSolver
mastermind.core.solvers.MastermindAlgorithm
mastermind.core.solvers.MediumAlgorithm
An implementation of the medium algorithm, which is a modified and
less-optimal version of the Donald-Knuth algorithm, for solving the
Mastermind game.
It will first guess 1 color for all 4 positions for the first 3 attempts.
It will reduce the permutations after every guess and response. Then, it
will guess the first permutation in the set of remaining permutations.
-
Constructor Summary
-
Method Summary
Methods inherited from class mastermind.core.solvers.MastermindSolver
getAttempts, hasExceededMaxGuesses, isInitialGuess
-
Constructor Details
-
MediumAlgorithm
public MediumAlgorithm()Constructs a new instance of the `MediumAlgorithm` 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.
-
guess
Determines the subsequent guesses for the Mastermind game. 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, the game status is set to Win, and the previous guess is returned. If the maximum number of guesses has been exceeded, the game status is set to Lose, and the previous guess is returned. Otherwise, the algorithm proceeds to the next step. 4. Filters out invalid permutations that do not align with the response feedback using thereducePermutations
method. 5. Finds the next guess using thefindNextGuess
method. 6. 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 from the last guess.- 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.
-