-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
88 lines (78 loc) · 3.63 KB
/
King.java
File metadata and controls
88 lines (78 loc) · 3.63 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
//imports-------
import java.util.*;
public class King extends Piece {
public King(int row,int col,boolean isWhite){
super(row, col, isWhite);
}
public void calculateMoves(Board b){
int[][] dir={{-1,-1},{-1,1},{1,-1},{1,1},{1,0},{0,1},{-1,0},{0,-1}};
for(int[] d:dir){
if(this.row+d[0]<8 && this.row+d[0]>=0 && this.col+d[1]<8 && this.col+d[1]>=0 && (b.getCell(this.row+d[0],this.col+d[1]).getPiece()==null || b.getCell(this.row+d[0],this.col+d[1]).getPiece().getIsWhite()!=this.isWhite)){
super.possibleMoves.add(new int[] {this.row+d[0], this.col+d[1]});
}
}
validateMoves(b);
}
public boolean isCheck(Board b){
//no need for that!!!
//run BFS to check if its check+ 8 moves for Knight and 2 for pawns.
//checking for top checks
//a=0 if vert/horiz , a=1 if diag a=2 if horse, a=3 if pawn
//a[0] is row a[1] is col a[2] is kind (diag/horiz/vert/horse/pawn)
int[][] dir={{-1,-1},{-1,1},{1,-1},{-1,-1},{1,0},{0,1},{-1,0},{0,-1}};
//first diagonal then vertical
Piece p;
for(int i=0;i<dir.length;i++) {
for(int r=this.row,c=this.col;r<8 && r>=0 && c<8 && c>=0;r+=dir[i][0],c+=dir[i][1]){
if((p=b.getCell(r,c).getPiece())!=null){
if(p.isWhite!=this.isWhite){//check if not our color
if(i<=3){
if(p instanceof Queen || p instanceof Bishop){
return true;
}
}else{
if(p instanceof Queen || p instanceof Rook){
return true;
}
}
}else{
//we can break cuz it blocks
break;
}
}
}
}
//check for knight and pawn checks:
//knight:
int[][] moves={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}};//taken from the knight class
//exact same algorithm, just reusing differently
for(int[] mov:moves){
if(this.row+mov[0]>=0 && this.row+mov[0]<8 && this.col+mov[1]>=0 && this.col+mov[1]<8){//checking if inside the board
if(b.getCell(this.row+mov[0],this.col+mov[1]).getPiece()!=null && //if there is a piece there
b.getCell(this.row+mov[0],this.col+mov[1]).getPiece() instanceof Knight &&//is this piece a Knight
b.getCell(this.row+mov[0], this.col+mov[1]).getPiece().getIsWhite() != this.isWhite){
return true;
}
}
}
//pawn:
if(this.row-1>=0 && this.col+1<8 && b.getCell(this.row-1, this.col+1).getPiece()!=null){//checking if there is a pawn infront on the right of the king
if(b.getCell(this.row-1, this.col+1).getPiece() instanceof Pawn && b.getCell(this.row-1, this.col+1).getPiece().getIsWhite() != this.isWhite){
return true;
}
}
if(this.row-1>=0 && this.col-1>=0 && b.getCell(this.row-1, this.col-1).getPiece()!=null){//checking if there is a pawn infront on the left of the king
if(b.getCell(this.row-1, this.col+1).getPiece() instanceof Pawn && b.getCell(this.row-1, this.col+1).getPiece().getIsWhite() != this.isWhite){
return true;
}
}
//if no checks found we return false.
return false;
}
@Override
public char toChar() {
return this.isWhite ? 'K' : 'k';
}
@Override
public void draw() {}
}