-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
171 lines (143 loc) · 4.09 KB
/
GUI.java
File metadata and controls
171 lines (143 loc) · 4.09 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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
@SuppressWarnings("serial")
/** Graphical User Interface
* Takes care of graphics for board and pieces
* Makes moves when pieces are dragged if the move is legal
* TODO: Implement 2-click moves?? squares would light up on 1st click?
**/
public class GUI extends JPanel implements MouseListener, MouseMotionListener
{
Image piecesImg = new ImageIcon("ChessPieces.png").getImage();
static int mouseX, mouseY, newMouseX, newMouseY;
static int x = 0, y = 0;
static int squareSize = 64;
public void paintComponent(Graphics g)
{
this.addMouseListener(this);
this.addMouseMotionListener(this);
super.paintComponent(g);
// Drawing the chess board
for(int i=0; i<64; i+=2)
{
g.setColor(Color.lightGray);
g.fillRect((i%8+(i/8)%2)*squareSize, (i/8)*squareSize, squareSize, squareSize);
g.setColor(new Color(150,50,30));
g.fillRect(((i+1)%8-((i+1)/8)%2)*squareSize, ((i+1)/8)*squareSize, squareSize, squareSize);
g.drawString(" ", x, y);
}
// Displaying the pieces
for (int i=0;i<64;i++)
{
int j=-1,k=-1;
switch (Board.chessBoard[i/8][i%8])
{
case "K": j=0; k=0;
break;
case "k": j=0; k=1;
break;
case "Q": j=1; k=0;
break;
case "q": j=1; k=1;
break;
case "R": j=2; k=0;
break;
case "r": j=2; k=1;
break;
case "B": j=3; k=0;
break;
case "b": j=3; k=1;
break;
case "N": j=4; k=0;
break;
case "n": j=4; k=1;
break;
case "P": j=5; k=0;
break;
case "p": j=5; k=1;
break;
}
if (j!=-1 && k!=-1)
{
g.drawImage(piecesImg, (i%8)*squareSize, (i/8)*squareSize, (i%8+1)*squareSize,
(i/8+1)*squareSize, j*64, k*64, (j+1)*64, (k+1)*64, this);
}
}
}
// dragMove is any move that user makes (even illegal ones)
String dragMove="";
public void mouseMoved(MouseEvent e){}
public void mousePressed(MouseEvent e){
if(e.getX()<(8*squareSize)&&e.getY()<(8*squareSize)) // If cursor is on the board
mouseX=e.getX();
mouseY=e.getY();
repaint();
}
public void mouseReleased(MouseEvent e){
if(e.getX()<(8*squareSize)&&e.getY()<(8*squareSize))
newMouseX=e.getX();
newMouseY=e.getY();
repaint();
if (e.getButton()==MouseEvent.BUTTON1)
dragMove=""+mouseY/squareSize+mouseX/squareSize+newMouseY/squareSize+newMouseX/squareSize;
// dragMove must appear in possibleMoves for it to be a legal move
String userPoss=Board.possibleMoves();
if (userPoss.replaceAll(dragMove, "").length()<userPoss.length())
{
int i = userPoss.indexOf(dragMove)+4;
dragMove+=userPoss.charAt(i);
Board.recordMove(dragMove);
long startTime = System.currentTimeMillis();
AI.reset();
AI.think(3, 3);
try{
Board.recordMove(AI.bestPath.substring(0,5));
} catch(Exception x)
{ System.out.println("\n\n ###############\n # CHECKMATE!! # \n ###############\n\n"); }
System.out.println("\n"+dragMove);
BB.refreshMoves();
printCntl();
long finishTime = System.currentTimeMillis();
System.out.println("\nThat took: "+(finishTime-startTime)+ " ms");
}
}
public void mouseClicked(MouseEvent e){}
public void mouseDragged(MouseEvent e){
if(e.getX()<(8*squareSize)&&e.getY()<(8*squareSize))
newMouseX=e.getX();
newMouseY=e.getY();
repaint();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void printCntl() // FOR TESTING PURPOSES
{
BB.refreshMoves();
System.out.println("");
for (int j=0; j<64; j++)
{
if(BB.controlW()[j] && BB.controlB()[j])
System.out.print(" #");
else if(BB.controlW()[j]==true)
System.out.print(" X");
else if(BB.controlB()[j]==true)
System.out.print(" O");
else System.out.print(" .");
if(j%8==7)
System.out.println(" |"+(8-j/8));
}
System.out.println(" ________________\n a b c d e f g h ");
}
public static void printBoard() // FOR TESTING PURPOSES
{
// Print the board on the console
for (int j=0; j<64; j++)
{
System.out.print(" "+Board.chessBoard[j/8][j%8]);
if(j%8==7)
System.out.println(" |"+(8-j/8));
}
System.out.println(" ________________\n a b c d e f g h ");
}
}