Skip to content

Method: deepCopy()

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.domain.impl;
17:
18: import java.util.Collections;
19: import java.util.LinkedHashMap;
20: import java.util.Map;
21: import java.util.Objects;
22: import java.util.Optional;
23: import java.util.Random;
24:
25: import de.fhdw.gaming.GefangenenDilemma.domain.GDPlayer;
26: import de.fhdw.gaming.core.domain.AbstractPlayer;
27:
28: /**
29: * Implements {@link GDPlayer}.
30: */
31: final class GDPlayerImpl
32: extends AbstractPlayer<GDPlayer> implements GDPlayer {
33:
34: /**
35: * The possible outcomes of this player. The key for the first-level map is the answer of the first player, the key
36: * for the second-level map is the answer of the second player.
37: */
38: private final Map<GDGameBuilderFactoryImpl.MOVES,
39: Map<GDGameBuilderFactoryImpl.MOVES, Double>> possibleOutcomes;
40: /**
41: * The answer of the player.
42: */
43: private Optional<GDGameBuilderFactoryImpl.MOVES> answer;
44:
45: /**
46: * implements a random object.
47: */
48: private final Random random = new Random();
49:
50: /**
51: * Creates a Gefangenen-Dilemma player.
52: *
53: * @param name The name of the player.
54: * @param possibleOutcomes The possible outcomes of this player. The key for the first-level map is the answer of
55: * the first player, the key for the second-level map is the answer of the second player.
56: */
57: GDPlayerImpl(final String name,
58: final Map<GDGameBuilderFactoryImpl.MOVES,
59: Map<GDGameBuilderFactoryImpl.MOVES, Double>> possibleOutcomes) {
60: super(name);
61: this.possibleOutcomes = Collections
62: .unmodifiableMap(new LinkedHashMap<>(Objects.requireNonNull(possibleOutcomes, "possibleOutcomes")));
63: this.answer = Optional.empty();
64: }
65:
66: /**
67: * Creates a Gefangenen-Dilemma player.
68: *
69: * @param source The {@link GDPlayer} to copy.
70: */
71: GDPlayerImpl(final GDPlayer source) {
72: super(source);
73: this.possibleOutcomes = source.getPossibleOutcomes();
74: this.answer = source.getAnswer();
75: }
76:
77: @Override
78: public String toString() {
79: return String
80: .format("GDPlayer[name=%s, state=%s, outcome=%s, answer=%s]", this.getName(), this.getState(),
81: this.getOutcome(),
82: this.answer);
83: }
84:
85: @Override
86: public boolean equals(final Object obj) {
87: if (obj instanceof GDPlayerImpl) {
88: final GDPlayerImpl other = (GDPlayerImpl) obj;
89: return super.equals(obj) && this.answer.equals(other.answer)
90: && this.possibleOutcomes.equals(other.possibleOutcomes);
91: }
92: return false;
93: }
94:
95: @SuppressWarnings("PMD.UselessOverridingMethod")
96: @Override
97: public int hashCode() {
98: return super.hashCode() ^ Objects.hash(this.answer, this.possibleOutcomes);
99: }
100:
101: @Override
102: public Map<GDGameBuilderFactoryImpl.MOVES,
103: Map<GDGameBuilderFactoryImpl.MOVES, Double>> getPossibleOutcomes() {
104: return this.possibleOutcomes;
105: }
106:
107: @Override
108: public Optional<GDGameBuilderFactoryImpl.MOVES> getAnswer() {
109: return this.answer;
110: }
111:
112: @Override
113: public void setAnswer(final GDGameBuilderFactoryImpl.MOVES newAnswer) {
114: if (this.answer.isPresent()) {
115: throw new IllegalStateException(String.format("Player %s tried to change her answer.", this.getName()));
116: }
117: this.answer = Optional.of(newAnswer);
118: }
119:
120: @Override
121: public GDPlayer deepCopy() {
122: return new GDPlayerImpl(this);
123: }
124:
125: /**
126: * gettern for random.
127: */
128: @Override
129: public Random getRandom() {
130: return this.random;
131: }
132: }