-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMove.java
168 lines (143 loc) · 5.06 KB
/
Move.java
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
package ataxx;
import static java.lang.Math.abs;
import static ataxx.GameException.error;
/** Represents an Ataxx move. There is one Move object created for
* each distinct Move.
* @author Aaron Lee
*/
class Move {
/* Moves get generated profligately during the calculations of an AI,
* so it's a good idea to make that operation efficient. Instead of
* relying on a Move constructor, which does a memory allocation with
* each use of 'new', we use a "Move factory": a static method that
* returns a Move, but not necessarily a new one. Moves themselves
* are immutable, and for any possible move, there is exactly one
* object of type Move. */
/** The move COL0 ROW0 - COL1 ROW1. This must be a legal move. */
Move(int col0, int row0, int col1, int row1) {
_col0 = (char) (col0 + 'a' - 2);
_row0 = (char) (row0 + '1' - 2);
_col1 = (char) (col1 + 'a' - 2);
_row1 = (char) (row1 + '1' - 2);
_fromIndex = row0 * EXTENDED_SIDE + col0;
_toIndex = row1 * EXTENDED_SIDE + col1;
}
/** A pass. */
Move() {
_col0 = _col1 = _row0 = _row1 = 0;
_fromIndex = _toIndex = -1;
}
/** A factory method that returns a Move from COL0 ROW0 to COL1 ROW1,
* assuming the column and row designations are valid. Returns null
* if no such move is ever possible because it is more than 2 squares
* in some direction. The moves are on the extended board (i.e., they
* may go into the border layers). */
static Move move(char col0, char row0, char col1, char row1) {
if (!(col0 >= 'a' && row0 >= '1'
&& col1 >= 'a' - 2 && row1 >= '1' - 2)) {
throw error("Illegal move");
} else {
return ALL_MOVES[col0 - 'a' + 2][row0 - '1' + 2]
[col1 - 'a' + 2][row1 - '1' + 2];
}
}
/** Returns a pass. */
static Move pass() {
return PASS;
}
/** Return true iff I am a pass. */
boolean isPass() {
return _toIndex == -1;
}
/** Return true if this is an extension (move to adjacent square). */
boolean isExtend() {
return (abs(_fromIndex % 11 - _toIndex % 11) <= 1
&& abs(_fromIndex - _toIndex) <= 12);
}
/** Return true if thIs is a jump (move to adjacent square). */
boolean isJump() {
return (abs(_fromIndex % 11 - _toIndex % 11) == 2
|| abs(_fromIndex - _toIndex) >= ab
&& abs(_fromIndex - _toIndex) <= ba);
}
/** Returns from column. Undefined if a pass. */
char col0() {
return _col0;
}
/** Returns from row. Undefined if a pass. */
char row0() {
return _row0;
}
/** Returns to column. Undefined if a pass. */
char col1() {
return _col1;
}
/** Returns to row. Undefined if a pass. */
char row1() {
return _row1;
}
/** Return the linearized index of my 'from' square,
* or -1 if I am a pass. */
int fromIndex() {
return _fromIndex;
}
/** Return The linearized index of my 'to' square,
* or -1 if I am a pass. */
int toIndex() {
return _toIndex;
}
/** @return Magic number. */
int getAb() {
return ab;
}
/** @return Magic number. */
int getBa() {
return ba;
}
@Override
public String toString() {
if (isPass()) {
return "-";
} else {
String c0 = Character.toString(_col0);
String r0 = Character.toString(_row0);
String c1 = Character.toString(_col1);
String r1 = Character.toString(_row1);
return c0 + r0 + "-" + c1 + r1;
}
}
/** Size of a side of the board. */
static final int SIDE = 7;
/** Size of side of a board plus 2-deep boundary. */
static final int EXTENDED_SIDE = SIDE + 4;
/** The pass. */
static final Move PASS = new Move();
/** Linearized indices. */
private final int _fromIndex, _toIndex;
/** From and two squares, or 0s if a pass. */
private char _col0, _row0, _col1, _row1;
/** Magic number. */
private final int ba = 23;
/** Magic number. */
private final int ab = 21;
/** The set of all Moves other than pass, indexed by from and to row and
* column positions. */
private static final Move[][][][] ALL_MOVES =
new Move[EXTENDED_SIDE][EXTENDED_SIDE][EXTENDED_SIDE][EXTENDED_SIDE];
/* A "static initializer". These code sections are run when the class
* is initialized and are intended to initialize static variables. */
static {
for (int c = 2; c < SIDE + 2; c += 1) {
for (int r = 2; r < SIDE + 2; r += 1) {
for (int dc = -2; dc <= 2; dc += 1) {
for (int dr = -2; dr <= 2; dr += 1) {
if (dc != 0 || dr != 0) {
ALL_MOVES[c][r][c + dc][r + dr] =
new Move(c, r, c + dc, r + dr);
}
}
}
}
}
}
}