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;
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!");
        }
 
    }
JUnit 5 tests should be package-private.
Reports JUnit 5 test classes and methods that are not package-private. Contrary to JUnit 4 tests, which required public visibility to be run by the engine, JUnit 5 tests can also be run if they're package-private. Marking them as such is a good practice to limit their visibility. Test methods are identified as those which use `@Test`, `@RepeatedTest`, `@TestFactory`, `@TestTemplate` or `@ParameterizedTest`.
    
        
            
class MyTest { // not public, that's fine
    @Test
    public void testBad() { } // should not have a public modifier

    @Test
    protected void testAlsoBad() { } // should not have a protected modifier

    @Test
    private void testNoRun() { } // should not have a private modifier

    @Test
    void testGood() { } // package private as expected
}

        
    
See PMD documentation.
@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!"); } } }