-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathState.java
More file actions
199 lines (180 loc) · 5.14 KB
/
State.java
File metadata and controls
199 lines (180 loc) · 5.14 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
public class State {
//0 means empty, 1 means black, 2 means white
public int[][] s, weight;
public State() {
s = new int[8][];
int i, j;
for(i = 0; i < 8; i++)
s[i] = new int[8];
for(i = 0; i < 8; i++) for(j = 0; j < 8; j++)
s[i][j] = 0;
weight = new int[8][];
for(i = 0; i < 8; i++)
weight[i] = new int[8];
for(i = 0; i < 8; i++)
for(j = 0; j < 8; j++)
weight[i][j] = 1;
for(i = 0;i < 8;i++)
{
weight[0][i] = 2;
weight[7][i] = 2;
weight[i][0] = 2;
weight[i][7] = 2;
}
weight[0][0] = 40;
weight[0][7] = 40;
weight[7][0] = 40;
weight[7][7] = 40;
}
public State(State an) {
this();
int i, j;
for(i = 0; i < 8; i++) for(j = 0; j < 8; j++)
s[i][j] = an.s[i][j];
}
public void init() {
int i, j;
for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) {
if((i == 3 || i == 4) && (j == 3 || j == 4)) {
if(i == j) s[i][j] = 2;
else s[i][j] = 1;
} else s[i][j] = 0;
}
}
//reverse 1 and 2 in s
public void reverse() {
int i, j;
for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) {
if(s[i][j] == 0) continue;
s[i][j] = 3 - s[i][j];
}
}
//calculate how many chesses we can change in the direction (r, c)
//assuming (x, y) is black
private int cal(int x, int y, int r, int c) {
int count = 0;
x += r; y += c;
while(x <= 7 && x >= 0 && y <= 7 && y >= 0) {
if(s[x][y] == 0) return 0;
if(s[x][y] == 1) break;
count++;
x += r;
y += c;
}
if(x < 0 || x > 7 || y < 0 || y > 7) return 0;
x -= r; y -= c;
while(s[x][y] == 2) {
s[x][y] = 1;
x -= r; y -= c;
}
return count;
}
public int calEmpty() {
int i, j, count = 0;
for(i = 0; i < 8; i++) for(j = 0; j < 8; j++)
if(s[i][j] == 0) count++;
return count;
}
private int cal1(int x, int y, int r, int c) {
int count = 0;
x += r; y += c;
while(x <= 7 && x >= 0 && y <= 7 && y >= 0) {
if(s[x][y] == 0) return 0;
if(s[x][y] == 1) break;
count += 2 * weight[x][y];
x += r;
y += c;
}
if(x < 0 || x > 7 || y < 0 || y > 7) return 0;
x -= r; y -= c;
while(s[x][y] == 2) {
s[x][y] = 1;
x -= r; y -= c;
}
return count;
}
//insert a chess in (x, y), if you can't insert return -1
//else return the number of change points
//when the function called, 's' will be changed
public int insert(int x, int y, int color) {
if(x < 0 || x > 7 || y < 0 || y > 7 || s[x][y] != 0) return -1;
int i, j;
boolean needReverse = color == 2;
if(needReverse) reverse();
s[x][y] = 1;
int tot = 0;
for(i = -1; i <= 1; i++) for(j = -1; j <= 1; j++) {
if(i == 0 && j == 0) continue;
tot += cal(x, y, i, j);
}
if(tot == 0) return -1;
if(needReverse) reverse();
return tot;
}
public int insert1(int x, int y, int color) {
if(x < 0 || x > 7 || y < 0 || y > 7 || s[x][y] != 0) return -1;
int i, j;
boolean needReverse = color == 2;
if(needReverse) reverse();
s[x][y] = 1;
int tot = 0;
for(i = -1; i <= 1; i++) for(j = -1; j <= 1; j++) {
if(i == 0 && j == 0) continue;
tot += cal1(x, y, i, j);
}
if(tot == 0) return -1;
if(needReverse) reverse();
tot += weight[x][y];
return tot;
}
//the same as insert
//but will not change 's'
public int test(int x, int y, int color) {
State an = new State();
int i, j;
for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) {
an.s[i][j] = s[i][j];
}
int val = an.insert(x, y, color);
// System.out.println("x: " + x + " y:" + y + " color:" + color + "val: " + val);
return val;
}
public int test1(int x, int y, int color) {
State an = new State();
int i, j;
for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) {
an.s[i][j] = s[i][j];
}
int val = an.insert1(x, y, color);
// System.out.println("x: " + x + " y:" + y + " color:" + color + "val: " + val);
return val;
}
public int judge(int color)
{
int tot = 0;
for(int i = 0;i < 8;i++)
for(int j = 0;j < 8;j++)
if(s[i][j]!=0)
{
if(s[i][j] == color)
tot++;
else
tot--;
}
return tot;
}
public int judge1(int color)
{
int tot = 0;
for(int i = 0;i < 8;i++)
for(int j = 0;j < 8;j++)
if(s[i][j]!=0)
{
if(s[i][j] == color)
tot += weight[i][j];
else
tot -= weight[i][j];
}
return tot;
}
}