-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPosition.java
More file actions
150 lines (127 loc) · 3.89 KB
/
Position.java
File metadata and controls
150 lines (127 loc) · 3.89 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
public class Position {
public static final int WIDTH = 7;
public static final int HEIGHT = 6;
public static final int MIN_SCORE = -(WIDTH * HEIGHT) / 2 + 3;
public static final int MAX_SCORE = (WIDTH * HEIGHT + 1) / 2 - 3;
public static class State {
final long current_position;
final long mask;
final int moves;
public State(long current_position, long mask, int moves) {
this.current_position = current_position;
this.mask = mask;
this.moves = moves;
}
}
public State save() {
return new State(current_position, mask, moves);
}
public void restore(State s) {
this.current_position = s.current_position;
this.mask = s.mask;
this.moves = s.moves;
}
private static final long[] TOP_MASK = new long[WIDTH];
private static final long[] BOTTOM_MASK = new long[WIDTH];
private static final long[] COLUMN_MASK = new long[WIDTH];
static {
for (int c = 0; c < WIDTH; c++) {
TOP_MASK[c] = (1L << (HEIGHT - 1)) << (c * (HEIGHT + 1));
BOTTOM_MASK[c] = 1L << (c * (HEIGHT + 1));
COLUMN_MASK[c] = ((1L << HEIGHT) - 1) << (c * (HEIGHT + 1));
}
}
private long current_position;
private long mask;
private int moves;
public Position() {
current_position = 0;
mask = 0;
moves = 0;
}
public Position(Position P) {
current_position = P.current_position;
mask = P.mask;
moves = P.moves;
}
public boolean playable(int col) {
return (mask & top_mask(col)) == 0;
}
public void play(int col) {
current_position ^= mask;
mask |= (mask + bottom_mask(col));
moves++;
}
public int play(String seq) {
for (int i = 0; i < seq.length(); i++) {
int col = Character.getNumericValue(seq.charAt(i)) - 1;
if (col < 0 || col >= WIDTH || !playable(col) || winsByPlaying(col)) {
return i;
}
play(col);
}
return seq.length();
}
public boolean winsByPlaying(int col) {
long pos = current_position;
pos |= (mask + bottom_mask(col)) & column_mask(col);
return alignment(pos);
}
public int getMoves() {
return moves;
}
public long key() {
return current_position + mask;
}
public long mirrorKey() {
long cp = current_position;
long m = mask;
long cpMir = 0L, mMir = 0L;
for (int c = 0; c < WIDTH; c++) {
int mc = WIDTH - 1 - c;
int shift = (mc - c) * (HEIGHT + 1);
long colMask = COLUMN_MASK[c];
long bitsCp = cp & colMask;
long bitsM = m & colMask;
if (shift > 0) {
cpMir |= bitsCp << shift;
mMir |= bitsM << shift;
} else if (shift < 0) {
cpMir |= bitsCp >>> -shift;
mMir |= bitsM >>> -shift;
} else {
cpMir |= bitsCp;
mMir |= bitsM;
}
}
return cpMir + mMir;
}
private boolean alignment(long pos) {
long m = pos & (pos >> (HEIGHT + 1));
if ((m & (m >> (2 * (HEIGHT + 1)))) != 0) {
return true;
}
m = pos & (pos >> HEIGHT);
if ((m & (m >> (2 * HEIGHT))) != 0) {
return true;
}
m = pos & (pos >> (HEIGHT + 2));
if ((m & (m >> (2 * (HEIGHT + 2)))) != 0) {
return true;
}
m = pos & (pos >> 1);
if ((m & (m >> 2)) != 0) {
return true;
}
return false;
}
private long top_mask(int col) {
return TOP_MASK[col];
}
private long bottom_mask(int col) {
return BOTTOM_MASK[col];
}
private long column_mask(int col) {
return COLUMN_MASK[col];
}
}