Skip to content

Package: GDTitForTatStrategy

GDTitForTatStrategy

nameinstructionbranchcomplexitylinemethod
GDTitForTatStrategy(GDMoveFactory)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
computeNextMove(int, GDPlayer, GDState, long)
M: 88 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
toString()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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;
17:
18: import java.util.Optional;
19: import java.util.List;
20: import java.util.Map.Entry;
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.GDRemainSilentMove;
28: import de.fhdw.gaming.GefangenenDilemma.moves.impl.GDSnitchMove;
29: import de.fhdw.gaming.core.domain.Move;
30: import de.fhdw.gaming.memory.Key;
31: import de.fhdw.gaming.memory.impl.MemoryBrainImpl;
32: import de.fhdw.gaming.memory.impl.MemoryKeyImpl;
33: import de.fhdw.gaming.memory.impl.MemoryPairImpl;
34:
35: /**
36: * Implements {@link GDStrategy} by always saying "yes".
37: */
38: public final class GDTitForTatStrategy implements GDStrategy {
39:
40: /**
41: * The factory for creating Gefangenen-Dilemma moves.
42: */
43: private final GDMoveFactory moveFactory;
44:
45: /**
46: * Creates an {@link GDTitForTatStrategy}.
47: *
48: * @param moveFactory The factory for creating Gefangenen-Dilemma moves.
49: */
50: GDTitForTatStrategy(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: final GDState state,
59: final long maxComputationTimePerMove) {
60:
61: //MemoryBrainImpl.getInstance().setMemoryLength(1);
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: final List<MemoryPairImpl> check = MemoryBrainImpl.getInstance().getMemory(key).show();
73:
74: //Add code inside this if
75:• if (check != null && !check.isEmpty()) {
76: final Move<?, ?> lastMoveUsed =
77: MemoryBrainImpl.getInstance().getMemory(key).show().get(0).getOpponentMove();
78:
79:
80:• if (lastMoveUsed != null) {
81:• if (lastMoveUsed.getClass().equals(GDRemainSilentMove.class)) {
82: return Optional.of(this.moveFactory.createRemainSilentMove());
83:• } else if (lastMoveUsed.getClass().equals(GDSnitchMove.class)) {
84: return Optional.of(this.moveFactory.createSnitchMove());
85: } else {
86: return Optional.of(this.moveFactory.createRandomMove());
87: }
88: } else {
89: return Optional.of(this.moveFactory.createRandomMove());
90: }
91: } else {
92: return Optional.of(this.moveFactory.createRandomMove());
93: }
94:
95:
96: }
97:
98: @Override
99: public String toString() {
100: return GDTitForTatStrategy.class.getSimpleName();
101: }
102: }