Package: GDTitForTwoTatsStrategy
GDTitForTwoTatsStrategy
| name | instruction | branch | complexity | line | method | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| GDTitForTwoTatsStrategy(GDMoveFactory) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| computeNextMove(int, GDPlayer, GDState, long) | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
| toString() | 
  | 
  | 
  | 
  | 
  | 
||||||||||||||||||||
Coverage
1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-GD.
5:  *
6:  * ipspiel24-GD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7:  * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
8:  * version.
9:  *
10:  * ipspiel24-GD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
11:  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12:  *
13:  * You should have received a copy of the GNU General Public License along with ipspiel24-GD. If not, see
14:  * <http://www.gnu.org/licenses/>.
15:  */
16: package de.fhdw.gaming.GefangenenDilemma.strategy.stratsWithBrain;
17: 
18: import java.util.List;
19: import java.util.Map.Entry;
20: import java.util.Optional;
21: 
22: import de.fhdw.gaming.GefangenenDilemma.domain.GDPlayer;
23: import de.fhdw.gaming.GefangenenDilemma.domain.GDState;
24: import de.fhdw.gaming.GefangenenDilemma.domain.GDStrategy;
25: import de.fhdw.gaming.GefangenenDilemma.moves.GDMove;
26: import de.fhdw.gaming.GefangenenDilemma.moves.factory.GDMoveFactory;
27: import de.fhdw.gaming.GefangenenDilemma.moves.impl.GDSnitchMove;
28: import de.fhdw.gaming.core.domain.Move;
29: import de.fhdw.gaming.memory.Key;
30: import de.fhdw.gaming.memory.impl.MemoryBrainImpl;
31: import de.fhdw.gaming.memory.impl.MemoryKeyImpl;
32: import de.fhdw.gaming.memory.impl.MemorySaveEntryImpl;
33: 
34: /**
35:  * Tit for Two Tats Strategy:
36:  * Cooperates in the first round and defects only if the opponent defected in the last two rounds.
37:  */
38: public final class GDTitForTwoTatsStrategy implements GDStrategy {
39: 
40:     /**
41:      * The factory for creating Gefangenen-Dilemma moves.
42:      */
43:     private final GDMoveFactory moveFactory;
44: 
45:     /**
46:      * Creates an {@link GDTitForTwoTatsStrategy}.
47:      *
48:      * @param moveFactory The factory for creating Gefangenen-Dilemma moves.
49:      */
50:     GDTitForTwoTatsStrategy(final GDMoveFactory moveFactory) {
51:         this.moveFactory = moveFactory;
52:     }
53: 
54:     @Override
55:     public Optional<GDMove> computeNextMove(
56:             final int gameId,
57:             final GDPlayer player,
58: 
59:             final GDState state,
60: 
61:             final long maxComputationTimePerMove) {
62: 
63:         GDPlayer opponent = null;
64:•        for (Entry<String, GDPlayer> entry : state.getPlayers().entrySet()) {
65:•            if (!entry.getValue().equals(player)) {
66:                 opponent = entry.getValue();
67:             }
68:         }
69: 
70:         final Key key = new MemoryKeyImpl(player, opponent, this);
71: 
72:         List<MemorySaveEntryImpl> check = null;
73:•        if (MemoryBrainImpl.getInstance().getMemory(key) != null) {
74:             check = MemoryBrainImpl.getInstance().getMemory(key).show();
75:         }
76: 
77:•        if (check == null || check.size() < 2) {
78:             return Optional.of(this.moveFactory.createRemainSilentMove());
79:         }
80: 
81:         final int size = check.size();
82:         final Move<?, ?> lastMoveUsed1 = check.get(size - 1).getOpponentMove();
83:         final Move<?, ?> lastMoveUsed2 = check.get(size - 2).getOpponentMove();
84:•        if (lastMoveUsed1.getClass().equals(GDSnitchMove.class)
85:•                && lastMoveUsed2.getClass().equals(GDSnitchMove.class)) {
86:             return Optional.of(this.moveFactory.createSnitchMove());
87:         } else {
88:             return Optional.of(this.moveFactory.createRemainSilentMove());
89:         }
90: 
91:     }
92: 
93:     @Override
94:     public String toString() {
95:         return GDTitForTwoTatsStrategy.class.getSimpleName();
96:     }
97: }