Skip to content

Method: toString()

1: package de.fhdw.gaming.ipspiel23.dilemma.strategy.internals;
2:
3: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaPlayer;
4: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaState;
5: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMoveFactory;
6: import de.fhdw.gaming.ipspiel23.memory.GameMemoryIdentifier;
7: import de.fhdw.gaming.ipspiel23.memory.IGameMemory;
8: import de.fhdw.gaming.ipspiel23.memory.IGameMemoryCapacity;
9: import de.fhdw.gaming.ipspiel23.memory.IGameMemoryIdentifier;
10: import de.fhdw.gaming.ipspiel23.memory.IGameMemoryProvider;
11:
12: /**
13: * Represents the base class for all strategies that require a memory to access
14: * the decisions of previous rounds against the same opponent.
15: */
16: public abstract class DilemmaMemoryStrategy extends DilemmaStrategy {
17:
18: /**
19: * Creates a new instance of the {@link DilemmaMemoryStrategy} class.
20: * @param moveFactory The move factory to use for creating moves.
21: */
22: protected DilemmaMemoryStrategy(final IDilemmaMoveFactory moveFactory) {
23: super(moveFactory);
24: }
25:
26: /**
27: * How good should the memory be? (how many rounds in the past can be remembered?)
28: */
29: protected abstract IGameMemoryCapacity requestedMemoryCapacity();
30:
31: /**
32: * Gets or creates the memory for the given player - strategy combination.
33: * if the memory does not exist yet, it will be created.
34: * @param player The player to get the memory for.
35: * @param state The current state of the game.
36: * @return The memory for the given player - strategy combination.
37: */
38: protected final IGameMemory<DilemmaRoundData> getMemoryForPlayer(final IDilemmaPlayer player,
39: final IDilemmaState state) {
40: final IGameMemoryProvider memoryProvider = state.getMemoryProvider();
41: final IDilemmaPlayer opponent = state.getOpponentOf(player);
42: final IGameMemoryIdentifier identifier = GameMemoryIdentifier
43: .of(player, player.getStrategy(), opponent.getStrategy());
44: return memoryProvider.requestMemoryForStrategy(identifier, requestedMemoryCapacity());
45: }
46:
47: @Override
48: public int hashCode() {
49: int hash = 17;
50: hash = hash * 31 + toString().hashCode();
51: return hash;
52: }
53:
54: @Override
55: public String toString() {
56: return getClass().getSimpleName();
57: }
58:
59: @Override
60: public boolean equals(final Object other) {
61: return other != null
62: && other.getClass().equals(getClass())
63: && other.hashCode() == hashCode();
64: }
65: }