-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck.java
More file actions
70 lines (64 loc) · 2.69 KB
/
Check.java
File metadata and controls
70 lines (64 loc) · 2.69 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
public class Check {
public static int[] findKing(Empty[][] map, int color) {
int[] kingLocation = new int[2];
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[i].length; j++) {
if(map[i][j].color == color && map[i][j].symbols[0] == "♔") {
kingLocation[0] = j;
kingLocation[1] = i;
return kingLocation;
}
}
}
return kingLocation;
}
public static boolean checkForCheck(Empty[][] map, int color) {
int[] kingLocation = findKing(map, color); // { 4, 0 }
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map[i].length; j++) {
if(map[i][j].color == (color * -1)) {
int[] ji = { j, i };
boolean canTake = map[i][j].move(map, kingLocation, ji);
if(canTake) {
return true;
}
}
}
}
return false;
}
public static boolean checkForMate(Empty[][] map, int color) {
Empty[][] fullProto = new Empty[map.length][];
for(int a = 0; a < map.length; ++a) {
fullProto[a] = new Empty[map[a].length];
System.arraycopy(map[a], 0, fullProto[a], 0, map[a].length);
} // create a prototype map
//int[] kingLocation = findKing(map, color); // find the targeted king
//checks every possible move on the board to see if it prevents check
for(int i = 0; i < fullProto.length; i++) {
for(int j = 0; j < fullProto[i].length; j++) {
if(fullProto[i][j].color == color) {
Empty selected = fullProto[i][j];
int[] started = { j, i };
for(int k = 0; k < fullProto.length; k++) {
for(int l = 0; l < fullProto[k].length; l++) {
int[] targeted = { l, k };
if(selected.move(fullProto, targeted, started)) {
Empty saved = fullProto[k][l];
fullProto[k][l] = selected;
fullProto[i][j] = new Empty(0);
boolean inCheck = checkForCheck(fullProto, color);
fullProto[k][l] = saved;
fullProto[i][j] = selected;
if(!inCheck) {
return false;
}
}
}
}
}
}
}
return true;
}
}