-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pawn.java
311 lines (248 loc) · 12.2 KB
/
Pawn.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ChessGame;
/**
*
* @author Alizey
*/
class Pawn extends Piece{
public Pawn(boolean colour, boolean alive){
this.colour = colour;
this.alive = alive;
}
/*
The move method is supposed to move the chess piece in a particular way
abiding by the piece's behaviour.
The type
*/
@Override
/*Methods still not made so as to differentiate the moves for the black and white pieces, have to work on it too!*/
/* For the move method got to determine when some of th epieces comeinthe way or not!*/
void move(int inirow, int inicolumn, int firow, int ficolumn){
boolean check = true;
Piece.che = false;
// Ensuring that in white's turn, white pieces are only move and in black's chance only black pieces move!
if((Chessboard.chance%2 == 0 && colour == true) || (Chessboard.chance%2 == 1 && colour == false)){
//System.out.println("Hi1");
if(inirow < 8 && inicolumn < 8 && inirow >= 0 && inicolumn >= 0 &&(firow < 8 && ficolumn < 8 && firow >= 0 && ficolumn >= 0) ){
// System.out.println("Hi2");
if(Chessboard.boxes[firow][ficolumn] == null){
//System.out.println("Hi3");
//System.out.println("Hi4");
if((firow - inirow) == 1 && ficolumn - inicolumn == 0 && colour == true){
//System.out.println("Hi5");
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
if(check(firow, ficolumn) == true){
System.out.println("Opponent move your King! Check!");
}
}
else if((firow - inirow) == -1 && ficolumn - inicolumn == 0 && colour == false){
//System.out.println("Hi6");
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
if(check(firow, ficolumn) == true){
System.out.println("Opponent move your King! Check!");
}
}
else if((firow - inirow) == 2 && ficolumn - inicolumn == 0 && colour == true && inirow == 1){
for(int i = inirow+1; i <= firow; i++){
if(Piece.che == false){
if(Chessboard.boxes[i][inicolumn] != null){
Piece.che = true;
//System.out.println("Sad");
break;
}
}
else
break;
}
for(int i = inirow-1; i >= firow; i--){
if(Piece.che == false){
if(Chessboard.boxes[i][inicolumn] != null){
Piece.che = true;
break;
}
}
else
break;
}
if(Piece.che == false){
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
if(check(firow, ficolumn) == true){
System.out.println("Opponent move your King! Check!");
}
}
else
check = false;
}
else if((firow - inirow) == -2 && ficolumn - inicolumn == 0 && colour == false && inirow == 6){
for(int i = inirow+1; i <= firow; i++){
if(Piece.che == false){
if(Chessboard.boxes[i][inicolumn] != null){
Piece.che = true;
//System.out.println("Sad");
break;
}
}
else
break;
}
for(int i = inirow-1; i >= firow; i--){
if(Piece.che == false){
if(Chessboard.boxes[i][inicolumn] != null){
Piece.che = true;
break;
}
}
else
break;
}
if(Piece.che == false){
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
if(check(firow, ficolumn) == true){
System.out.println("Opponent move your King! Check!");
}
}
else
check = false;
}
else
check = false;
}
else{
if(Chessboard.boxes[firow][ficolumn] != null){
//System.out.println("Hey1");
if(inirow == firow && inicolumn == ficolumn){
check = false;
}
else{
//if(inirow != firow && inicolumn != ficolumn){
if((firow - inirow) == 1 && Math.abs(ficolumn - inicolumn) == 1 && colour == true){
// The kill method is not yet made, will implement it in future submissions!
if(Chessboard.boxes[inirow][inicolumn].colour != Chessboard.boxes[firow][ficolumn].colour){
kill((Piece)Chessboard.boxes[firow][ficolumn], firow, ficolumn);
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
if(check(firow, ficolumn) == true){
System.out.println("Opponent move your King! Check!");
}
}
else
check = false;
}
else if((firow - inirow) == -1 && Math.abs(ficolumn - inicolumn) == 1 && colour == false){
if(Chessboard.boxes[inirow][inicolumn].colour != Chessboard.boxes[firow][ficolumn].colour){
kill((Piece)Chessboard.boxes[firow][ficolumn], firow, ficolumn);
Chessboard.boxes[firow][ficolumn] = Chessboard.boxes[inirow][inicolumn];
Chessboard.boxes[inirow][inicolumn] = null;
if(check(firow, ficolumn) == true){
System.out.println("Opponent move your King! Check!");
}
}
else
check = false;
}
else
check = false;
//}
}
}
}
}
else
check = false;
}
else
check = false;
if(check == false)
System.out.println("Wrong inputs for move!");
else
Chessboard.chance++;
//System.out.println(Chessboard.boxes[firow][ficolumn].getClass());
}
/*
The kill method as its name suggests kills the piece on which it is called
upon, sends the piece to the graveyard! This method is same for every piece
actually.
*/
@Override
void kill(Piece p, int row, int column){
p.alive = false;
Chessboard.graveyard[Chessboard.gravecounter] = p;
Chessboard.boxes[row][column] = null;
Chessboard.gravecounter++;
}
/*
This method is only used by the Pawn for when it reacher the base of the
base of the opponent to respawn a player's lost pieces.
*/
void revive(Piece p){}
/*
This method is called upon after the move method is called upon and tells
whether the opponent's king is under siege or not. If this returns as true
then the move method of the opponent's king is forced into play.
inirow and inicolumn sends the present position of the piece into the method.
*/
@Override
boolean check(int inirow, int inicolumn){
Chessboard.krow = 0;
Chessboard.kcolumn = 0;
boolean ch = true;
int i, j;
boolean col = this.colour;
// colour of opponent's king
col = !col;
King k = new King(col, true);
//System.out.println(col);
for(i = 0; i < 8; i++){
for(j = 0; j < 8; j++){
//System.out.println("Hi");
if(Chessboard.boxes[i][j] != null){
if(Chessboard.boxes[i][j].getClass() == k.getClass() && Chessboard.boxes[i][j].colour == k.colour){
//System.out.println("Hi");
Chessboard.krow = i;
Chessboard.kcolumn = j;
break;
}
}
}
}
//System.out.println(Chessboard.krow +" "+ Chessboard.kcolumn);
if((Chessboard.krow - inirow) == 1 && Math.abs(Chessboard.kcolumn - inicolumn) == 1){
// The kill method is not yet made, will implement it in future submissions!
if(Chessboard.boxes[inirow][inicolumn].colour != Chessboard.boxes[Chessboard.krow][Chessboard.kcolumn].colour){
ch = true;
}
else
ch = false;
}
else if((Chessboard.krow - inirow) == -1 && Math.abs(Chessboard.kcolumn - inicolumn) == 1 && colour == false){
if(Chessboard.boxes[inirow][inicolumn].colour != Chessboard.boxes[Chessboard.krow][Chessboard.kcolumn].colour){
ch = true;
}
else
ch = false;
}
else
ch = false;
return ch;
}
/*
When a checkmate happens, that is, when check and kill method is called
simultaneously then finally the checkMate method is called to end the game itself.
*/
void checkMate(King k){}
@Override
public String toString(){
if(colour == true)
return "wP";
else
return "bP";
}
}