Content of file C4GSTStrategyTest.java
package de.fhdw.gaming.ipspiel23.c4.gststrategy;
import static org.junit.jupiter.api.Assertions.*;
Since Checkstyle 3.0
Checks that there are no import statements that use the * notation.
Rationale: Importing all classes from a package or static members from a class leads to tight coupling between packages or classes and might lead to problems when a new version of a library introduces name clashes.
import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import de.fhdw.gaming.core.domain.GameException; import de.fhdw.gaming.ipspiel23.c4.domain.IC4Game; import de.fhdw.gaming.ipspiel23.c4.domain.IC4GameBuilder; import de.fhdw.gaming.ipspiel23.c4.domain.IC4Player; import de.fhdw.gaming.ipspiel23.c4.domain.IC4PlayerBuilder; import de.fhdw.gaming.ipspiel23.c4.domain.IC4State; import de.fhdw.gaming.ipspiel23.c4.domain.impl.C4GameBuilder; import de.fhdw.gaming.ipspiel23.c4.domain.impl.C4Position; import de.fhdw.gaming.ipspiel23.c4.moves.IC4Move; import de.fhdw.gaming.ipspiel23.c4.moves.factory.IC4MoveFactory; import de.fhdw.gaming.ipspiel23.c4.moves.impl.C4DefaultMoveFactory; import de.fhdw.gaming.ipspiel23.c4.strategies.IC4Strategy; import de.fhdw.gaming.ipspiel23.c4.strategies.IC4StrategyFactory; /** * Tests the C4GSTStrategy. */ @SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.UnusedPrivateMethod"}) class C4GSTStrategyTest { /** * Game for tests. */ private IC4Game game; /** * MoveFactory for tests. */ private IC4MoveFactory moveFactory; /** * First player for tests. */ private IC4Player playerA; /** * Second player for tests. */ private IC4Player playerB; /** * Players strategy. */ private IC4Strategy strategy; /** * Creates an empty game. * * @throws GameException * @throws InterruptedException */ @BeforeEach void gameSetup() throws GameException, InterruptedException { final IC4GameBuilder gb = new C4GameBuilder(); gb.changeBoardColumns(7); gb.changeBoardRows(6); final IC4PlayerBuilder pbA = gb.createPlayerBuilder(); final IC4PlayerBuilder pbB = gb.createPlayerBuilder(); pbA.changeName("A"); this.playerA = pbA.build(); pbB.changeName("B"); this.playerB = pbB.build(); this.moveFactory = new C4DefaultMoveFactory(); final IC4StrategyFactory strategyFactory = new C4GSTMoveStrategyFactory(); this.strategy = strategyFactory.create(moveFactory); gb.addPlayer(playerA, strategy); gb.addPlayer(playerB, strategy); this.game = gb.build(0); } /** * This Test ensures the correct name is given by toString. */ @Test void testStrategyName() { final String expectedName = "C4GSTMoveStrategy"; assertEquals(expectedName, strategy.toString()); } /** * This test ensures, that a move that instantly wins will be taken. * * @throws GameException * @throws InterruptedException */ @Test void testWin1() throws GameException, InterruptedException { final IC4State copied = game.getState(); this.moveFactory.createMove(playerA, new C4Position(5, 0)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(5, 6)).applyTo(copied, playerB); copied.nextTurn(); this.moveFactory.createMove(playerA, new C4Position(4, 0)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(4, 6)).applyTo(copied, playerB); copied.nextTurn(); this.moveFactory.createMove(playerA, new C4Position(3, 0)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(3, 6)).applyTo(copied, playerB); copied.nextTurn(); /* * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 1 0 0 0 0 0 2 * 1 0 0 0 0 0 2 * 1 0 0 0 0 0 2 * * * Player A can win with (2, 0) * * * */ final IC4Move expectedMove = moveFactory.createMove(playerA, new C4Position(2, 0)); final Optional<IC4Move> foundMoveOptional = strategy.computeNextMove(0, playerA, copied); if (foundMoveOptional.isPresent()) { assertEquals(expectedMove, foundMoveOptional.get()); } else { fail("no move found!"); } } /** * This Test ensures, that Player B will prevent a Player A win, by blocking the winning move. * * @throws GameException * @throws InterruptedException */ @Test void testPlayerBLosePrevention() throws GameException, InterruptedException { final IC4State copied = game.getState(); this.moveFactory.createMove(playerA, new C4Position(5, 0)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(5, 6)).applyTo(copied, playerB); copied.nextTurn(); this.moveFactory.createMove(playerA, new C4Position(4, 0)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(4, 6)).applyTo(copied, playerB); copied.nextTurn(); this.moveFactory.createMove(playerA, new C4Position(3, 0)).applyTo(copied, playerA); copied.nextTurn(); /* * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 1 0 0 0 0 0 0 * 1 0 0 0 0 0 2 * 1 0 0 0 0 0 2 * * * Player A can win with (2, 0), Player B must prevent this! * * * */ final IC4Move expectedMove = moveFactory.createMove(playerB, new C4Position(2, 0)); final Optional<IC4Move> foundMoveOptional = strategy.computeNextMove(0, playerB, copied); if (foundMoveOptional.isPresent()) { assertEquals(expectedMove, foundMoveOptional.get()); } else { fail("no move found!"); } } /** * This Test ensures, that Player B will Win. * * @throws GameException * @throws InterruptedException */ @Test void testPlayerBWin1() throws GameException, InterruptedException { final IC4State copied = game.getState(); this.moveFactory.createMove(playerA, new C4Position(5, 0)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(5, 6)).applyTo(copied, playerB); copied.nextTurn(); this.moveFactory.createMove(playerA, new C4Position(5, 1)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(4, 6)).applyTo(copied, playerB); copied.nextTurn(); this.moveFactory.createMove(playerA, new C4Position(4, 0)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(3, 6)).applyTo(copied, playerB); copied.nextTurn(); this.moveFactory.createMove(playerA, new C4Position(3, 0)).applyTo(copied, playerA); copied.nextTurn(); /* * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 1 0 0 0 0 0 2 * 1 0 0 0 0 0 2 * 1 1 0 0 0 0 2 * * * Player B can win with (2, 6). * * * */ final IC4Move expectedMove = moveFactory.createMove(playerB, new C4Position(2, 6)); final Optional<IC4Move> foundMoveOptional = strategy.computeNextMove(0, playerB, copied); if (foundMoveOptional.isPresent()) { assertEquals(expectedMove, foundMoveOptional.get()); } else { fail("no move found!"); } } /** * This Test ensures, that Player A will make a lose prevention Move. * * @throws GameException * @throws InterruptedException */ @Test void testPlayerALosePrevention() throws GameException, InterruptedException { final IC4State copied = game.getState(); this.moveFactory.createMove(playerA, new C4Position(5, 0)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(5, 6)).applyTo(copied, playerB); copied.nextTurn(); this.moveFactory.createMove(playerA, new C4Position(5, 1)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(4, 6)).applyTo(copied, playerB); copied.nextTurn(); this.moveFactory.createMove(playerA, new C4Position(4, 0)).applyTo(copied, playerA); copied.nextTurn(); this.moveFactory.createMove(playerB, new C4Position(3, 6)).applyTo(copied, playerB); copied.nextTurn(); /* * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 1 0 0 0 0 0 2 * 1 0 0 0 0 0 2 * 1 1 0 0 0 0 2 * * * Player B can win with (2, 6), A must prevent this! * * * */ final IC4Move expectedMove = moveFactory.createMove(playerA, new C4Position(2, 6)); final Optional<IC4Move> foundMoveOptional = strategy.computeNextMove(0, playerA, copied); if (foundMoveOptional.isPresent()) { assertEquals(expectedMove, foundMoveOptional.get()); } else { fail("no move found!"); } } }