Skip to content

Content of file DilemmaCustomStrategy.java

package de.fhdw.gaming.ipspiel21.dilemmaOriginal.strategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;

import de.fhdw.gaming.core.domain.GameException;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.domain.DilemmaPlayer;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.domain.DilemmaState;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.domain.DilemmaStrategy;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.moves.DilemmaMove;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.moves.factory.DilemmaMoveFactory;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.moves.impl.DilemmaBeSilentMove;
import de.fhdw.gaming.ipspiel21.dilemmaOriginal.moves.impl.DilemmaConfessMove;

/**
 * Implements {@link DilemmaStrategy} by saying that what was set before the game started.
 */
public final class DilemmaCustomStrategy implements DilemmaStrategy {

    /**
     * Constant that represents the identifier when the opponent moves are {@link DilemmaConfessMove) for 100 percent.
     */
    private static final String IDENTIFIER_100_CONFESS = new DilemmaConfessMove().toString() + " Numberbased100";
    /**
     * Constant that represents the identifier when the opponent moves are {@link DilemmaConfessMove) for >50 percent.
     */
    private static final String IDENTIFIER_50_CONFESS = new DilemmaConfessMove().toString() + " Numberbased50";
    /**
     * Constant that represents the identifier when the opponent moves are {@link DilemmaBeSilentMove) for 100 percent.
     */
    private static final String IDENTIFIER_100_BESILENT = new DilemmaBeSilentMove().toString() + " Numberbased100";
    /**
     * Constant that represents the identifier when the opponent moves are {@link DilemmaBeSilentMove) for >50 percent.
     */
    private static final String IDENTIFIER_50_BESILENT = new DilemmaBeSilentMove().toString() + " Numberbased50";
    /**
     * The provided Data.
     */
    private Map<String, DilemmaMove> providedMoveData;
    /**
     * Amount of considered games.
     */
    private Integer amountOfGames;
    /**
     * Default move of player.
     */
    private DilemmaMove initialMove;
    
    /**
     * The factory for creating Dilemma moves.
     */
    private final DilemmaMoveFactory moveFactory;

    /**
     * Creates an {@link DilemmaAlwaysConfessStrategy}.
     *
     * @param moveFactory The factory for creating Dilemma moves.
     */
    public DilemmaCustomStrategy(final DilemmaMoveFactory moveFactory) {
        this.moveFactory = moveFactory;
    }
    /**
     * Returns the provided moves.
     * 
     * @return provided moves.
     */
    public Map<String, DilemmaMove> getProvidedMoveData() {
        return providedMoveData;
    }

    /**
     * Sets the provided moves.
     * 
     * @param providedMoveData
     */
    public void setProvidedMoveData(Map<String, Object> providedMoveData) {
        final Set<Entry<String, Object>> entrySet = providedMoveData.entrySet();

        for (Entry<String, Object> entry : entrySet) {
            if (IDENTIFIER_100_BESILENT.equals(entry.getKey()) || IDENTIFIER_100_CONFESS.equals(entry.getKey())
                    || IDENTIFIER_50_BESILENT.equals(entry.getKey()) || IDENTIFIER_50_CONFESS.equals(entry.getKey())) {
                providedMoveData.put(entry.getKey(), (DilemmaMove) entry.getValue());
            }
        }
    }

    /**
     * Returns the amount of considered games.
     * 
     * @return amount of considered games.
     */
    public Integer getAmountOfGames() {
        return amountOfGames;
    }

    /**
     * Sets the amount of considered games.
     * 
     * @param amountOfGames The amount of considered games.
     */
    public void setAmountOfGames(Integer amountOfGames) {
        this.amountOfGames = amountOfGames;
    }

    /**
     * Returns the initial move choice of the player.
     * 
     * @return initial move.
     */
    public DilemmaMove getInitialMove() {
        return initialMove;
    }

    /**
     * Sets the initial move.
     * 
     * @param initialMove The initial move choice of the player
     */
    public void setInitialMove(DilemmaMove initialMove) {
        this.initialMove = initialMove;
    }

    @Override
    public Optional<DilemmaMove> computeNextMove(int gameId, DilemmaPlayer player, DilemmaState state)
Parameter 'state' is not assigned and could be declared final.
A method argument that is never re-assigned within the method can be declared final.
    
        

public void foo1 (String param) {       // do stuff with param never assigning it

}

public void foo2 (final String param) { // better, do stuff with param never assigning it

}

        
    
See PMD documentation.
throws GameException, InterruptedException { if (providedMoveData.isEmpty()) { return Optional.of(initialMove); } final List<DilemmaMove> opponentMoves = new ArrayList<>(); for (int i = 0; i < amountOfGames; i++) { opponentMoves.add((DilemmaMove) player.getGameHistoryCollection().getSpecificGameHistory(i) .getOpponentMove(i)); } if (opponentMoves.isEmpty()) { return Optional.of(initialMove); } Integer countBeSilentMoves = 0; for (DilemmaMove dilemmaMove : opponentMoves) { if (dilemmaMove instanceof DilemmaBeSilentMove) { countBeSilentMoves++; } } return getMoveByPercentage(Double.valueOf(countBeSilentMoves), Double.valueOf(opponentMoves.size())); } /** * Returns the move choice of the current player by integrating the number based evaluation. * * @param countBeSilentMoves The amount of {@link DilemmaBeSilentMove}. * @param amountOfOppenentMoves The amount of opponent moves which are played. * @return */ private Optional<DilemmaMove> getMoveByPercentage(Double countBeSilentMoves, Double amountOfOppenentMoves) { final Double percent = countBeSilentMoves / amountOfOppenentMoves; if (percent == 1.0) { return Optional.of(this.providedMoveData.get(IDENTIFIER_100_BESILENT)); } else if (percent > 0.5) { return Optional.of(this.providedMoveData.get(IDENTIFIER_50_BESILENT)); } else if (percent <= 0.5 && percent > 0.0) { return Optional.of(this.providedMoveData.get(IDENTIFIER_50_CONFESS)); } else { return Optional.of(this.providedMoveData.get(IDENTIFIER_100_CONFESS)); } } }