-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.java
More file actions
202 lines (171 loc) · 5.79 KB
/
Line.java
File metadata and controls
202 lines (171 loc) · 5.79 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
// Owen Gregson
// Artificial Intelligence
// TTT Checkpoint #3
// Dec 18, 2024
public class Line {
private long positions;
private String name;
private Line(String name) {
this.positions = 0;
this.name = name;
}
private Line(long positions) {
this.positions = positions;
this.name = "";
}
public long positions() {
// A bit vector containing the four positions
// making up this line on the board.
return this.positions;
}
public String name() {
// A descriptive name for this line.
return this.name;
}
public boolean contains(int position) {
return Bit.isSet(this.positions, position);
}
public boolean intersects(Line other) {
return (this.positions & other.positions) != 0;
}
public int intersection(Line other) {
if (this.intersects(other)) {
return Bit.leadingOne(this.positions & other.positions);
} else {
return -1;
}
}
public boolean equals(Line other) {
return this.positions == other.positions;
}
@Override
public boolean equals(Object other) {
if (other != null && other instanceof Line) {
return this.equals((Line) other);
} else {
return false;
}
}
@Override
public String toString() {
StringBuilder result = new StringBuilder("{");
String separator = "";
for (int position : Bit.ones(this.positions)) {
result.append(separator);
result.append(Math.abs(position));
separator = ", ";
}
result.append("}");
return result.toString();
}
// Private methods to construct the various lines on the board
private static enum Axis { X, Y, Z }
private static final int N = Coordinate.N;
private void set(int x, int y, int z) {
this.positions = Bit.set(this.positions, Coordinate.position(x, y, z));
}
private static Line Straight(Axis axis, int row, int column) {
String name = "Straight line: ";
switch (axis) {
case X: name += "Row: Y = " + row + " Z = " + column; break;
case Y: name += "Column: X = " + row + " Z = " + column; break;
case Z: name += "Pillar: X = " + row + " Y = " + column; break;
default: name += "???: row = " + row + " col = " + column; break;
}
Line line = new Line(name);
for (int i = 0; i < N; i++) {
switch (axis) {
case X: line.set(i, row, column); break;
case Y: line.set(row, i, column); break;
case Z: line.set(row, column, i); break;
}
}
return line;
}
private static Line ForwardDiagonal(Axis axis, int value) {
String name = "Forward diagonal: ";
switch (axis) {
case X: name += "YZ-Plane X = " + value; break;
case Y: name += "XZ-Plane Y = " + value; break;
case Z: name += "XY-Plane Z = " + value; break;
default: name += "??-Plane (" + value + ")"; break;
}
Line line = new Line(name);
for (int i = 0; i < N; i++) {
switch (axis) {
case X: line.set(value, i, i); break;
case Y: line.set(i, value, i); break;
case Z: line.set(i, i, value); break;
}
}
return line;
}
private static Line ForwardDiagonal() {
String name = "Main diagonal";
Line line = new Line(name);
for (int i = 0; i < N; i++) {
line.set(i, i, i);
}
return line;
}
private static Line ReverseDiagonal(Axis axis, int value) {
String name = "Reverse diagonal: ";
switch (axis) {
case X: name += "YZ-Plane X = " + value; break;
case Y: name += "XZ-Plane Y = " + value; break;
case Z: name += "XY-Plane Z = " + value; break;
default: name += "??-Plane (" + value + ")"; break;
}
Line line = new Line(name);
for (int i = 0; i < N; i++) {
switch (axis) {
case X: line.set(value, i, N-i-1); break;
case Y: line.set(i, value, N-i-1); break;
case Z: line.set(i, N-i-1, value); break;
}
}
return line;
}
private static Line ReverseDiagonal(Axis axis) {
String name = "Main diagonal: reverse";
switch (axis) {
case X: name += "X-Axis"; break;
case Y: name += "Y-Axis"; break;
case Z: name += "Z-Axis"; break;
}
Line line = new Line(name);
for (int i = 0; i < N; i++) {
switch (axis) {
case X: line.set(N-i-1, i, i); break;
case Y: line.set(i, N-i-1, i); break;
case Z: line.set(i, i, N-i-1); break;
}
}
return line;
}
// A list of all the possible lines on the board:
public static final Line[] lines = new Line[76];
static {
int count = 0;
for (Axis axis : Axis.values()) {
for (int row = 0; row < N; row++) {
for (int column = 0; column < N; column++) {
lines[count++] = Straight(axis, row, column);
}
}
for (int value = 0; value < N; value++) {
lines[count++] = ForwardDiagonal(axis, value);
lines[count++] = ReverseDiagonal(axis, value);
}
lines[count++] = ReverseDiagonal(axis);
}
lines[count++] = ForwardDiagonal();
assert count == 76;
}
public static Line find(long positions) {
for (Line line : lines) {
if (line.positions() == positions) return line;
}
return null;
}
}