-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
264 lines (243 loc) · 7.53 KB
/
Copy pathRook.java
File metadata and controls
264 lines (243 loc) · 7.53 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
class Rook extends Piece{
public String name;
protected String cPos;
Rook(Board brd, int i, int j, boolean col){
this.board = brd;
this.let = i;
this.num = j;
this.side = col;
this.legalMoves = new int[14][4];
this.protects = new int[4][2];
this.initialPos = true;
this.name = "White Rook";
if(col == false)
this.name = "Black King";
}
public String toString(){
String ret = this.name+" "+this.let+" "+this.num;
return ret;
}
public void moves(){
int[][] moves = new int[14][4];
int moveC = 0;
int[][] protecs = new int[4][2];
int proC = 0;
// System.out.println("Finding moves");
// System.out.println("initial array = " + moves);
// System.out.println("let, num = " + this.let+ ", " + this.num);
//--------------- Horizontal ----------
for(int i = this.let+1; i <= 7; i++){
// System.out.println("going right i = " + i);
if(this.board.squareIsEmpty(i, this.num) == true){
moves[moveC][2] = i;
moves[moveC][3] = this.num;
moveC++;
} else if((this.board.pieceAtPos(i, this.num).getSide() != this.side)){
moves[moveC][2] = i;
moves[moveC][3] = this.num;
moveC++;
break;
} else {
protecs[proC][0] = i;
protecs[proC][1] = this.num;
proC++;
break;
}
}
for(int i = this.let-1; i >= 0; i--){
// System.out.println("going left i = " + i);
if(this.board.squareIsEmpty(i, this.num) == true){
moves[moveC][2] = i;
moves[moveC][3] = this.num;
moveC++;
} else if((this.board.pieceAtPos(i, this.num).getSide() != this.side)){
moves[moveC][2] = i;
moves[moveC][3] = this.num;
moveC++;
break;
} else {
protecs[proC][0] = i;
protecs[proC][1] = this.num;
proC++;
break;
}
}
//-------------- Vertical------------
for(int n = this.num+1; n <= 7; n++){
// System.out.println("going up n = " + n);
if(this.board.squareIsEmpty(this.let, n) == true){
moves[moveC][2] = this.let;
moves[moveC][3] = n;
moveC++;
} else if((this.board.pieceAtPos(this.let,n).getSide() != this.side)){
moves[moveC][2] = this.let;
moves[moveC][3] = n;
moveC++;
break;
} else {
// System.out.println("color of checking piece + "+this.board.pieceAtPos(this.let,n).getSide()+ " result "+(this.board.pieceAtPos(this.let,n).getSide() != this.side));
protecs[proC][0] = this.let;
protecs[proC][1] = n;
proC++;
break;
}
}
for(int n = this.num-1; n >= 0; n--){
// System.out.println("going down n = " + n);
if(this.board.squareIsEmpty(this.let, n) == true){
moves[moveC][2] = this.let;
moves[moveC][3] = n;
moveC++;
} else if((this.board.pieceAtPos(this.let,n).getSide() != this.side)){
moves[moveC][2] = this.let;
moves[moveC][3] = n;
moveC++;
break;
} else {
protecs[proC][0] = this.let;
protecs[proC][1] = n;
proC++;
break;
}
}
for(int j = 0; j < moveC; j++){
moves[j][0] = this.let;
moves[j][1] = this.num;
}
this.protects = protecs;
this.legalMoves = moves;
}//end moves
public boolean attacksSquare(int x, int y){
if(!(this.board.squareIsEmpty(x,y))){
if(this.board.pieceAtPos(x,y).getSide() == side){
return false;
}
}
if((x != this.let) && (y != this.num)){
return false;
} else if(x == this.let){
if(y > this.num){
for(int i = 1; i < y-this.num; i++){
if(!(this.board.squareIsEmpty(this.let, this.num+i))){
return false;
}
}
return true;
} else {
for(int i = 1; i < this.num-y; i++){
// System.out.println("let, num, x, y, i " + this.let+", "+this.num+", "+x+", "+y+", "+i);
if(!(this.board.squareIsEmpty(this.let, this.num-i))){
// System.out.println("returning false + let, num, x, y, i" + this.let+", "+this.num+", "+x+", "+y+", "+i);
return false;
}
}
return true;
}
} else {
if(x > this.let){
for(int i = 1; i < x-this.let; i++){
if(!(this.board.squareIsEmpty(this.let+i, this.num))){
return false;
}
}
return true;
} else {
for(int i = 1; i < this.let-x; i++){
if(!(this.board.squareIsEmpty(this.let-i, this.num))){
return false;
}
}
return true;
}
}
}
//kX, kY is King square, aX, aY is attacking Piece
public void checkMoves(int kX, int kY, int aX, int aY){
int[][] moves = new int[16][4];
int moveC = 0;
int interC = 0;
int[][] interposeSquares = new int[8][2];
if((aX == kX) || (aY == kY)){
if(aX > kX){
for(int i = 0; i < aX-kX; i++){
interposeSquares[interC][0] = aX-i;
interposeSquares[interC][1] = kY;
interC++;
}
} else if(aX < kX) {
for(int i = 0; i < kX-aX; i++){
interposeSquares[interC][0] = kX+i;
interposeSquares[interC][1] = kY;
interC++;
}
} else if(aY > kY){
for(int i = 0; i < aY-kY; i++){
interposeSquares[interC][0] = kX;
interposeSquares[interC][1] = kY+i;
interC++;
}
} else if(aY < kY){
for(int i = 0; i < kY-aY; i++){
interposeSquares[interC][0] = kX;
interposeSquares[interC][1] = kY+i;
interC++;
}
} else if((aX > kX) && (aY > kY)){
for(int i = 1; ((i+kY) <= aY) && ((i+kX) <= aX); i++){
interposeSquares[i][0] = kX+i;
interposeSquares[i][1] = kY+i;
interC++;
}
} else if((aX < kX) && (aY < kY)){
for(int i = 1; ((kY-i) >= aY) && ((kX-i) >= aX); i++){
interposeSquares[i][0] = kX-i;
interposeSquares[i][1] = kY-i;
interC++;
}
} else if((aX < kX) && (aY > kY)){
for(int i = 1; ((kY-i) <= aY) && ((i+kX) >= aX); i++){
interposeSquares[i][0] = kX+i;
interposeSquares[i][1] = kY-i;
interC++;
}
}
else if((aX > kX) && (aY < kY)){
for(int i = 1; ((kY+i) >= aY) && ((kX-i) <= aX); i++){
interposeSquares[i][0] = kX-i;
interposeSquares[i][1] = kY+i;
interC++;
}
}
}
for(int n = 0; (interposeSquares[n][0] != 0) || (interposeSquares[n][1] != 0); n++){
if(attacksSquare(interposeSquares[n][0], interposeSquares[n][1])){
moves[moveC][2] = interposeSquares[n][0];
moves[moveC][3] = interposeSquares[n][1];
moveC++;
}
}
for(int j = 0; j < moveC; j++){
moves[j][0] = this.let;
moves[j][1] = this.num;
}
ArrayTools.println(moves);
this.legalMoves = moves;
}//end checkMoves
public void display(){
double square = (this.board.getWidth()-this.board.getX())/8;
double centerPoint = square/2;
if(this.side == true){
StdDraw.picture(centerPoint+square*let,centerPoint+square*num,"images/rookW.png", square, square);
} else {
StdDraw.picture(centerPoint+square*let,centerPoint+square*num,"images/rookB.png", square, square);
}
} //end display
public boolean protectsPiece(int a, int b){
for(int i = 0; i < protects.length;i++){
if((protects[i][0] == a) && (protects[i][1] == b)){
return true;
}
}
return false;
}//end protects pieces
}//end class