Repository navigation
Expand file tree
/
Copy pathChessFrame.java
More file actions
439 lines (418 loc) · 16.6 KB
/
Copy pathChessFrame.java
File metadata and controls
439 lines (418 loc) · 16.6 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
class Move{
String from,to,event,details;
public Move( String a , String b , String c , String d ){
from=a; to=b; event=c; details=d;
}
}
public class ChessFrame extends JFrame implements MouseListener{
public static final int SQUARE_WIDTH = 55;
public static final int BOARD_MARGIN = 70;
int selectedSquareX = -1;
int selectedSquareY = -1;
boolean turn=false;
static Piece pieces[][] = new Piece[8][8];
Stack <Move> st = new Stack <Move>();
public ChessFrame(){
initializeChessBoard();
setTitle("Chess Game");
//let the screen size fit the board size
setSize(SQUARE_WIDTH*8+BOARD_MARGIN*2, SQUARE_WIDTH*9+BOARD_MARGIN*2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel undoPanel= new JPanel();
JPanel savePanel= new JPanel();
JButton undoButton = new JButton("undo");
JButton saveButton = new JButton("save");
undoButton.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
undo();
}
});
saveButton.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
save("game.txt");
} catch (Exception e1) {}
}
});
undoPanel.add( undoButton );
savePanel.add( saveButton );
this.add( undoPanel );
this.add( savePanel );
undoPanel.setBounds( 140 , 535 , 100 , 100 );
savePanel.setBounds( 340 , 535 , 100 , 100 );
this.getContentPane().setLayout(null);
addMouseListener(this);
}
public void initializeChessBoard(){
for(int i = 0; i<8; i++)
for(int j = 0; j<8; j++){
if( j == 0 || j == 7 ){
if( i == 0 || i == 7 ) pieces[i][j] = new Rook( i , j , j == 0 );
if( i == 1 || i == 6 ) pieces[i][j] = new Knight( i , j , j == 0 );
if( i == 2 || i == 5 ) pieces[i][j] = new Bishop( i , j , j == 0 );
if( i == 3 ) pieces[i][j] = new Queen( i , j , j == 0 );
if( i == 4 ) pieces[i][j] = new King( i , j , j == 0 );
}
else if(j == 1 || j == 6 ) pieces[i][j] = new Pawn( i , j , j == 1 );
else pieces[i][j] = null;
}
}
public boolean castling( boolean isKingSide ){
int tempY=7;
String king="white-king";
String rook="white-rook";
System.out.println(""+isKingSide);
if( turn ){
tempY=0;
king="black-king";
rook="black-rook";
}
if( pieces[4][tempY] == null || !pieces[4][tempY].getName().equals(king) ) return false;
if( isKingSide ){
if( pieces[7][tempY] == null || !pieces[7][tempY].getName().equals(rook) || pieces[5][tempY] != null || pieces[6][tempY] != null ) return false;
pieces[7][tempY]=pieces[4][tempY]=null;
pieces[6][tempY]=new King( 6 , tempY , turn );
pieces[5][tempY]=new Rook( 5 , tempY , turn );
st.push( new Move( "a1" , "a1" , "castling" , "kingside") );
}else{
if( pieces[0][tempY] == null || !pieces[0][tempY].getName().equals(rook) || pieces[1][tempY] != null || pieces[2][tempY] != null || pieces[3][tempY] != null ) return false;
pieces[0][tempY]=pieces[4][tempY]=null;
pieces[2][tempY]=new King( 2 , tempY , turn );
pieces[3][tempY]=new Rook( 3 , tempY , turn );
st.push( new Move( "a1" , "a1" , "castling" , "queenside") );
}
repaint();
turn=!turn;
return true;
}
public boolean isInCheck(){
String king="white-king";
if( turn ) king="black-king";
int curX=0,curY=0;
for( int i=0 ; i<8 ; i++ )
for( int j=0 ; j<8 ; j++ ) if( pieces[i][j]!=null && pieces[i][j].getName().equals(king) ){ curX=i; curY=j; break; }
for( int i=0 ; i<8 ; i++ )
for( int j=0 ; j<8 ; j++ )
if( pieces[i][j]!=null && pieces[i][j].getColor() == !turn ){
int diffX=curX-i;
int diffY=curY-j;
if( pieces[i][j].canCapture(diffX, diffY) ) return true;
}
return false;
}
public boolean isStalemate(){
boolean ret=true;
if( isInCheck() ) return false;
for( int i=0 ; i<8 ; i++ )
for( int j=0 ; j<8 ; j++ ) if( pieces[i][j] != null && pieces[i][j].getColor() == turn ){
for( int x=0 ; x<8 ; x++ )
for( int y=0 ; y<8 ; y++ ){
boolean cando=executeMove( ""+(char)('a'+i)+(char)('0'+8-j) , ""+(char)('a'+x)+(char)('0'+8-y) );
if( cando ) turn=!turn;
boolean notsafe=isInCheck();
if( cando ) turn=!turn;
if( cando ) undo();
if( !notsafe ) return false;
}
}
return ret;
}
public boolean isCheckmate(){
if( !isInCheck() ) return false;
for( int i=0 ; i<8 ; i++ )
for( int j=0 ; j<8 ; j++ ) if( pieces[i][j] != null && pieces[i][j].getColor() == turn ){
for( int x=0 ; x<8 ; x++ )
for( int y=0 ; y<8 ; y++ ){
boolean cando=executeMove( ""+(char)('a'+i)+(char)('0'+8-j) , ""+(char)('a'+x)+(char)('0'+8-y) );
if( cando ) turn=!turn;
boolean notsafe=isInCheck();
if( cando ) turn=!turn;
if( cando ) undo();
if( !notsafe ){
System.out.println(""+(char)('a'+i)+(char)('0'+8-j)+","+(char)('a'+x)+(char)('0'+8-y));
return false;
}
}
}
return true;
}
public void undo(){
if( st.isEmpty() ) return;
System.out.println("in undo");
turn=!turn;
Move thisMove=st.pop();
int prevSquareX=thisMove.from.charAt(0)-'a';
int prevSquareY=8-(thisMove.from.charAt(1)-'0');
int nextSquareX=thisMove.to.charAt(0)-'a';
int nextSquareY=8-(thisMove.to.charAt(1)-'0');
if( thisMove.event.equals("castling") ){
if( thisMove.details.equals("kingside") ){
int tempY=7;
if( turn ) tempY=0;
pieces[4][tempY]=new King( 4 , tempY , turn );
pieces[7][tempY]=new Rook( 7 , tempY , turn );
pieces[5][tempY]=pieces[6][tempY]=null;
}
else{
int tempY=7;
if( turn ) tempY=0;
pieces[4][tempY]=new King( 4 , tempY , turn );
pieces[0][tempY]=new Rook( 0 , tempY , turn );
pieces[2][tempY]=pieces[3][tempY]=null;
}
}else if( thisMove.event.equals("capture") ){
pieces[prevSquareX][prevSquareY]=pieces[nextSquareX][nextSquareY];
pieces[prevSquareX][prevSquareY].X=prevSquareX;
pieces[prevSquareX][prevSquareY].Y=prevSquareY;
if( thisMove.details.equals("knight") ) pieces[nextSquareX][nextSquareY]= new Knight( nextSquareX , nextSquareY , !turn );
if( thisMove.details.equals("bishop") ) pieces[nextSquareX][nextSquareY]= new Bishop( nextSquareX , nextSquareY , !turn );
if( thisMove.details.equals("queen") ) pieces[nextSquareX][nextSquareY]= new Queen( nextSquareX , nextSquareY , !turn );
if( thisMove.details.equals("rook") ) pieces[nextSquareX][nextSquareY]= new Rook( nextSquareX , nextSquareY , !turn );
if( thisMove.details.equals("pawn") ) pieces[nextSquareX][nextSquareY]= new Pawn( nextSquareX , nextSquareY , !turn );
}else if( thisMove.event.equals("move") ){
pieces[prevSquareX][prevSquareY]=pieces[nextSquareX][nextSquareY];
pieces[nextSquareX][nextSquareY]=null;
pieces[prevSquareX][prevSquareY].X=prevSquareX;
pieces[prevSquareX][prevSquareY].Y=prevSquareY;
}else if( thisMove.event.equals("promotion") ){
pieces[prevSquareX][prevSquareY]= new Pawn( prevSquareX , prevSquareY , turn );
pieces[nextSquareX][nextSquareY]= null;
}else if( thisMove.event.equals("capture-promotion") ){
pieces[prevSquareX][prevSquareY]= new Pawn( prevSquareX , prevSquareY , turn );
if( thisMove.details.equals("knight") ) pieces[nextSquareX][nextSquareY]= new Knight( nextSquareX , nextSquareY , !turn );
if( thisMove.details.equals("bishop") ) pieces[nextSquareX][nextSquareY]= new Bishop( nextSquareX , nextSquareY , !turn );
if( thisMove.details.equals("queen") ) pieces[nextSquareX][nextSquareY]= new Queen( nextSquareX , nextSquareY , !turn );
if( thisMove.details.equals("rook") ) pieces[nextSquareX][nextSquareY]= new Rook( nextSquareX , nextSquareY , !turn );
if( thisMove.details.equals("pawn") ) pieces[nextSquareX][nextSquareY]= new Pawn( nextSquareX , nextSquareY , !turn );
}
repaint();
}
public boolean executeMove( String from , String to ){
selectedSquareX=from.charAt(0)-'a';
selectedSquareY=8-(from.charAt(1)-'0');
int targetSquareX=to.charAt(0)-'a';
int targetSquareY=8-(to.charAt(1)-'0');
boolean ret=false;
String capture="";
//if these are inside the board
if(selectedSquareX >= 0 && selectedSquareY >= 0 &&
selectedSquareX < 8 && selectedSquareY < 8 &&
targetSquareX >= 0 && targetSquareY >= 0 &&
targetSquareX < 8 && targetSquareY < 8)
{
System.out.println("inside");
if(pieces[selectedSquareX][selectedSquareY] != null && pieces[selectedSquareX][selectedSquareY].getColor() == turn )
{
System.out.println("selected");
int diffX = targetSquareX - selectedSquareX;
int diffY = targetSquareY - selectedSquareY;
if(pieces[targetSquareX][targetSquareY] != null ){
System.out.println("a target");
if( pieces[targetSquareX][targetSquareY].getColor() != pieces[selectedSquareX][selectedSquareY].getColor() && pieces[selectedSquareX][selectedSquareY].canCapture(diffX, diffY) ){
System.out.println("can capture");
capture=pieces[targetSquareX][targetSquareY].getName();
if( capture.contains("pawn") ) capture="pawn";
if( capture.contains("rook") ) capture="rook";
if( capture.contains("bishop") ) capture="bishop";
if( capture.contains("queen") ) capture="queen";
if( capture.contains("knight") ) capture="knight";
pieces[targetSquareX][targetSquareY] = pieces[selectedSquareX][selectedSquareY];
pieces[selectedSquareX][selectedSquareY] = null;
turn=!turn;
ret=true;
}
}
else{
System.out.println("no target");
if( pieces[selectedSquareX][selectedSquareY].canMove(diffX, diffY)){
System.out.println("can move");
pieces[targetSquareX][targetSquareY] = pieces[selectedSquareX][selectedSquareY];
pieces[selectedSquareX][selectedSquareY] = null;
if( !(targetSquareX == selectedSquareX && targetSquareY == selectedSquareY) ){
turn=!turn;
ret=true;
}
}
}
if( pieces[targetSquareX][targetSquareY] != null ){
pieces[targetSquareX][targetSquareY].X = targetSquareX;
pieces[targetSquareX][targetSquareY].Y = targetSquareY;
}
if( ret ){
String pawn="white-pawn";
if( !turn ) pawn="black-pawn";
System.out.println(pieces[targetSquareX][targetSquareY].getName());
if( pieces[targetSquareX][targetSquareY].getName().equals(pawn) && ( targetSquareY == 0 || targetSquareY == 7) ){
pieces[targetSquareX][targetSquareY]=new Queen( targetSquareX , targetSquareY , !turn );
if( !capture.equals("") ) st.push( new Move( ""+(char)('a'+selectedSquareX)+(char)('0'+8-selectedSquareY) , ""+(char)('a'+targetSquareX)+(char)('0'+8-targetSquareY) , "capture-promotion" , capture ) );
else st.push( new Move( ""+(char)('a'+selectedSquareX)+(char)('0'+8-selectedSquareY) , ""+(char)('a'+targetSquareX)+(char)('0'+8-targetSquareY) , "promotion" , "" ) );
}else{
if( !capture.equals("") ) st.push( new Move( ""+(char)('a'+selectedSquareX)+(char)('0'+8-selectedSquareY) , ""+(char)('a'+targetSquareX)+(char)('0'+8-targetSquareY) , "capture" , capture ) );
else st.push( new Move( ""+(char)('a'+selectedSquareX)+(char)('0'+8-selectedSquareY) , ""+(char)('a'+targetSquareX)+(char)('0'+8-targetSquareY) , "move" , "" ) );
}
}
}
}
return ret;
}
public void save( String fileName ){
File f=new File(fileName);
PrintStream prnt = null;
try {
prnt = new PrintStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("damn");
}
if( turn ) prnt.println("black");
else prnt.println("white");
for( int i=0 ; i<8 ; i++ )
for( int j=0 ; j<8 ; j++ ) if( pieces[i][j] != null ){
prnt.println(at(""+(char)('a'+i)+(char)('0'+8-j))+"-"+(char)('a'+i)+(char)('0'+8-j));
}
}
public static ChessFrame load( String fileName ){
File f=new File(fileName);
ChessFrame loadFrame = new ChessFrame();
for( int i=0 ; i<8 ; i++ )
for( int j=0 ; j<8 ; j++ ) loadFrame.pieces[i][j]=null;
Scanner read = null;
try {
read = new Scanner( f );
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("damn");
}
String trn=read.next();
loadFrame.turn=trn.equals("black");
while( read.hasNext() ){
String st=read.next();
int size=st.length();
String where=st.substring( size-2 );
boolean type=st.contains("black");
int x=where.charAt(0)-'a';
int y=8-(where.charAt(1)-'0');
if( st.contains("rook") ) loadFrame.pieces[x][y]= new Rook( x , y , type );
if( st.contains("king") ) loadFrame.pieces[x][y]= new King( x , y , type );
if( st.contains("queen") ) loadFrame.pieces[x][y]= new Queen( x , y , type );
if( st.contains("pawn") ) loadFrame.pieces[x][y]= new Pawn( x , y , type );
if( st.contains("bishop") ) loadFrame.pieces[x][y]= new Bishop( x , y , type );
if( st.contains("knight") ) loadFrame.pieces[x][y]= new Knight( x , y , type );
}
return loadFrame;
}
public void gameOver( boolean flag ){
JFrame gameover = new JFrame();
JButton gameoverButton;
if( flag ) gameoverButton= new JButton("game over check mate");
else gameoverButton= new JButton("game over stalemate");
gameover.setSize(500, 500);
gameover.add(gameoverButton);
gameover.setVisible(true);
gameoverButton.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
gameover.setVisible(false);
}
});
}
public String at(String pos){
String ret="";
int currentSquareX=pos.charAt(0)-'a';
int currentSquareY=8-(pos.charAt(1)-'0');
if( pieces[currentSquareX][currentSquareY] != null ) ret+=pieces[currentSquareX][currentSquareY].getName();
return ret;
}
public boolean move( String from, String to ){
boolean ret=executeMove( from , to );
if( !ret && (!turn && ( from.equals("e1") && ( to.equals("h1") || to.equals("a1") ) ) && pieces[4][7].getName().equals("white-king") ) ||
(turn && ( from.equals("e8") && ( to.equals("h8") || to.equals("a8") ) ) && pieces[4][0].getName().equals("black-king") ) ){
System.out.println("castling");
if( to.charAt(0) == 'h' ) ret=castling( true );
else ret=castling( false );
}
if( ret ){
boolean flag=true;
turn=!turn;
if( isInCheck() ){
flag=false;
turn=!turn;
undo();
}
if( flag ) turn=!turn;
}
System.out.println("repainting");
if( ret && isCheckmate() ) gameOver( true );
else if( ret && isStalemate() ) gameOver( false );
repaint();
return ret;
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
//print the board 's lines to show squares
for(int i = 0; i<=8; i++)
{
if( i < 8 ) g.drawString( ""+(8-i) , BOARD_MARGIN-SQUARE_WIDTH/2 , BOARD_MARGIN+(i)*SQUARE_WIDTH+5*SQUARE_WIDTH/8 );
if( i < 8 ) g.drawString( ""+(8-i) , BOARD_MARGIN-SQUARE_WIDTH/2+9*SQUARE_WIDTH , BOARD_MARGIN+(i)*SQUARE_WIDTH+5*SQUARE_WIDTH/8 );
if( i < 8 ) g.drawString( ""+(char)('a'+i) , BOARD_MARGIN+(i)*SQUARE_WIDTH+SQUARE_WIDTH/2 , BOARD_MARGIN-SQUARE_WIDTH/3 );
if( i < 8 ) g.drawString( ""+(char)('a'+i) , BOARD_MARGIN+(i)*SQUARE_WIDTH+SQUARE_WIDTH/2 , BOARD_MARGIN-SQUARE_WIDTH/3+SQUARE_WIDTH*9 );
}
//print the pieces
for(int i = 0; i<8; i++)
{
for(int j = 0; j<8; j++)
{
g.drawRect( i*SQUARE_WIDTH+BOARD_MARGIN , j*SQUARE_WIDTH+BOARD_MARGIN , SQUARE_WIDTH , SQUARE_WIDTH);
if( ((i+j)&1) == 1) g.setColor(Color.getHSBColor( 100 , 200 , 100 ));
else g.setColor(Color.white);
g.fillRect( i*SQUARE_WIDTH+BOARD_MARGIN , j*SQUARE_WIDTH+BOARD_MARGIN , SQUARE_WIDTH , SQUARE_WIDTH );
if(pieces[i][j] != null)
{
pieces[i][j].drawYourself(g, i*SQUARE_WIDTH+BOARD_MARGIN,
j*SQUARE_WIDTH+BOARD_MARGIN, SQUARE_WIDTH);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("Clicked");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("Pressed");
//calculate which square is selected
selectedSquareX = (e.getX()-BOARD_MARGIN)/SQUARE_WIDTH;
selectedSquareY = (e.getY()-BOARD_MARGIN)/SQUARE_WIDTH;
System.out.println(selectedSquareX+","+selectedSquareY);
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("Released");
//calculate which square is targeted
int targetSquareX = (e.getX()-BOARD_MARGIN)/SQUARE_WIDTH;
int targetSquareY = (e.getY()-BOARD_MARGIN)/SQUARE_WIDTH;
System.out.println(targetSquareX+","+targetSquareY+"\n");
move( ""+(char)('a'+selectedSquareX)+(char)('0'+8-selectedSquareY) , ""+(char)('a'+targetSquareX)+(char)('0'+8-targetSquareY) );
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("Entered");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("Exited");
}
}