Skip to content

Method: MemoryBrainImpl()

1: package de.fhdw.gaming.memory.impl;
2:
3: import java.util.ArrayList;
4: import java.util.HashMap;
5: import java.util.Map;
6: import java.util.Set;
7: import java.util.List;
8:
9: import de.fhdw.gaming.core.domain.Game;
10: import de.fhdw.gaming.core.domain.Move;
11: import de.fhdw.gaming.core.domain.Player;
12: import de.fhdw.gaming.core.domain.Strategy;
13: import de.fhdw.gaming.memory.Brain;
14: import de.fhdw.gaming.memory.Key;
15: import de.fhdw.gaming.memory.ShiftList;
16:
17:
18: /**
19: * A class managing all memories of all players.
20: */
21: public final class MemoryBrainImpl implements Brain {
22:
23: /**
24: * Returns the single instance of the MemoryBrainImpl.
25: */
26: private static Brain instance = new MemoryBrainImpl();
27:
28: /**
29: * The number of outcomes that can be remembered. -1 for as much as possible.
30: */
31: private int memoryLength = 0;
32:
33: /**
34: * A Map linking a key to a corresponding ShiftList.
35: */
36: private Map<Key, ShiftList> memory = new HashMap<>();
37:
38: /**
39: * A private constructor to prevent it being accessed from outside.
40: */
41: private MemoryBrainImpl() {
42: }
43:
44: /**
45: * Returns the singleton instance of MemoryBrainImpl.
46: */
47: public static Brain getInstance() {
48: return instance;
49: }
50:
51: /**
52: * .
53: * @return
54: */
55: public int getLength() {
56: return memoryLength;
57: }
58:
59:
60: /**
61: * Sets the amount of outcomes that the brain can remember. Can be used during a game.
62: * @param length
63: */
64: @Override
65: public void setMemoryLength(int length) throws IllegalArgumentException {
66: if (length >= -1) {
67: memoryLength = length;
68: for (Map.Entry<Key, ShiftList> entry : memory.entrySet()) {
69: final ShiftList memoryShiftList = entry.getValue();
70: memoryShiftList.setLength(memoryLength);
71: }
72: } else {
73: throw new IllegalArgumentException();
74: }
75: }
76:
77: /**
78: * Adds the outcomes of the current game for each player to the memory. Each combination of active player,
79: * their opponent, and the strategy of the active player requires a new {@link Key} for which the outcomes
80: * will be saved. In a 2 player game without changing strategies there should
81: * only ever be two {@link Key}s.
82: * @param game
83: */
84: @Override
85: public void rememberOutcome(Game<?, ?, ?, ?> game) {
86:
87: final Map<String, ? extends Strategy<?, ?, ?>> tempStrategies = game.getStrategies();
88: final Map<String, ? extends Player<?>> tempPlayers = game.getPlayers();
89: final List<String> playerNames = new ArrayList<>(tempPlayers.keySet());
90: final Set<Key> memoryKeys = memory.keySet();
91:
92: final Key currentKey1 = new MemoryKeyImpl(tempPlayers
93: .get(playerNames.get(0)), tempPlayers.get(playerNames.get(1)), tempStrategies.get(playerNames.get(0)));
94:
95: final Key currentKey2 = new MemoryKeyImpl(tempPlayers
96: .get(playerNames.get(1)), tempPlayers.get(playerNames.get(0)), tempStrategies.get(playerNames.get(1)));
97:
98: if (memoryKeys.contains(currentKey1)) {
99: memory.get(currentKey1).push(currentKey1.getPlayer().getOutcome());
100: } else {
101: final MemoryShiftListImpl newShiftList1 = new MemoryShiftListImpl(memoryLength);
102: newShiftList1.push(currentKey1.getPlayer().getOutcome());
103: memory.put(currentKey1, newShiftList1);
104: }
105: if (memoryKeys.contains(currentKey2)) {
106: memory.get(currentKey2).push(currentKey2.getPlayer().getOutcome());
107: } else {
108: final MemoryShiftListImpl newShiftList2 = new MemoryShiftListImpl(memoryLength);
109: newShiftList2.push(currentKey2.getPlayer().getOutcome());
110: memory.put(currentKey2, newShiftList2);
111: }
112:
113: this.setMemoryLength(memoryLength);
114:
115: }
116:
117: /**
118: * .
119: * @param key
120: * @param move
121: */
122: public void rememberMyMove(Key key, Move<?, ?> move) {
123: if (memory.keySet().contains(key)) {
124: getMemory(key).rememberMyMove(move);
125: } else {
126: final MemoryShiftListImpl newShiftList1 = new MemoryShiftListImpl(memoryLength);
127:
128: newShiftList1.rememberMyMove(move);
129:
130: memory.put(key, newShiftList1);
131: }
132:
133: this.setMemoryLength(memoryLength);
134: }
135:
136: /**
137: * .
138: * @param key
139: * @param move
140: */
141: public void rememberOpponentMove(Key key, Move<?, ?> move) {
142: if (memory.keySet().contains(key)) {
143: getMemory(key).rememberOpponentMove(move);
144: } else {
145: final MemoryShiftListImpl newShiftList1 = new MemoryShiftListImpl(memoryLength);
146:
147: newShiftList1.rememberOpponentMove(move);
148:
149: memory.put(key, newShiftList1);
150: }
151:
152: this.setMemoryLength(memoryLength);
153: }
154:
155: /**
156: * Returns the {@link ShiftList} containing the outcomes for a certain {@link Key}.
157: * That means a certain combination of player the memory is for,
158: * their opponent and the strategy used by the active player.
159: * If the {@link Key} is not in memory, returns null.
160: * @param key {@link Key}
161: */
162: @Override
163: public ShiftList getMemory(Key key) {
164: final Set<Key> memoryKeys = memory.keySet();
165: if (memoryKeys.contains(key)) {
166: return memory.get(key);
167: } else {
168: return null;
169: }
170: }
171:
172: /**
173: * Returns the entirety of the memory. It can be used to acquire any
174: * {@link ShiftList} of outcomes with the right key.
175: */
176: @Override
177: public Map<Key, ShiftList> getMemory() {
178: return memory;
179: }
180:
181: /**
182: * Resets the brain.
183: */
184: public static void strongHitOnTheHead() {
185: instance = new MemoryBrainImpl();
186: }
187:
188:
189: }