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;
Reports import statements that can be removed. They are either unused,
duplicated, or the members they import are already implicitly in scope,
because they're in java.lang, or the current package.
import java.io.File; // not used, can be removed
import java.util.Collections; // used below
import java.util.*; // so this one is not used
import java.lang.Object; // imports from java.lang, unnecessary
import java.lang.Object; // duplicate, unnecessary
public class Foo {
static Object emptyList() {
return Collections.emptyList();
}
}
See PMD documentation.
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!");
}
}
}