Skip to content

Package: DilemmaImperfectTitForTatStrategy

DilemmaImperfectTitForTatStrategy

nameinstructionbranchcomplexitylinemethod
DilemmaImperfectTitForTatStrategy(IDilemmaMoveFactory)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
computeNextMove(int, IDilemmaPlayer, IDilemmaState)
M: 0 C: 35
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.imperfect_tit_for_tat;
2:
3: import java.util.Optional;
4: import java.util.Random;
5:
6: import de.fhdw.gaming.core.domain.GameException;
7: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaPlayer;
8: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaState;
9: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMove;
10: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMoveFactory;
11: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaRoundData;
12: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.tit_for_tat.DilemmaTitForTatStrategy;
13: import de.fhdw.gaming.ipspiel23.memory.IGameMemory;
14:
15: /**
16: * Tit-For-Tat, but imitates opponent's last move with high (but less than one) probability.
17: */
18: public class DilemmaImperfectTitForTatStrategy extends DilemmaTitForTatStrategy {
19:
20: /**
21: * The probability with which to play TFT.
22: */
23: static final double IMITATION_PROBABILITY = 0.9d;
24:
25: /**
26: * RNG.
27: */
28: private static final Random RANDOM = new Random();
29:
30: /**
31: * Creates a new instance of the {@link DilemmaImperfectTitForTatStrategy} class.
32: * @param moveFactory The move factory to use.
33: */
34: DilemmaImperfectTitForTatStrategy(final IDilemmaMoveFactory moveFactory) {
35: super(moveFactory);
36: }
37:
38: @Override
39: public Optional<IDilemmaMove> computeNextMove(final int gameId, final IDilemmaPlayer player,
40: final IDilemmaState state)
41: throws GameException, InterruptedException {
42: // if this is the first move, always cooperate (no previous move to imitate with less than 100% probability)
43: final IGameMemory<DilemmaRoundData> memory = getMemoryForPlayer(player, state);
44: final IDilemmaMoveFactory moveFactory = getMoveFactory();
45:• if (memory.size() == 0) {
46: return Optional.of(moveFactory.createCooperateMove());
47: }
48: // be lazy about it and see what TFT would normally return ...
49: final IDilemmaMove perfectTftMove = super.computeNextMove(gameId, player, state).get();
50: // ... now see what RNGsus has to say about that:
51:• return Optional.of(RANDOM.nextDouble() > IMITATION_PROBABILITY
52: ? moveFactory.oppositeOf(perfectTftMove)
53: : perfectTftMove);
54: }
55:
56: @Override
57: public String toString() {
58: return String.format("%s[imitationProbability: %f]", getClass().getSimpleName(), IMITATION_PROBABILITY);
59: }
60: }