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: 5 C: 71
93%
M: 2 C: 10
83%
M: 2 C: 5
71%
M: 1 C: 15
94%
M: 0 C: 1
100%
determineResponse(Move)
M: 5 C: 20
80%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
M: 0 C: 1
100%
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.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.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.MemorySaveEntryImpl;
34:
35: /**
36: * Implements {@link GDStrategy}.
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: GDPlayer opponent = null;
62:• for (Entry<String, GDPlayer> entry : state.getPlayers().entrySet()) {
63:• if (!entry.getValue().equals(player)) {
64: opponent = entry.getValue();
65: }
66: }
67:
68: final Key key = new MemoryKeyImpl(player, opponent, this);
69:
70: List<MemorySaveEntryImpl> check = null;
71:• if (MemoryBrainImpl.getInstance().getMemory(key) != null) {
72: check = MemoryBrainImpl.getInstance().getMemory(key).show();
73: }
74:
75:• if (check == null || check.isEmpty()) {
76: return Optional.of(this.moveFactory.createRemainSilentMove());
77: }
78:
79: final Move<?, ?> lastMoveUsed = MemoryBrainImpl.getInstance().getMemory(key)
80: .show().get(check.size() - 1).getOpponentMove();
81:
82:• if (lastMoveUsed == null) {
83: return Optional.of(this.moveFactory.createRemainSilentMove());
84: }
85:
86: return determineResponse(lastMoveUsed);
87:
88: }
89:
90: /**
91: * Determines the move to play according to the last Opponent Move.
92: *
93: * @param lastMoveUsed
94: * @return
95: */
96: private Optional<GDMove> determineResponse(final Move<?, ?> lastMoveUsed) {
97:• if (lastMoveUsed.getClass().equals(GDRemainSilentMove.class)) {
98: return Optional.of(this.moveFactory.createRemainSilentMove());
99:• } else if (lastMoveUsed.getClass().equals(GDSnitchMove.class)) {
100: return Optional.of(this.moveFactory.createSnitchMove());
101: } else {
102: return Optional.of(this.moveFactory.createRemainSilentMove());
103: }
104: }
105:
106: @Override
107: public String toString() {
108: return GDTitForTatStrategy.class.getSimpleName();
109: }
110: }