Skip to content

Method: C4Player(C4Player)

1: package de.fhdw.gaming.ipspiel23.c4.domain.impl;
2:
3: import java.util.Optional;
4:
5: import de.fhdw.gaming.core.domain.PlayerState;
6: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Player;
7: import de.fhdw.gaming.ipspiel23.c4.strategies.IC4Strategy;
8:
9: /**
10: * The default implementation of {@link IC4Player}.
11: */
12: public class C4Player implements IC4Player {
13:
14: /**
15: * The name of the player.
16: */
17: private final String name;
18:
19: /**
20: * The token uniquely identifying the player.
21: */
22: private final int token;
23:
24: /**
25: * The state of the player.
26: * @hidden this is an attempt to get PMD to shut up about the "DataClass" warning
27: */
28: private final C4MutablePlayerState mutableState;
29:
30: /**
31: * The strategy of the player.
32: */
33: private IC4Strategy strategy;
34:
35: /**
36: * Creates a new instance.
37: *
38: * @param builder The parent player builder that will be receive the strategy hook for the player.
39: * @param token The token uniquely identifying the player.
40: * @param name The name of the player.
41: */
42: C4Player(final C4PlayerBuilder builder, final int token, final String name) {
43: this.token = token;
44: this.name = name;
45: builder.setPlayerStrategyHook(newStrategy -> this.strategy = newStrategy);
46:
47: this.mutableState = new C4MutablePlayerState(PlayerState.PLAYING, null);
48: }
49:
50: /**
51: * Copy constructor.
52: *
53: * @param player The player to copy.
54: */
55: private C4Player(final C4Player player) {
56: this.token = player.token;
57: this.name = player.name;
58: this.mutableState = new C4MutablePlayerState(player.mutableState.getState(),
59: player.mutableState.getOutcome().orElse(null));
60: this.strategy = player.strategy;
61: }
62:
63: @Override
64: public String getName() {
65: return this.name;
66: }
67:
68: @Override
69: public PlayerState getState() {
70: return this.mutableState.getState();
71: }
72:
73: @Override
74: public void setState(final PlayerState newState) {
75: this.mutableState.setState(newState);
76: }
77:
78: @Override
79: public Optional<Double> getOutcome() {
80: return this.mutableState.getOutcome();
81: }
82:
83: @Override
84: public void setOutcome(final double newOutcome) {
85: this.mutableState.setOutcome(newOutcome);
86: }
87:
88: @Override
89: public IC4Player deepCopy() {
90: return new C4Player(this);
91: }
92:
93: @Override
94: public int getToken() {
95: return this.token;
96: }
97:
98: @Override
99: public IC4Strategy getStrategy() {
100: if (this.strategy == null) {
101: throw new IllegalStateException("The strategy of the player " + this.name + " was never set.");
102: }
103: return this.strategy;
104: }
105:
106: @Override
107: public boolean equals(final Object other) {
108: if (other == null) {
109: return false;
110: }
111: if (!(other instanceof IC4Player)) {
112: return false;
113: }
114: final IC4Player otherPlayer = (IC4Player) other;
115: return this.token == otherPlayer.getToken()
116: && this.name.equals(otherPlayer.getName())
117: && this.getState() == otherPlayer.getState()
118: && this.getOutcome().equals(otherPlayer.getOutcome())
119: && this.getStrategy().equals(otherPlayer.getStrategy());
120: }
121:
122: @Override
123: public int hashCode() {
124: int hash = 7;
125: hash = 31 * hash + this.token;
126: hash = 31 * hash + this.name.hashCode();
127: hash = 31 * hash + this.mutableState.hashCode();
128: if (this.strategy != null) {
129: hash = 31 * hash + this.strategy.hashCode();
130: }
131: return hash;
132: }
133:
134: @Override
135: public String toString() {
136: return "C4Player [name=" + this.name
137: + ", token=" + this.token
138: + ", state=" + this.getState()
139: + ", outcome=" + this.getOutcome()
140: + ", strategy=" + this.strategy + "]";
141: }
142: }