Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Add DonaldKnuthAlgorithm unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
powersagitar committed Jan 9, 2025
1 parent 2d6d6be commit b476a8f
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mastermind.solvers;

import mastermind.core.Code;
import mastermind.core.Response;
import mastermind.core.solvers.DonaldKnuthAlgorithm;
import mastermind.core.solvers.MastermindSolver;
import mastermind.utils.Tuple2;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

class DonaldKnuthAlgorithmTest {
@Test
void testFirstGuess() {
final DonaldKnuthAlgorithm solver = new DonaldKnuthAlgorithm();
final Code firstGuess = solver.guess();
final Code expectedFirstGuess = new Code(List.of(0, 0, 1, 1));

assertEquals(expectedFirstGuess.getColors(), firstGuess.getColors());
}

@Test
void testGuesses() {
// Secret code is purple yellow blue red
final Code secretCode = new Code(List.of(5, 3, 2, 1));
final DonaldKnuthAlgorithm solver = new DonaldKnuthAlgorithm();

Tuple2<MastermindSolver.Status, Code> result = new Tuple2<>(MastermindSolver.Status.Continue, solver.guess());

while (result.first == MastermindSolver.Status.Continue) {
final Response hints = new Response(secretCode, result.second);
result = solver.guess(hints);
}

if (result.first == MastermindSolver.Status.Lose) {
fail("Donald Knuth algorithm failed to solve the secret code");
}

assertTrue(solver.getAttempts() <= 6);
}
}

0 comments on commit b476a8f

Please sign in to comment.