-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathking.java
More file actions
106 lines (92 loc) · 2.58 KB
/
king.java
File metadata and controls
106 lines (92 loc) · 2.58 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
/*
* moves determines how the king can move, as well as castling. Castling rights are in Board.
* kingSafe determines if the king is in check (legal move)
* Position is stored as integer kingPos and kpTemp for speed
*/
public class King
{
public static int kingPosW = 60, kingPosB = 4;
public static boolean kingSafeW()
{
BB.refreshMoves();
if(BB.controlB()[kingPosW])
return false;
else
return true;
}
public static boolean kingSafeB()
{
BB.refreshMoves();
if(BB.controlW()[kingPosB])
return false;
else
return true;
}
// ~~MOVES~~
public static String movesW(int i) {
String move = "", oldPiece;
int r = i / 8, c = i % 8;
int kpTemp = 0;
for (int j = 0; j < 9; j++) {
if (j != 4) {
try {
String newPos = Board.chessBoard[r - 1 + j / 3][c - 1 + j % 3];
if (Character.isLowerCase(newPos.charAt(0)) || " ".equals(newPos)) {
oldPiece = newPos;
Board.makeMove(""+r + c + (r - 1 + j / 3) + (c - 1 + j % 3) + oldPiece);
kpTemp = kingPosW;
kingPosW = (r - 1 + j / 3)*8 + (c - 1 + j % 3);
if(King.kingSafeW())
move = ""+move + r + c + (r - 1 + j / 3) + (c - 1 + j % 3) + oldPiece;
Board.undoMove(""+r + c + (r - 1 + j / 3) + (c - 1 + j % 3) + oldPiece);
kingPosW = kpTemp;
}
} catch (Exception e) {}
}
}
return move;
}
public static String movesB(int i) {
String move = "", oldPiece;
int r = i / 8, c = i % 8;
int kpTemp = 0;
for (int j = 0; j < 9; j++) {
if (j != 4) {
try {
String newPos = Board.chessBoard[r - 1 + j / 3][c - 1 + j % 3];
if (Character.isUpperCase(newPos.charAt(0)) || " ".equals(newPos)) {
oldPiece = newPos;
kpTemp = kingPosB;
Board.makeMove(""+r + c + (r - 1 + j / 3) + (c - 1 + j % 3) + oldPiece);
kingPosB = (r - 1 + j / 3)*8 + (c - 1 + j % 3);
if(King.kingSafeB())
move = ""+move + r + c + (r - 1 + j / 3) + (c - 1 + j % 3) + oldPiece;
Board.undoMove(""+r + c + (r - 1 + j / 3) + (c - 1 + j % 3) + oldPiece);
kingPosB = kpTemp;
}
} catch (Exception e) {}
}
}
return move;
}
public static void controlW(int i) {
int r = i / 8, c = i % 8;
for (int j = 0; j <= 9; j++) {
if (j != 9) {
try {
BB.kMovesW[(r - 1 + j / 3) * 8 + (c - 1 + j % 3)] = true;
} catch (Exception e) {}
}
}
}
public static void controlB(int i) {
int r = i / 8, c = i % 8;
for (int j = 0; j <= 9; j++) {
if (j != 9) {
try {
BB.kMovesB[(r - 1 + j / 3) * 8 + (c - 1 + j % 3)] = true;
} catch (Exception e) {}
}
}
}
}