Package: SspStateImpl
SspStateImpl
| name | instruction | branch | complexity | line | method | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| SspStateImpl(SspPlayer, SspPlayer) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| SspStateImpl(SspStateImpl) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| computeNextPlayers() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| deepCopy() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| equals(Object) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getFirstPlayer() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getPlayers() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getSecondPlayer() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| hashCode() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| nextTurn() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| outcomeToState(Double) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| toString() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
Coverage
1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-Ssp.
5:  *
6:  * Ipspiel24-Ssp 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-Ssp 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-Ssp. If not, see
14:  * <http://www.gnu.org/licenses/>.
15:  */
16: package de.fhdw.gaming.ipspiel24.ssp.domain.impl;
17: 
18: import java.util.LinkedHashMap;
19: import java.util.LinkedHashSet;
20: import java.util.Map;
21: import java.util.Objects;
22: import java.util.Set;
23: 
24: import de.fhdw.gaming.core.domain.GameException;
25: import de.fhdw.gaming.core.domain.PlayerState;
26: import de.fhdw.gaming.ipspiel24.ssp.domain.SspPlayer;
27: import de.fhdw.gaming.ipspiel24.ssp.domain.SspPossibleChoice;
28: import de.fhdw.gaming.ipspiel24.ssp.domain.SspState;
29: 
30: /**
31:  * Implements {@link SspState}.
32:  */
33: final class SspStateImpl implements SspState {
34: 
35:     /**
36:      * The first player.
37:      */
38:     private final SspPlayer firstPlayer;
39:     /**
40:      * The second player.
41:      */
42:     private final SspPlayer secondPlayer;
43: 
44:     /**
45:      * Creates a Ssp state.
46:      *
47:      * @param firstPlayer  The first player.
48:      * @param secondPlayer The second player.
49:      * @throws GameException if the state cannot be created according to the rules of the game.
50:      */
51:     SspStateImpl(final SspPlayer firstPlayer, final SspPlayer secondPlayer)
52:             throws GameException {
53: 
54:         this.firstPlayer = Objects.requireNonNull(firstPlayer, "firstPlayer");
55:         this.secondPlayer = Objects.requireNonNull(secondPlayer, "secondPlayer");
56: 
57:•        if (this.firstPlayer.getName().equals(this.secondPlayer.getName())) {
58:             throw new IllegalArgumentException(
59:                     String.format("Both players have the same name '%s'.", this.firstPlayer.getName()));
60:         }
61:     }
62: 
63:     /**
64:      * Creates a Ssp state by copying an existing one.
65:      *
66:      * @param source The state to copy.
67:      */
68:     SspStateImpl(final SspStateImpl source) {
69:         this.firstPlayer = source.firstPlayer.deepCopy();
70:         this.secondPlayer = source.secondPlayer.deepCopy();
71:     }
72: 
73:     /**
74:      * Returns the first player.
75:      */
76:     @Override
77:     public SspPlayer getFirstPlayer() {
78:         return this.firstPlayer;
79:     }
80: 
81:     /**
82:      * Returns the second player.
83:      */
84:     @Override
85:     public SspPlayer getSecondPlayer() {
86:         return this.secondPlayer;
87:     }
88: 
89:     @Override
90:     public String toString() {
91:         return String.format("SspState[firstPlayer=%s, secondPlayer=%s]", this.firstPlayer, this.secondPlayer);
92:     }
93: 
94:     @Override
95:     public boolean equals(final Object obj) {
96:•        if (obj instanceof SspStateImpl) {
97:             final SspStateImpl other = (SspStateImpl) obj;
98:•            return this.firstPlayer.equals(other.firstPlayer) && this.secondPlayer.equals(other.secondPlayer);
99:         }
100:         return false;
101:     }
102: 
103:     @Override
104:     public SspState deepCopy() {
105:         return new SspStateImpl(this);
106:     }
107: 
108:     @Override
109:     public int hashCode() {
110:         return Objects.hash(this.firstPlayer, this.secondPlayer);
111:     }
112: 
113:     @Override
114:     public Map<String, SspPlayer> getPlayers() {
115:         final Map<String, SspPlayer> result = new LinkedHashMap<>();
116:         result.put(this.firstPlayer.getName(), this.firstPlayer);
117:         result.put(this.secondPlayer.getName(), this.secondPlayer);
118:         return result;
119:     }
120: 
121:     @Override
122:     public Set<SspPlayer> computeNextPlayers() {
123:         final Set<SspPlayer> playersWithoutMove = new LinkedHashSet<>();
124:•        if (this.firstPlayer.getAnswer().isEmpty()) {
125:             playersWithoutMove.add(this.firstPlayer);
126:         }
127:•        if (this.secondPlayer.getAnswer().isEmpty()) {
128:             playersWithoutMove.add(this.secondPlayer);
129:         }
130:         return playersWithoutMove;
131:     }
132: 
133:     @Override
134:     public void nextTurn() {
135:         final Set<SspPlayer> playersWithoutMove = this.computeNextPlayers();
136:•        if (playersWithoutMove.isEmpty()) {
137:             final SspPossibleChoice answerOfFirstPlayer = this.firstPlayer.getAnswer().orElseThrow();
138:             final SspPossibleChoice answerOfSecondPlayer = this.secondPlayer.getAnswer().orElseThrow();
139: 
140:             final Double outcomeOfFirstPlayer = this.firstPlayer.getPossibleOutcomes().get(answerOfFirstPlayer)
141:                     .get(answerOfSecondPlayer);
142:             this.firstPlayer.setState(outcomeToState(outcomeOfFirstPlayer));
143:             this.firstPlayer.setOutcome(outcomeOfFirstPlayer);
144: 
145:             final Double outcomeOfSecondPlayer = this.secondPlayer.getPossibleOutcomes().get(answerOfSecondPlayer)
146:                     .get(answerOfFirstPlayer);
147:             this.secondPlayer.setState(outcomeToState(outcomeOfSecondPlayer));
148:             this.secondPlayer.setOutcome(outcomeOfSecondPlayer);
149:         }
150:     }
151: 
152:     /**
153:      * Computes a player state from an outcome.
154:      *
155:      * @param outcome The player's outcome.
156:      */
157:     private static PlayerState outcomeToState(final Double outcome) {
158:•        return outcome > 0.0 ? PlayerState.WON : outcome < 0.0 ? PlayerState.LOST : PlayerState.DRAW;
159:     }
160: }
    