Skip to contentMethod: toString()
      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:  * Implements {@link GDStrategy} using the Grim Trigger strategy.
36:  * Starts by cooperating and continues to cooperate unless the opponent defects.
37:  * One defection leads to permanent defection.
38:  */
39: public final class GDGrimTriggerStrategy implements GDStrategy {
40: 
41:     /**
42:      * The factory for creating Gefangenen-Dilemma moves.
43:      */
44:     private final GDMoveFactory moveFactory;
45: 
46:     /**
47:      * Creates an {@link GDGrimTriggerStrategy}.
48:      *
49:      * @param moveFactory The factory for creating Gefangenen-Dilemma moves.
50:      */
51:     GDGrimTriggerStrategy(final GDMoveFactory moveFactory) {
52:         this.moveFactory = moveFactory;
53:     }
54: 
55:     @Override
56:     public Optional<GDMove> computeNextMove(
57:             final int gameId,
58:             final GDPlayer player,
59:             final GDState state,
60:             final long maxComputationTimePerMove) {
61: 
62:         GDPlayer opponent = null;
63:         for (Entry<String, GDPlayer> entry : state.getPlayers().entrySet()) {
64:             if (!entry.getValue().equals(player)) {
65:                 opponent = entry.getValue();
66:             }
67:         }
68: 
69:         final Key key = new MemoryKeyImpl(player, opponent, this);
70: 
71:         List<MemorySaveEntryImpl> check = null;
72:         if (MemoryBrainImpl.getInstance().getMemory(key) != null) {
73:             check = MemoryBrainImpl.getInstance().getMemory(key).show();
74:         }
75: 
76:         if (check == null || check.isEmpty()) {
77:             return Optional.of(this.moveFactory.createRemainSilentMove());
78:         }
79: 
80:         for (MemorySaveEntryImpl memoryPair : check) {
81:             final Move<?, ?> lastMoveUsed = memoryPair.getOpponentMove();
82:             if (lastMoveUsed.getClass().equals(GDSnitchMove.class)) {
83:                 return Optional.of(this.moveFactory.createSnitchMove());
84:             }
85:         }
86: 
87:         return Optional.of(this.moveFactory.createRemainSilentMove());
88:     }
89: 
90:     @Override
91:     public String toString() {
92:         return GDGrimTriggerStrategy.class.getSimpleName();
93:     }
94: }