Skip to contentMethod: DilemmaTitForTatStrategy(IDilemmaMoveFactory)
      1: package de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.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: /**
18:  * Cooperates on the first round and imitates its opponent's previous move thereafter.
19:  */
20: public class DilemmaTitForTatStrategy extends DilemmaMemoryStrategy {
21: 
22:     /**
23:      * Creates a new instance of the {@link DilemmaTitForTatStrategy} class.
24:      * @param moveFactory the move factory to use.
25:      */
26:     protected DilemmaTitForTatStrategy(final IDilemmaMoveFactory moveFactory) {
27:         super(moveFactory);
28:     }
29: 
30:     @Override
31:     public Optional<IDilemmaMove> computeNextMove(final int gameId, final IDilemmaPlayer player, 
32:             final IDilemmaState state)
33:             throws GameException, InterruptedException {
34:         final IGameMemory<DilemmaRoundData> memory = getMemoryForPlayer(player, state);
35:         final IDilemmaMoveFactory moveFactory = getMoveFactory();
36:         // cooperate on the first round ...
37:         if (memory.size() == 0) {
38:             return Optional.of(moveFactory.createCooperateMove());
39:         }
40:         // ...and imitate the opponent's previous move thereafter.
41:         final DilemmaRoundData previousRound = memory.getLatestRound();
42:         final DilemmaRoundPlayerData otherPlayersAction = previousRound.forOpponentOf(player);
43:         final IDilemmaMove myMove = moveFactory.sameAs(otherPlayersAction.move());
44:         return Optional.of(myMove);
45:     }
46: 
47:     @Override
48:     protected IGameMemoryCapacity requestedMemoryCapacity() {
49:         return GameMemoryCapacity.of(1);
50:     }
51: }