-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKing.java
69 lines (66 loc) · 2.73 KB
/
King.java
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
import javax.swing.*;
import java.awt.*;
public class King extends Piece {
//A child class of Piece, overrides the Piece values with the specific child piece based info
//Such as the value of the piece, image, encoded value in boardGrid, and maxMoves
public King(int colour) {
this.colour = colour;
code = 6;
maxMoves = 1;
moveSet = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
if (colour == 0) {
value = 900;
img = new ImageIcon("Sprites/W_King.png").getImage();
}
else {
value = -900;
img = new ImageIcon("Sprites/B_King.png").getImage();
}
}
//A static method that finds the position of the given colour's King in a board
//If not found, returns null
public static Point findKing(int colour, int[][] board) {
int objective = colour * 10 + 6;
for (int row = 0; row < 8; row++) {
for (int column = 0; column < 8; column++) {
if (board[row][column] == objective) {
return new Point(column, row);
}
}
}
return null;
}
//A static method that checks if there are piece between the rook and the corresponding king if castling is available
//If the conditions met castling then just mark the position in possibleMove grid for castling as true
public static void kingCastleMove(Point kingPos, int[][] boardGrid, boolean[][] possibleMove, boolean[][] castle) {
if (!Board.check(boardGrid, kingPos)) {
for (int index = 0; index < 2; index++) {
boolean[] curCastle = castle[index];
int y = index == 0 ? 7 : 0;
if (kingPos.y != y) {continue;}
if (curCastle[1]) {
if (curCastle[0]) {
boolean flag = true;
for (int i = 3; i >= 1; i--) {
if (boardGrid[y][i]%10 != 0) {
flag = false;
break;
}
}
if (flag) {possibleMove[y][2] = true;}
}
if (curCastle[2]) {
boolean flag = true;
for (int i = 5; i < 7; i++) {
if (boardGrid[y][i]%10 != 0) {
flag = false;
break;
}
}
if (flag) {possibleMove[y][6] = true;}
}
}
}
}
}
}