This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d6d6be
commit b476a8f
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
culminating-mastermind/app/src/test/java/mastermind/solvers/DonaldKnuthAlgorithmTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |