Class MediumAlgorithm


public class MediumAlgorithm extends MastermindAlgorithm
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 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

      public Code guess()
      Determines the initial guess for the Mastermind game.
      Specified by:
      guess in class MastermindAlgorithm
      Returns:
      The initial guess to be made by the algorithm.
    • guess

      public Tuple2<Status,Code> guess(Response response)
      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 an IllegalCallerException 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 the reducePermutations method. 5. Finds the next guess using the findNextGuess method. 6. Updates the last guess with the next guess and returns a Tuple2 containing status Status.Continue and the next guess.
      Specified by:
      guess in class MastermindAlgorithm
      Parameters:
      response - The feedback from the last guess.
      Returns:
      A Tuple2<Status, Code> object where Status represents the current game status (e.g., Win, Lose, Continue), and Code represents the next guess to be made.
      Throws:
      IllegalCallerException - If this method is invoked for the initial guess.