Skip to content

Content of file GstTest.java

package de.fhdw.gaming.ipspiel23.gst.impl;

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

import org.junit.jupiter.api.Test;

import de.fhdw.gaming.core.domain.Move;
import de.fhdw.gaming.ipspiel23.gst.domain.TttMove;
import de.fhdw.gaming.ipspiel23.gst.domain.impl.GstKopplungsMoveCalculator;
import de.fhdw.gaming.ipspiel23.gst.strategies.impl.GstKopplungNegaMax;
import de.fhdw.gaming.ipspiel23.gst.strategies.impl.GstKopplungNegaMaxMultithreading;

/**
 * .
 *
 * @author Dave
 *
 */
class GstTest {

    /**
     * .
     */
    private final String PLAYER_A = "PlayerA";
This final field could be made static.
If a final field is assigned to a compile-time constant, it could be made static, thus saving overhead in each object at runtime.
    
        

public class Foo {
  public final int BAR = 42; // this could be static and save some space
}

        
    
See PMD documentation.
/** * . */ private final String PLAYER_B = "PlayerB"; /** * . */ private GstKopplungsMoveCalculator<TttPlayer, TttState> calc; /** * . */ private TttKopplung kopplung; /** * . */ private TttPlayer playerA; /** * . */ private TttPlayer playerB; /** * . */ @Test void currentPlayerWinsMoveTest() { /* * 00 = X, 20 = X, 11 = O, 22= 0 * * X to move, 01 is a winning move. * */ this.calc = new GstKopplungsMoveCalculator<>(); this.playerA = new TttPlayer(PLAYER_A, 1); this.playerB = new TttPlayer(PLAYER_B, -1); this.kopplung = new TttKopplung(); final TttState state = new TttState(this.playerA, this.playerB); state.getBoard()[0][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[2][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[1][1] = this.playerB.getPlayerSymbolValue(); state.getBoard()[2][2] = this.playerB.getPlayerSymbolValue(); final Move<TttPlayer, TttState> move = this.calc.calculateMove(this.kopplung, state, 500, new GstKopplungNegaMax<>()); final TttMove tttMove = (TttMove) move; final TttMove bestMove = new TttPlaceMarkMove(0, 1); assertEquals(bestMove, tttMove); } /** * . */ @Test void currentPlayerWinsBeforeNextPlayerTest() { /* * /* * 00 = X, 20 = X, 02 = O, 22= 0 * * X to move, 01 is a winning move. * O could win with 12, but because X is to move, X needs to win. * */ this.calc = new GstKopplungsMoveCalculator<>(); this.playerA = new TttPlayer(PLAYER_A, 1); this.playerB = new TttPlayer(PLAYER_B, -1); this.kopplung = new TttKopplung(); final TttState state = new TttState(this.playerA, this.playerB); state.getBoard()[0][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[2][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[0][2] = this.playerB.getPlayerSymbolValue(); state.getBoard()[2][2] = this.playerB.getPlayerSymbolValue(); final Move<TttPlayer, TttState> move = this.calc.calculateMove(this.kopplung, state, 500, new GstKopplungNegaMax<>()); final TttMove tttMove = (TttMove) move; final TttMove bestMove = new TttPlaceMarkMove(1, 0); assertEquals(bestMove, tttMove); } /** * . */ @Test void currentPlayerPreventsOtherPlayersWinTest() { /* * 00 = X, 11 = X, 02 = O, 22= 0 * * X to move, 12 is a prevention move. * O could win with 12, but because X is to move, X needs to prevent that win. * */ this.calc = new GstKopplungsMoveCalculator<>(); this.playerA = new TttPlayer(PLAYER_A, 1); this.playerB = new TttPlayer(PLAYER_B, -1); this.kopplung = new TttKopplung(); final TttState state = new TttState(this.playerA, this.playerB); state.getBoard()[0][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[1][1] = this.playerA.getPlayerSymbolValue(); state.getBoard()[0][2] = this.playerB.getPlayerSymbolValue(); state.getBoard()[2][2] = this.playerB.getPlayerSymbolValue(); final Move<TttPlayer, TttState> move = this.calc.calculateMove(this.kopplung, state, 500, new GstKopplungNegaMax<>()); final TttMove tttMove = (TttMove) move; final TttMove bestMove = new TttPlaceMarkMove(1, 2); assertEquals(bestMove, tttMove); } /** * . */ @Test void currentPlayerWinsMoveTestMT() { /* * 00 = X, 20 = X, 11 = O, 22= 0 * * X to move, 01 is a winning move. * */ this.calc = new GstKopplungsMoveCalculator<>(); this.playerA = new TttPlayer(PLAYER_A, 1); this.playerB = new TttPlayer(PLAYER_B, -1); this.kopplung = new TttKopplung(); final TttState state = new TttState(this.playerA, this.playerB); state.getBoard()[0][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[2][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[1][1] = this.playerB.getPlayerSymbolValue(); state.getBoard()[2][2] = this.playerB.getPlayerSymbolValue(); final Move<TttPlayer, TttState> move = this.calc.calculateMove(this.kopplung, state, 500, new GstKopplungNegaMaxMultithreading<>()); final TttMove tttMove = (TttMove) move; final TttMove bestMove = new TttPlaceMarkMove(0, 1); assertEquals(bestMove, tttMove); } /** * . */ @Test void currentPlayerWinsBeforeNextPlayerTestMT() { /* * /* * 00 = X, 20 = X, 02 = O, 22= 0 * * X to move, 01 is a winning move. * O could win with 12, but because X is to move, X needs to win. * */ this.calc = new GstKopplungsMoveCalculator<>(); this.playerA = new TttPlayer(PLAYER_A, 1); this.playerB = new TttPlayer(PLAYER_B, -1); this.kopplung = new TttKopplung(); final TttState state = new TttState(this.playerA, this.playerB); state.getBoard()[0][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[2][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[0][2] = this.playerB.getPlayerSymbolValue(); state.getBoard()[2][2] = this.playerB.getPlayerSymbolValue(); final Move<TttPlayer, TttState> move = this.calc.calculateMove(this.kopplung, state, 500, new GstKopplungNegaMaxMultithreading<>()); final TttMove tttMove = (TttMove) move; final TttMove bestMove = new TttPlaceMarkMove(1, 0); assertEquals(bestMove, tttMove); } /** * . */ @Test void currentPlayerPreventsOtherPlayersWinTestMT() { /* * 00 = X, 11 = X, 02 = O, 22= 0 * * X to move, 12 is a prevention move. * O could win with 12, but because X is to move, X needs to prevent that win. * */ this.calc = new GstKopplungsMoveCalculator<>(); this.playerA = new TttPlayer(PLAYER_A, 1); this.playerB = new TttPlayer(PLAYER_B, -1); this.kopplung = new TttKopplung(); final TttState state = new TttState(this.playerA, this.playerB); state.getBoard()[0][0] = this.playerA.getPlayerSymbolValue(); state.getBoard()[1][1] = this.playerA.getPlayerSymbolValue(); state.getBoard()[0][2] = this.playerB.getPlayerSymbolValue(); state.getBoard()[2][2] = this.playerB.getPlayerSymbolValue(); final Move<TttPlayer, TttState> move = this.calc.calculateMove(this.kopplung, state, 500, new GstKopplungNegaMaxMultithreading<>()); final TttMove tttMove = (TttMove) move; final TttMove bestMove = new TttPlaceMarkMove(1, 2); assertEquals(bestMove, tttMove); } }