Skip to content

Package: GDPavlovStrategy

GDPavlovStrategy

nameinstructionbranchcomplexitylinemethod
GDPavlovStrategy(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%
changeMove(Move)
M: 4 C: 14
78%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
M: 0 C: 1
100%
computeNextMove(int, GDPlayer, GDState, long)
M: 0 C: 77
100%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 0 C: 15
100%
M: 0 C: 1
100%
determineResponse(Move, Move)
M: 10 C: 20
67%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 2 C: 5
71%
M: 0 C: 1
100%
repeatMove(Move)
M: 11 C: 7
39%
M: 3 C: 1
25%
M: 2 C: 1
33%
M: 3 C: 2
40%
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.GameException;
30: import de.fhdw.gaming.core.domain.Move;
31: import de.fhdw.gaming.memory.Key;
32: import de.fhdw.gaming.memory.impl.MemoryBrainImpl;
33: import de.fhdw.gaming.memory.impl.MemoryKeyImpl;
34: import de.fhdw.gaming.memory.impl.MemoryPairImpl;
35:
36: /**
37: * implement {@link GDStrategy} by creating a {@link GDPavlovStrategy}.
38: */
39: public final class GDPavlovStrategy implements GDStrategy {
40:
41: /**
42: * The factory for creating Gefangenen-Dilemma moves.
43: */
44: private final GDMoveFactory moveFactory;
45:
46: /**
47: * Creates an {@link GDTitForTatStrategy}.
48: *
49: * @param moveFactory The factory for creating Gefangenen-Dilemma moves.
50: */
51: GDPavlovStrategy(final GDMoveFactory moveFactory) {
52: this.moveFactory = moveFactory;
53: }
54:
55: @Override
56: public Optional<GDMove> computeNextMove(
57: int gameId,
58: GDPlayer player,
59: GDState state,
60: long maxComputationTimePerMove)
61: throws GameException, InterruptedException {
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<MemoryPairImpl> check = null;
73:• if (MemoryBrainImpl.getInstance().getMemory(key) != null) {
74: check = MemoryBrainImpl.getInstance().getMemory(key).show();
75: }
76:
77:• if (check == null || check.isEmpty()) {
78: return Optional.of(this.moveFactory.createRemainSilentMove());
79: }
80:
81: final int size = check.size();
82: final Move<?, ?> lastMoveUsedOpponent = check.get(size - 1).getOpponentMove();
83: final Move<?, ?> lastMoveUsedSelf = check.get(size - 1).getMyMove();
84:
85: return determineResponse(lastMoveUsedOpponent, lastMoveUsedSelf);
86: }
87:
88: /**
89: * Determines the response according to the last played Moves from the player and the opponent.
90: *
91: * @param lastMoveUsedOpponent
92: * @param lastMoveUsedSelf
93: * @return
94: */
95: private Optional<GDMove> determineResponse(final Move<?, ?> lastMoveUsedOpponent,
96: final Move<?, ?> lastMoveUsedSelf) {
97:• if (lastMoveUsedOpponent == null || lastMoveUsedSelf == null) {
98: return Optional.of(this.moveFactory.createRemainSilentMove());
99: }
100:
101:• if (lastMoveUsedOpponent instanceof GDRemainSilentMove) {
102: return Optional.of(repeatMove(lastMoveUsedSelf));
103:• } else if (lastMoveUsedOpponent instanceof GDSnitchMove) {
104: return Optional.of(changeMove(lastMoveUsedSelf));
105: } else {
106: return Optional.of(this.moveFactory.createRemainSilentMove());
107: }
108: }
109:
110: /**
111: * Private method to assist computeNextMove.
112: *
113: * @param lastMove
114: * @return new Strategy with same class as lastMove
115: */
116: private GDMove repeatMove(Move<?, ?> lastMove) {
117:• if (lastMove instanceof GDRemainSilentMove) {
118: return this.moveFactory.createRemainSilentMove();
119:• } else if (lastMove instanceof GDSnitchMove) {
120: return this.moveFactory.createSnitchMove();
121: } else {
122: return this.moveFactory.createRemainSilentMove();
123: }
124: }
125:
126: /**
127: * Private method to assist computeNextMove.
128: *
129: * @param lastMove
130: * @return new Strategy with different class as lastMove
131: */
132: private GDMove changeMove(Move<?, ?> lastMove) {
133:• if (lastMove instanceof GDRemainSilentMove) {
134: return this.moveFactory.createSnitchMove();
135:• } else if (lastMove instanceof GDSnitchMove) {
136: return this.moveFactory.createRemainSilentMove();
137: } else {
138: return this.moveFactory.createRemainSilentMove();
139: }
140: }
141:
142: @Override
143: public String toString() {
144: return GDPavlovStrategy.class.getSimpleName();
145: }
146:
147: }