Skip to content

Package: DilemmaDiscriminatingAltruistStrategy

DilemmaDiscriminatingAltruistStrategy

nameinstructionbranchcomplexitylinemethod
DilemmaDiscriminatingAltruistStrategy(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: 36
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
requestedMemoryCapacity()
M: 0 C: 2
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.discriminating_altruist;
2:
3: import java.util.Optional;
4:
5: import de.fhdw.gaming.core.domain.GameException;
6: import de.fhdw.gaming.ipspiel23.dilemma.domain.DilemmaAnswerType;
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.DilemmaMemoryStrategy;
12: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaRoundData;
13: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.cooperate.DilemmaCooperateStrategy;
14: import de.fhdw.gaming.ipspiel23.memory.GameMemoryCapacity;
15: import de.fhdw.gaming.ipspiel23.memory.IGameMemory;
16: import de.fhdw.gaming.ipspiel23.memory.IGameMemoryCapacity;
17:
18: /**
19: * This strategy is a variant of the {@link DilemmaCooperateStrategy} that
20: * discriminates against opponents that have defected in the past.
21: *
22: * It does so by refusing to engage with opponents that have defected in the
23: * past. So it basically just becomes upset and doesn't want to play anymore.
24: * Poor thing.
25: */
26: public class DilemmaDiscriminatingAltruistStrategy extends DilemmaMemoryStrategy {
27:
28: /**
29: * Creates a new instance of the {@link DilemmaDiscriminatingAltruistStrategy} class.
30: *
31: * @param moveFactory the move factory to use
32: */
33: protected DilemmaDiscriminatingAltruistStrategy(final IDilemmaMoveFactory moveFactory) {
34: super(moveFactory);
35: }
36:
37: @Override
38: public Optional<IDilemmaMove> computeNextMove(final int gameId, final IDilemmaPlayer player,
39: final IDilemmaState state)
40: throws GameException, InterruptedException {
41: final IGameMemory<DilemmaRoundData> memory = getMemoryForPlayer(player, state);
42: final IDilemmaMoveFactory moveFactory = getMoveFactory();
43:
44: int index;
45: // check enire history for defect of opponent
46: // maybe for some reason we didn't start out using DA strategy?
47:• for (index = 0; index < memory.size() && memory
48: .getRound(index)
49: .forOpponentOf(player)
50:• .answer().equals(DilemmaAnswerType.COOPERATE);) {
51: // pretty ugly, but PMD didn't like the empty for loop :P
52: index++;
53: }
54:
55:• if (index >= memory.size()) {
56: // our opponent always cooperated :)
57: return Optional.of(moveFactory.createCooperateMove());
58: }
59: // refuse to engage/do anything
60: // basically give up :P
61: // also: what does this even mean?
62: // if you're not saying anything isn't that technically cooperating?
63: return Optional.empty();
64: }
65:
66: @Override
67: protected IGameMemoryCapacity requestedMemoryCapacity() {
68: return GameMemoryCapacity.unlimited();
69: }
70: }