Package: C4Move
C4Move
| name | instruction | branch | complexity | line | method | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| C4Move(IC4Player, IC4Position) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| C4Move(IC4Player, int, int) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| applyTo(IC4State, IC4Player) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| equals(Object) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getPlayer() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| getTargetPosition() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| hashCode() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| toString() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
Coverage
1: package de.fhdw.gaming.ipspiel23.c4.moves.impl;
2: 
3: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Position;
4: import de.fhdw.gaming.core.domain.GameException;
5: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Field;
6: import de.fhdw.gaming.ipspiel23.c4.domain.IC4Player;
7: import de.fhdw.gaming.ipspiel23.c4.domain.IC4State;
8: import de.fhdw.gaming.ipspiel23.c4.domain.impl.C4Position;
9: import de.fhdw.gaming.ipspiel23.c4.moves.IC4Move;
10: 
11: /**
12:  * The default implementation of {@link IC4Move}.
13:  */
14: final class C4Move implements IC4Move {
15:     
16:     /**
17:      * The player that performs this move.
18:      */
19:     // I don't think we actually need the player in here 
20:     // (but the framework kinda forces us to have it ¯\_(ツ)_/¯)
21:     // yeah idunno, we basically only ever need the player passed to us 
22:     // via applyTo, but we still do some sanity checks against this
23:     // player instance here, just in case there actually *is* a meaning
24:     // behind all of this :)
25:     private final IC4Player localPlayer;
26: 
27:     /**
28:      * The row at which the token is inserted.
29:      */
30:     private final int row;
31: 
32:     /**
33:      * The column at which the token is inserted.
34:      */
35:     private final int column;
36: 
37:     /**
38:      * The lazily initialized target position.
39:      */
40:     private IC4Position position;
41: 
42:     /**
43:      * Creates a new instance of {@link C4Move}.
44:      * 
45:      * @param player The player that performs this move.
46:      * @param rowIndex The row at which the token is inserted.
47:      * @param columnIndex The column at which the token is inserted.
48:      */
49:     C4Move(final IC4Player player, final int rowIndex, final int columnIndex) {
50:         this.localPlayer = player;
51:         this.row = rowIndex;
52:         this.column = columnIndex;
53:     }
54: 
55:     /**
56:      * Creates a new instance of {@link C4Move}.
57:      * 
58:      * @param player The player that performs this move.
59:      * @param position The position at which the token is inserted.
60:      */
61:     C4Move(final IC4Player player, final IC4Position position) {
62:         this(player, position.getRow(), position.getColumn());
63:         this.position = position;
64:     }
65: 
66:     /**
67:      * Gets the player that performs this move.
68:      * @return The player that performs this move.
69:      */
70:     IC4Player getPlayer() {
71:         return this.localPlayer;
72:     }
73: 
74:     @Override
75:     public IC4Position getTargetPosition() {
76:•        if (this.position == null) {
77:             this.position = new C4Position(this.row, this.column);
78:         }
79:         return this.position;
80:     }
81: 
82:     @Override
83:     public void applyTo(final IC4State state, final IC4Player player) throws GameException {
84: 
85:         // quick sanity check, still not sure why player would be different from localPlayer
86:         // but it can't hurt to be sure I guess :P
87:•        if (player.getToken() != this.localPlayer.getToken()) {
88:             throw new IllegalStateException(String
89:                 .format("The specified player '%s' cannot perform move '%s', which was reserved for '%s'", 
90:                 player, this.localPlayer, this.toString()));
91:         }
92: 
93:         final IC4Field field = state.getBoard().getField(this.getTargetPosition());
94:         final boolean success = field.trySetOccupyingPlayer(player, false);
95:•        if (!success) {
96:             throw new GameException(String.format("The requested move '%s' could not be performed. "
97:             + "Did you attempt to place a token in the air? Was the target position already occupied?", 
98:             this.toString()));
99:         }
100:         //System.out.println(state.getBoard().toString());
101:         state.onMoveCompleted();
102:     }
103: 
104:     @Override
105:     public String toString() {
106:         return String.format("Insert token at (%s,%s)", this.column, this.row);
107:     }
108:     
109:     @Override
110:     public boolean equals(final Object obj) {
111:•        if (obj == null) {
112:             return false;
113:         }
114:•        if (!(obj instanceof IC4Move)) {
115:             return false;
116:         }
117:         final IC4Move other = (IC4Move) obj;
118:         return this.getTargetPosition().equals(other.getTargetPosition());
119:     }
120:     
121:     @Override
122:     public int hashCode() {
123:         int hash = 7;
124:         hash = hash * 31 + row;
125:         hash = hash * 31 + column;
126:         hash = hash * 31 + localPlayer.getToken();
127:         return hash;
128:     }
129: }
130: 
    