-
Notifications
You must be signed in to change notification settings - Fork 1
/
King.java
170 lines (153 loc) · 6.66 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
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
import java.util.*;
import java.net.URL;
import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
public class King extends Piece {
//protected int pts;
protected boolean inCheck;
protected boolean gameOver;
protected int pts;
protected int relStrength = 900;
protected String pieceName = "K";
//protected Image image;
public King(int r, int c, boolean white, BoardLoc[][] board, String pieceName)
{
super(r, c, white, board, pieceName);
inCheck = false;
gameOver = false;
pts = 0; // I'm just making a point value because I will use it to determine if a piece is the King
if(white) {
try {
URL url = getClass().getResource("White_king_(simple)-1.png");
super.image = ImageIO.read(url);
} catch(Exception e) {
e.printStackTrace();
}
}
else {
try {
URL url = getClass().getResource("Black_king_(simple)-1.png");
super.image = ImageIO.read(url);
} catch(Exception e) {
e.printStackTrace();
}
}
}
public void generateValidMoves(BoardLoc[][] board) {
super.moves = new ArrayList<Move>();
int[] dr = {0, 1, 1, 1, 0, -1, -1, -1};
int[] dc = {1, 1, 0, -1, -1, -1, 0, 1};
for(int i = 0; i < dr.length; i++) { // iterate through possible moves
int nr = super.pos.r + dr[i];
int nc = super.pos.c + dc[i];
if(nr < board.length && nr >= 0 && nc < board[0].length && nc >= 0) { // makes sure it's still within the bounds of the board
if(white && board[nr][nc].whiteKingCanHere) { // checks if it's a safe spot (for white King)
if(board[nr][nc].piece == null) { // checks if tile is empty or contains enemy piece that can be taken
//moves.add(board[nr][nc]);
super.moves.add(new Move(this.pieceName, "", board[nr][nc], board[pos.r][pos.c], this.white));
}
if(!board[nr][nc].piece.white){
super.moves.add(new Move(this.pieceName, "x", board[nr][nc], board[pos.r][pos.c], this.white));
}
}
if(!white && board[nr][nc].blackKingCanHere) { // checks if it's a safe spot (for black King)
if(board[nr][nc].piece == null) { // checks if tile is empty or contains enemy piece that can be taken
//moves.add(board[nr][nc]);
super.moves.add(new Move(this.pieceName, "", board[nr][nc], board[pos.r][pos.c], this.white));
}
if(board[nr][nc].piece.white){
super.moves.add(new Move(this.pieceName, "x", board[nr][nc], board[pos.r][pos.c], this.white));
}
}
}
}
if(moves.size() == 0 && inCheck) { // checks for CHECKMATE (get dad-joked, Sandro would be proud)
gameOver = true;
}
// Note: I plan to handle stalemates in the Player class because stalemates depend on knowing the states of other pieces
}
public boolean isInCheck(Player opponent) {
/*for(int i = 0; i < opponent.pieces.size(); i++) { // I've commented this part out because I will need to edit it when the Move class is done
Piece p = opponent.pieces(i);
for(int j = 0; j < p.moves; j++) {
if(super.pos = p.moves.get(j)) {
return true;
}
}
}
return false;
*/
return false;
}//end
public void castle(BoardLoc[][] board, Rook rook) {
if(rook.timesMoved == 0 && super.timesMoved == 0) { // if both have not been moved before
if((rook.white && this.white) || (!rook.white && !this.white)) { // See, this is where I might use the sameColor method instead, but we'll see...
if(this.white) {
if(rook.pos.c < super.pos.c) { // white queen's side castle
BoardLoc b = super.pos;
// checks if the spaces are threatened by enemy pieces or occupied by pieces (whether friendly or enemy)
if(board[super.pos.r][super.pos.c - 2].whiteKingCanHere && board[super.pos.r][super.pos.c - 2].piece == null
&& board[super.pos.r][super.pos.c - 1].whiteKingCanHere && board[super.pos.r][super.pos.c - 1].piece == null) {
// move King
super.pos.piece = null;
super.pos = board[b.r][b.c - 2];
super.pos.piece = this;
// move Rook
rook.pos.piece = null;
rook.pos = board[rook.pos.r][rook.pos.c + 3];
rook.pos.piece = rook;
}
}
else { // white king's side castle
BoardLoc b = super.pos;
// checks if the spaces are threatened by enemy pieces or occupied by pieces (whether friendly or enemy)
if(board[super.pos.r][super.pos.c + 2].whiteKingCanHere && board[super.pos.r][super.pos.c + 2].piece == null
&& board[super.pos.r][super.pos.c + 1].whiteKingCanHere && board[super.pos.r][super.pos.c + 1].piece == null) {
// move King
super.pos.piece = null;
super.pos = board[b.r][b.c + 2];
super.pos.piece = this;
// move Rook
rook.pos.piece = null;
rook.pos = board[rook.pos.r][rook.pos.c - 2];
rook.pos.piece = rook;
}
}
}
else {
if(rook.pos.c < super.pos.c) { // black king's side castle
BoardLoc b = super.pos;
// checks if the spaces are threatened by enemy pieces or occupied by pieces (whether friendly or enemy)
if(board[super.pos.r][super.pos.c - 2].blackKingCanHere && board[super.pos.r][super.pos.c - 2].piece == null
&& board[super.pos.r][super.pos.c - 1].blackKingCanHere && board[super.pos.r][super.pos.c - 1].piece == null) {
// move King
super.pos.piece = null;
super.pos = board[b.r][b.c - 2];
super.pos.piece = this;
// move Rook
rook.pos.piece = null;
rook.pos = board[rook.pos.r][rook.pos.c + 2];
rook.pos.piece = rook;
}
}
else { // black queen's side castle
BoardLoc b = super.pos;
// checks if the spaces are threatened by enemy pieces or occupied by pieces (whether friendly or enemy)
if(board[super.pos.r][super.pos.c + 2].blackKingCanHere && board[super.pos.r][super.pos.c + 2].piece == null
&& board[super.pos.r][super.pos.c + 1].blackKingCanHere && board[super.pos.r][super.pos.c + 1].piece == null) {
// move King
super.pos.piece = null;
super.pos = board[b.r][b.c + 2];
super.pos.piece = this;
// move Rook
rook.pos.piece = null;
rook.pos = board[rook.pos.r][rook.pos.c - 3];
rook.pos.piece = rook;
}
}
}
}
}
}
}