Skip to content

Content of file C4GSTStrategyTest.java

package de.fhdw.gaming.ipspiel23.c4.gststrategy;

import static org.junit.jupiter.api.Assertions.*;

import java.util.HashMap;
import java.util.Map;
Unused import - java.util.Map.

Since Checkstyle 3.0

Checks for unused import statements. Checkstyle uses a simple but very reliable algorithm to report on unused import statements. An import statement is considered unused if:

  • It is not referenced in the file. The algorithm does not support wild-card imports like import java.io.*;. Most IDE's provide very sophisticated checks for imports that handle wild-card imports.
  • It is a duplicate of another import. This is when a class is imported more than once.
  • The class imported is from the java.lang package. For example importing java.lang.String.
  • The class imported is from the same package.
  • Optionally: it is referenced in Javadoc comments. This check is on by default, but it is considered bad practice to introduce a compile time dependency for documentation purposes only. As an example, the import java.util.Date would be considered referenced with the Javadoc comment {@link Date}. The alternative to avoid introducing a compile time dependency would be to write the Javadoc comment as {@link java.util.Date}.

The main limitation of this check is handling the case where an imported type has the same name as a declaration, such as a member variable.

For example, in the following case the import java.awt.Component will not be flagged as unused:


import java.awt.Component;
class FooBar {
  private Object Component; // a bad practice in my opinion
  ...
}
        
import java.util.Optional; import java.util.Set; 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. */ 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 public 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 public 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 public 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!"); } } @Test public 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!"); } } @Test public 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!"); } } }