-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.java
More file actions
276 lines (232 loc) · 5.78 KB
/
Move.java
File metadata and controls
276 lines (232 loc) · 5.78 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
import java.util.StringTokenizer;
import java.util.Vector;
import java.lang.Integer;
/**
* Encapsulates a move
*
* In general, you should regard this as an opaque structure that exists
* in the GameState you obtain from findPossibleMoves.
*
* The functions isNormal() and isEOG() might be useful.
*
* You can probably ignore the rest of the interface.
*/
public class Move {
/**
* Constants for different types of moves.
*
*/
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_XW = -2; ///< X wins => end of game
public static final int MOVE_OW = -3; ///< O 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
public static final int SPECIAL_NONE = 0; ///< not a special move
public static final int SPECIAL_WIN = 1; ///< special move - win
public static final int SPECIAL_DRAW = 2; ///< special move - draw
private static final String DELIMITER = "_";
private int type;
private Vector<Integer> data = 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) {
setType(moveType);
}
/**
* Constructs a MOVE_BOG move.
*/
public Move() {
this(MOVE_BOG);
}
/**
* Constructs a normal move.
*
* @param p1 a destination cell
* @param p2 a player symbol
*/
public Move(int p1, int p2) {
this(MOVE_NORMAL);
this.data.setSize(0);
this.data.add(p1);
this.data.add(p2);
}
/**
* Constructs a Special move (Win or Draw) for player.
*
* @param p1 a destination cell
* @param p2 a player symbol
* @param specialMove a special move type
*/
public Move(int p1, int p2, int specialMove) {
this(p1, p2);
if (specialMove == SPECIAL_DRAW) {
setType(MOVE_DRAW);
}
if (specialMove == SPECIAL_WIN) {
if (p2 == Constants.CELL_O)
setType(MOVE_OW);
if (p2 == Constants.CELL_X)
setType(MOVE_XW);
}
}
/**
* Reconstructs the move from a string.
*
* @param string a string, which should have been previously generated by
* ToString(), or obtained from the server.
*/
public Move(final String string) {
StringTokenizer st = new StringTokenizer(string, DELIMITER);
String str = st.nextToken();
setType(Integer.parseInt(str));
int length = 0;
if (type == MOVE_NORMAL) {
length = 2;
} else if(type > 0) {
length = type + 1;
}
if (type == MOVE_OW) {
length = 2;
}
if (type == MOVE_XW) {
length = 2;
}
if (type == MOVE_DRAW) {
length = 2;
}
if ((length > 12) || (type < MOVE_NULL)) {
setType(MOVE_NULL);
return;
}
this.data.setSize(length);
for (int i = 0; i < length; ++i) {
str = st.nextToken();
int cell = Integer.parseInt(str);
this.data.set(i, cell);
}
}
/**
* Checks if the movement is null or invalid.
*/
public boolean isNull() {
return (this.type == MOVE_NULL);
}
/**
* Checks if the movement marks beginning of game.
*/
public boolean isBOG() {
return (this.type == MOVE_BOG);
}
/**
* Checks if the movement marks end of game.
*/
public boolean isEOG() {
return (this.type < MOVE_BOG);
}
/**
* Checks if the game ended in X win.
*/
public boolean isXWin() {
return (this.type == MOVE_XW);
}
/**
* Checks if the game ended in O win.
*/
public boolean isOWin() {
return (this.type == MOVE_OW);
}
/**
* Checks if the game ended in draw.
*/
public boolean isDraw() {
return (this.type == MOVE_DRAW);
}
/**
* Checks if the movement is a normal move.
*/
public boolean isNormal() {
return (this.type == MOVE_NORMAL);
}
/**
* Gets the type of the move.
*/
public int getType() {
return this.type;
}
private void setType(int moveType) {
this.type = moveType;
}
/**
* Gets the length of the move sequence.
*/
public int getLength() {
return this.data.size();
}
/**
* Gets the pN-th cell in the move sequence.
*/
public int at(int pN) {
return this.data.elementAt(pN);
}
/**
* Converts the move to a string so that it can be sent to the other player.
*/
public String toMessage() {
String str = new String();
str = str + type;
for (int i = 0; i < this.data.size(); ++i) {
str = str + DELIMITER + this.data.elementAt(i);
}
return str;
}
@Override
public String toString() {
if (this.type == MOVE_XW) {
return "XW";
}
if (this.type == MOVE_OW) {
return "OW";
}
if (this.type == MOVE_DRAW) {
return "DRAW";
}
if (this.type == MOVE_BOG) {
return "BOG";
}
if (this.isNull()) {
return "NULL";
}
String str = new String();
String delimiter = isNormal() ? "-" : "x";
assert(this.data.size() > 0);
// Concatenate all the cell numbers
str = str + this.data.elementAt(0);
for (int i = 1; i < this.data.size(); ++i) {
str = str + delimiter + this.data.elementAt(i);
}
return str;
}
/**
* Checks if the two objects represent the same move.
*/
public boolean equals(final Move otherMove) {
if (this.type != otherMove.type) {
return false;
}
if (this.data.size() != otherMove.data.size()) {
return false;
}
for (int i = 0; i < this.data.size(); ++i) {
if (this.data.elementAt(i) != otherMove.data.elementAt(i)) {
return false;
}
}
return true;
}
}