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 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("PlayerA", 1);
        this.playerB = new TttPlayer("PlayerB", -1);
The String literal "PlayerB" appears 6 times in this file; the first occurrence is on line 55.
Code containing duplicate String literals can usually be improved by declaring the String as a constant field.
    
        

private void bar() {
     buz("Howdy");
     buz("Howdy");
     buz("Howdy");
     buz("Howdy");
}
private void buz(String x) {}

        
    
See PMD documentation.
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("PlayerA", 1); this.playerB = new TttPlayer("PlayerB", -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("PlayerA", 1); this.playerB = new TttPlayer("PlayerB", -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("PlayerA", 1); this.playerB = new TttPlayer("PlayerB", -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("PlayerA", 1); this.playerB = new TttPlayer("PlayerB", -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("PlayerA", 1); this.playerB = new TttPlayer("PlayerB", -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); } }