Skip to content

Method: setMemoryLength(int)

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