-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.java
More file actions
298 lines (250 loc) · 6.3 KB
/
Move.java
File metadata and controls
298 lines (250 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import java.util.StringTokenizer;
import java.util.Vector;
/**
* Represents a move.
*
* In general, you should regard this as an opaque structure that exists in the
* GameState you obtain from FindPossibleMoves.
*
* The methods IsNormal(), IsJump() and IsEOG() might be useful.
*
* You can probably ignore the rest of the class.
* Note that "jump" refers to capturing a piece by jumping over it.
*/
public class Move {
/**
* Constants for different types of moves.
*
*/
public static final int MOVE_JUMP = 1; // A single jump (numbers above this will represent multiple jumps)
public static final int MOVE_NORMAL = 0; // A normal move
public static final int MOVE_BOG = -1; // Beginning of game
public static final int MOVE_RW = -2; // Red wins => end of game
public static final int MOVE_WW = -3; // White wins => end of game
public static final int MOVE_DRAW = -4; // Draw => end of game
public static final int MOVE_NULL = -5; // A null move
static final String cDelimiter = "_";
private int mType;
private Vector<Integer> mData = new Vector<Integer>();
/**
* Constructs a specific type of move.
*
* @param moveType
* moveType should be one of MOVE_BOG, MOVE_RW, MOVE_WW or MOVE_DRAW
* @return
*/
public Move(int moveType) {
this.mType = moveType;
}
/**
* Constructs a MOVE_BOG move.
*/
public Move() {
this(MOVE_BOG);
}
/**
* Constructs a normal move (not a jump).
*
* @param p1 the starting square
* @param p2 the ending square
*/
public Move(int p1, int p2) {
this(MOVE_NORMAL);
assert (this.mData.size() == 0);
this.mData.add(p1);
this.mData.add(p2);
}
/**
* Constructs a jump move, which may consist of a series of jumps.
*
* @param pData a series of squares which form the sequence of jumps
*/
public Move(Vector<Integer> pData, int len) {
this.mType = len - 1;
this.mData.setSize(0);
for (int i = 0; i < len; i++) {
this.mData.add(pData.get(i));
}
}
/**
* Reconstructs the move from a string.
*
* @param pString a string, which should have been previously generated by
* ToMessage(), or obtained from the server.
*/
public Move(final String pString) {
StringTokenizer st = new StringTokenizer(pString, Move.cDelimiter);
String str = st.nextToken();
this.mType = Integer.parseInt(str);
int lLen = 0;
if (mType == MOVE_NORMAL) {
lLen = 2;
} else if (mType > 0) {
lLen = mType + 1;
}
if (lLen > 12 || mType < MOVE_NULL) {
mType = MOVE_NULL;
return;
}
this.mData.setSize(lLen);
for (int i = 0; i < lLen; i++) {
int lCell;
str = st.nextToken();
lCell = Integer.parseInt(str);
if (lCell < 0 || lCell > 31) {
this.mType = MOVE_NULL;
break;
}
this.mData.set(i, lCell);
}
if (st.hasMoreTokens()) {
this.mType = MOVE_NULL;
}
}
/**
* Creates a deep copy of a Move.
*/
public Move(Move m) {
this.mType = m.mType;
this.mData = new Vector<Integer>(m.mData);
}
/**
* Creates a reverse version of this move.
*
* By "reverse" we mean to say that red player becomes white player, and
* vice versa.
*/
public Move reversed() {
Move result = new Move(this.mType);
if (this.isRedWin()) {
result.mType = MOVE_WW;
} else if (this.isWhiteWin()) {
result.mType = MOVE_RW;
}
for (int i = 0; i < this.mData.size(); i++) {
result.mData.add(31 - this.mData.elementAt(i));
}
return result;
}
/**
* Checks if the movement is null or invalid.
*/
public boolean isNull() {
return (this.mType == MOVE_NULL);
}
/**
* Checks if the movement marks beginning of game.
*/
public boolean isBOG() {
return (this.mType == MOVE_BOG);
}
/**
* Checks if the movement marks end of game.
*/
public boolean isEOG() {
return (this.mType < MOVE_BOG);
}
/**
* Checks if the game ended in red win.
*/
public boolean isRedWin() {
return (this.mType == MOVE_RW);
}
/**
* Checks if the game ended in white win.
*/
public boolean isWhiteWin() {
return (this.mType == MOVE_WW);
}
/**
* Checks if the game ended in draw.
*/
public boolean isDraw() {
return (this.mType == MOVE_DRAW);
}
/**
* Checks if the movement is a jump.
*/
public boolean isJump() {
return (this.mType > 0);
}
/**
* Checks if the movement is a normal move.
*/
public boolean isNormal() {
return (this.mType == MOVE_NORMAL);
}
/**
* Gets the type of the move.
*/
public int getType() {
return this.mType;
}
/**
* Gets (for normal moves and jumps) the length of the move sequence.
*/
public int length() {
return this.mData.size();
}
/**
* Gets the pN-th square in the move sequence.
*/
public int at(int pN) {
return this.mData.elementAt(pN);
}
/**
* Converts the move to a string so that it can be sent to the other player.
*/
public String toMessage() {
String lStream = new String();
lStream = lStream + mType;
for (int i = 0; i < this.mData.size(); i++) {
lStream = lStream + cDelimiter + this.mData.elementAt(i);
}
return lStream;
}
@Override
public String toString() {
if (this.mType == MOVE_WW) {
return "WW";
}
if (this.mType == MOVE_RW) {
return "RW";
}
if (this.mType == MOVE_DRAW) {
return "DRAW";
}
if (this.mType == MOVE_BOG) {
return "BOG";
}
if (this.isNull()) {
return "NULL";
}
String lStream = new String();
String delimiter = isNormal() ? "-" : "x";
assert (this.mData.size() > 0);
// Concatenate all the cell numbers
lStream = lStream + this.mData.elementAt(0);
for (int i = 1; i < this.mData.size(); i++) {
lStream = lStream + delimiter + this.mData.elementAt(i);
}
return lStream;
}
/**
* Checks if the two objects represent the same move.
*/
public boolean equals(final Move pRH) {
if (this.mType != pRH.mType) {
return false;
}
if (this.mData.size() != pRH.mData.size()) {
return false;
}
for (int i = 0; i < this.mData.size(); i++) {
if (this.mData.elementAt(i) != pRH.mData.elementAt(i)) {
return false;
}
}
return true;
}
}