Package: DilemmaGradualTitForTatStrategy
DilemmaGradualTitForTatStrategy
| name | instruction | branch | complexity | line | method | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| DilemmaGradualTitForTatStrategy(IDilemmaMoveFactory) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| computeNextMove(int, IDilemmaPlayer, IDilemmaState) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| continuePunishingOpponent(IDilemmaPlayer, IGameMemory) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| requestedMemoryCapacity() | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
Coverage
1: package de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.gradual_tit_for_tat;
2: 
3: import java.util.Optional;
4: 
5: import de.fhdw.gaming.core.domain.GameException;
6: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaPlayer;
7: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaState;
8: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMove;
9: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMoveFactory;
10: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaMemoryStrategy;
11: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaRoundData;
12: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaRoundPlayerData;
13: import de.fhdw.gaming.ipspiel23.memory.GameMemoryCapacity;
14: import de.fhdw.gaming.ipspiel23.memory.IGameMemory;
15: import de.fhdw.gaming.ipspiel23.memory.IGameMemoryCapacity;
16: 
17: import static de.fhdw.gaming.ipspiel23.dilemma.domain.DilemmaAnswerType.COOPERATE;
18: import static de.fhdw.gaming.ipspiel23.dilemma.domain.DilemmaAnswerType.DEFECT;
19: 
20: /**
21:  * TFT with two differences: 
22:  * <p>
23:  * (1) it increases the string of punishing defection responses with each additional defection by its opponent 
24:  * </p>
25:  * <p>
26:  * (2) it apologizes for each string of defections by cooperating in the subsequent two rounds.
27:  * </p>
28:  */
29: public class DilemmaGradualTitForTatStrategy extends DilemmaMemoryStrategy {
30: 
31:     /**
32:      * Creates a new instance of the {@link DilemmaGradualTitForTatStrategy} using the specified move factory.
33:      * 
34:      * @param moveFactory the move factory to use
35:      */
36:     protected DilemmaGradualTitForTatStrategy(final IDilemmaMoveFactory moveFactory) {
37:         super(moveFactory);
38:     }
39: 
40:     @Override
41:     public Optional<IDilemmaMove> computeNextMove(final int gameId, final IDilemmaPlayer player, 
42:             final IDilemmaState state)
43:             throws GameException, InterruptedException {
44:         final IGameMemory<DilemmaRoundData> memory = getMemoryForPlayer(player, state);
45:         final IDilemmaMoveFactory moveFactory = getMoveFactory();
46:         // cooperate on the first round ...
47:•        if (memory.size() == 0) {
48:             return Optional.of(moveFactory.createCooperateMove());
49:         }
50:         final DilemmaRoundData previousRound = memory.getRound(0, true);
51:         final DilemmaRoundPlayerData myPreviousAction = previousRound.forPlayer(player);
52:         final DilemmaRoundPlayerData otherPlayersAction = previousRound.forOpponentOf(player);
53:         // what happened the round before?
54:         // did I cooperate?
55:•        if (myPreviousAction.answer().equals(COOPERATE)) {
56:             // Am I currently apologizing?
57:•            if (memory.size() > 1 && memory.getRound(1, true).forPlayer(player).answer().equals(DEFECT)) {
58:                 // yes. cooperate again
59:                 return Optional.of(moveFactory.createCooperateMove());
60:             }
61:             // no. What did the opponent do previously?
62:•            if (otherPlayersAction.answer().equals(COOPERATE)) {
63:                 // they were nice to us, so we do the same
64:                 return Optional.of(moveFactory.createCooperateMove());
65:             }
66:             // they betrayed us :C 
67:             // let the punishment begin
68:             return Optional.of(moveFactory.createDefectMove());
69:         }
70:         // I was previously punishing my opponent!
71:         // do I need to continue?
72:•        if (continuePunishingOpponent(player, memory)) {
73:             // yes. make them suffer...
74:             return Optional.of(moveFactory.createDefectMove());
75:         }
76:         // no we're even now. Let's apologize
77:         return Optional.of(moveFactory.createCooperateMove());
78:     }
79:     
80:     /**
81:      * determines whether to continue punishing the opponent of the specified player.
82:      * @param player the player
83:      * @param memory the memory of the player.
84:      * @return true if we need to continue the punishment, otherwise false.
85:      */
86:     private static boolean continuePunishingOpponent(final IDilemmaPlayer player, 
87:             final IGameMemory<DilemmaRoundData> memory) {
88:         // I was previously punishing my opponent!
89:         // how much did I punish them already?
90:         int punishCounter;
91:•        for (punishCounter = 0; punishCounter < memory.size() 
92:             && memory.getRound(punishCounter, true)
93:                 .forPlayer(player)
94:•                .answer().equals(DEFECT);) {
95:             // more PMD-induced disgustingness :P
96:             punishCounter++;
97:         }
98:         // how often did I plan on punishing them?
99:         int defectsBeforePunishment = 0;
100:         // we don't count defects since we started the punishment to prevent deadlock.
101:•        for (int i = punishCounter; i < memory.size(); i++) {
102:•            if (memory.getRound(i, true).forOpponentOf(player).answer().equals(DEFECT)) {
103:                 defectsBeforePunishment++;
104:             }
105:         }
106:•        return punishCounter < defectsBeforePunishment;
107:     }
108:     
109:     @Override
110:     protected IGameMemoryCapacity requestedMemoryCapacity() {
111:         // perfect memory would be good
112:         return GameMemoryCapacity.unlimited();
113:     }
114: }