Skip to content

Content of file SSPGameBuilderImplTest.java

package de.schereSteinPapier.domain.impl;

import de.fhdw.gaming.core.domain.DefaultObserverFactoryProvider;
import de.fhdw.gaming.core.domain.GameException;
import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
import de.schereSteinPapier.domain.*;

//import static org.junit.jupiter.api.Assertions.assertThrows;

//import org.junit.jupiter.api.Test;

//import de.fhdw.gaming.core.domain.GameException;
//import de.schereSteinPapier.domain.SSPGameBuilder;

import de.schereSteinPapier.domain.factory.SSPStrategyFactory;
import de.schereSteinPapier.moves.factory.SSPMoveFactory;
import de.schereSteinPapier.moves.impl.SSPDefaultMoveFactory;

import static de.schereSteinPapier.SSPConstants.AuswahlConstants.PAPIER;
import static de.schereSteinPapier.SSPConstants.AuswahlConstants.SCHERE;
import static de.schereSteinPapier.SSPConstants.AuswahlConstants.STEIN;

import de.schereSteinPapier.strategy.SSPSayEquilibriumStrategyFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Tests {@link class SSPGameBuilderImpl}.
 */
class SSPGameBuilderImplTest {

    /**
     * GameBuilder
     */
    private SSPGameBuilderImpl sspGameBuilder;

    /**
     * Setup
     */
    @BeforeEach
    void setUp() {
        this.sspGameBuilder = new SSPGameBuilderImpl();
    }

    /**
     * Tries to create an SSP game with as much defaults as possible.
     */
    @Test
    void testCreateGameWithDefaults() throws GameException, InterruptedException {
        final SSPGameBuilder builder = new SSPGameBuilderImpl();

        final Map<String, Map<String, Double>> possibleOutcomes = new LinkedHashMap<>();
        final Map<String, Double> possibleOutcomesPapier = new LinkedHashMap<>();
        final Map<String, Double> possibleOutcomesStein = new LinkedHashMap<>();
        final Map<String, Double> possibleOutcomesSchere = new LinkedHashMap<>();

        possibleOutcomesSchere.put(SCHERE, 0.0);
        possibleOutcomesSchere.put(STEIN, -1.0);
        possibleOutcomesSchere.put(PAPIER, 1.0);

        possibleOutcomesStein.put(SCHERE, 1.0);
        possibleOutcomesStein.put(STEIN, 0.0);
        possibleOutcomesStein.put(PAPIER, -1.0);

        possibleOutcomesPapier.put(SCHERE, 1.0);
        possibleOutcomesPapier.put(STEIN, -1.0);
        possibleOutcomesPapier.put(PAPIER, 0.0);

        possibleOutcomes.put(SCHERE, possibleOutcomesSchere);
        possibleOutcomes.put(STEIN, possibleOutcomesStein);
        possibleOutcomes.put(PAPIER, possibleOutcomesPapier);

        final SSPPlayerBuilder playerBuilderA = builder.createPlayerBuilder().changeName("A")
                .changePossibleOutcomes(possibleOutcomes);
        final SSPPlayerBuilder playerBuilderB = builder.createPlayerBuilder().changeName("B")
                .changePossibleOutcomes(possibleOutcomes);
        try (SSPGame game = builder.addPlayer(playerBuilderA.build(),
                        new SSPStrategyStub(new SSPDefaultMoveFactory()))
                .addPlayer(playerBuilderB.build(), new SSPStrategyStub(new SSPDefaultMoveFactory()))
                .build(1)) {

            assertThat(game.getPlayers().keySet(), containsInAnyOrder("A", "B"));
        }
    }

    /**
     * Test for method addPlayer.
     */
    @Test
    void addPlayer() throws GameException {
        final Map<String, Map<String, Double>> possibleOutcomes = createPossibleOutcomes();

        SSPPlayer firstPlayer = createPlayer("A", possibleOutcomes);
        SSPPlayer secondPlayer = createPlayer("B", possibleOutcomes);

        sspGameBuilder.addPlayer(firstPlayer, createEquilibriumStrategy());
        sspGameBuilder.addPlayer(secondPlayer, createEquilibriumStrategy());

        try {
            SSPGame game = sspGameBuilder.build(1);
            assertTrue(game.getPlayers().containsKey("A") && game.getPlayers().containsKey("B"));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    Map<String, Map<String, Double>> createPossibleOutcomes() {
        final Map<String, Map<String, Double>> possibleOutcomes = new LinkedHashMap<>();
        Map<String, Double> possibleOutcomesSchere = new LinkedHashMap<>();
        Map<String, Double> possibleOutcomesStein = new LinkedHashMap<>();
        Map<String, Double> possibleOutcomesPapier = new LinkedHashMap<>();

        possibleOutcomesSchere.put(SCHERE, 0.0);
        possibleOutcomesSchere.put(STEIN, 1.0);
        possibleOutcomesSchere.put(PAPIER, -1.0);

        possibleOutcomesStein.put(STEIN, 0.0);
        possibleOutcomesStein.put(SCHERE, -1.0);
        possibleOutcomesStein.put(PAPIER, 1.0);

        possibleOutcomesPapier.put(PAPIER, 0.0);
        possibleOutcomesPapier.put(SCHERE, 1.0);
        possibleOutcomesPapier.put(STEIN, -1.0);

        possibleOutcomes.put(SCHERE, possibleOutcomesSchere);
        possibleOutcomes.put(STEIN, possibleOutcomesStein);
        possibleOutcomes.put(PAPIER, possibleOutcomesPapier);

        return possibleOutcomes;
    }

    /**
     * Creates a player
     *
     * @param playerName       name of the player
     * @param possibleOutcomes Map with all possible outcomes
     * @return SSPPlayer
     */
    SSPPlayer createPlayer(String playerName, Map<String, Map<String, Double>> possibleOutcomes) {
        try {
            return sspGameBuilder.createPlayerBuilder().changeName(playerName).changePossibleOutcomes(possibleOutcomes).build();
        } catch (GameException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Test for method changeMaximumComputationTimePerMove.
     */
    @Test
    void changeMaximumComputationTimePerMove() {
        sspGameBuilder.changeMaximumComputationTimePerMove(7);
        assertEquals(7, sspGameBuilder.getMaxComputationTimePerMove());
    }

    /**
     * Test for method changeObserverFactoryProvider.
     */
    @Test
    void changeObserverFactoryProvider() {
        ObserverFactoryProvider provider = new DefaultObserverFactoryProvider();
Variable 'provider' should be declared final.

Since Checkstyle 3.2

Checks that local variables that never have their values changed are declared final. The check can be configured to also check that unchanged parameters are declared final.

sspGameBuilder.changeObserverFactoryProvider(provider); assertEquals(provider, sspGameBuilder.getObserverFactoryProvider()); } /** * Test for method, that builds a game. */ @Test void build() throws InterruptedException, GameException { sspGameBuilder.addPlayer(createPlayer("A", createPossibleOutcomes()), createEquilibriumStrategy()); sspGameBuilder.addPlayer(createPlayer("B", createPossibleOutcomes()), createEquilibriumStrategy()); SSPGame game = sspGameBuilder.build(1); assertEquals(1, game.getId()); } /** * @return an EquilibriumStrategy */ SSPStrategy createEquilibriumStrategy() { final SSPMoveFactory moveFactory = new SSPDefaultMoveFactory(); SSPStrategyFactory equilibriumStrategyFactory = new SSPSayEquilibriumStrategyFactory(); return equilibriumStrategyFactory.create(moveFactory); } }