-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChessBoard.java
251 lines (202 loc) · 8.08 KB
/
ChessBoard.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
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.*;
import java.io.*;
import java.awt.Font;
public class ChessBoard{
private JFrame frame;
public static void main(String[]args){
new ChessBoard();
}//main
public ChessBoard(){
frame = new JFrame("Chess Program");
frame.setSize(800, 800);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(frame.getSize());
frame.add(new InnerProgram(frame.getSize()));
frame.pack();
frame.setVisible(true);
}//constructor
public static class InnerProgram extends JPanel implements Runnable, MouseListener {
Game game = new Game();
//BoardLoc[][] board = new BoardLoc[8][8];
private Thread animator;
Dimension d;
String str = "";
int xPos = 0;
int yPos = 0;
public InnerProgram (Dimension dimension) {
setSize(dimension);
setPreferredSize(dimension);
addMouseListener(this);
addKeyListener(new TAdapter());
setFocusable(true);
d = getSize();
//for animating the screen - you won't need to edit
if (animator == null) {
animator = new Thread(this);
animator.start();
}
setDoubleBuffered(true);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.GRAY);
g2.fillRect(0, 0,(int)d.getWidth() , (int)d.getHeight());
Color co = new Color(255,255,255);
g2.setColor(co);
int fontSize = 10;
g2.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
g2.drawString("String " + str,20,40);
for(int r = 0;r<game.board.length;r++){
for(int c = 0;c<game.board[0].length;c++){
if((r+c)%2==0){
g2.setColor(Color.white);
g2.fillRect(r*100, c*100, 100, 100);
}//if
if(game.p1Selected != null) {
g2.setColor(Color.RED);
g2.fillRect(game.p1Selected.pos.c * 100, game.p1Selected.pos.r*100, 100, 100);
}
}//end nested
}//end for
for(Piece p : game.player1.pieces) {
p.draw(g2);
//p.generateValidMoves();
}
for(Piece p : game.player2.pieces) {
p.draw(g2);
//p.generateValidMoves();
}
}
public void mousePressed(MouseEvent e) {
xPos = e.getX();
yPos = e.getY();
str = xPos + " " + yPos;
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
xPos = e.getX();
yPos = e.getY();
int r = yPos / 100;
int c = xPos / 100;
Piece selected = game.board[r][c].piece;
if(game.moveCount % 2 == 0) { // if white's move
if(game.p1Selected == null) { // if white has not chosen a piece yet
System.out.println("WHITE'S MOVE!");
if(selected.white) {
if(selected.pieceName==null){
((Pawn)selected).generateValidMoves(game.board);
} else if (selected.pieceName.equals("N")){
((Knight)selected).generateValidMoves(game.board);
} else if (selected.pieceName.equals("B")){
((Bishop)selected).generateValidMoves(game.board);
} else if (selected.pieceName.equals("R")){
((Rook)selected).generateValidMoves(game.board);
}
else if (selected.pieceName.equals("Q")){
((Queen)selected).generateValidMoves(game.board);
}
else if (selected.pieceName.equals("K")){
((King)selected).generateValidMoves(game.board);
}
System.out.println("SELECTED VALID PIECE!");
game.p1Selected = selected;
System.out.println("White selected " + game.p1Selected.pieceName + " at " + game.p1Selected.pos.c + ", " + game.p1Selected.pos.r);
}
}
else { // if already selected
/*for(Move m : game.p1Selected.moves) {
if(m.move == board[r][c]) {
game.p1Selected.move(board[r][c]);
game.p1Selected = null;
break;
}
}*/
if(game.p1Selected.move(game.board[r][c])) {
System.out.println("MOVED!");
game.p1Selected = null;
game.moveCount++;
}
}
}
else { // if black's move
if(game.p2Selected == null) { // if black has not chosen a piece yet
if(!selected.white) {
if(selected.pieceName==null){
((Pawn)selected).generateValidMoves(game.board);
} else if (selected.pieceName.equals("N")){
((Knight)selected).generateValidMoves(game.board);
} else if (selected.pieceName.equals("B")){
((Bishop)selected).generateValidMoves(game.board);
} else if (selected.pieceName.equals("R")){
((Rook)selected).generateValidMoves(game.board);
}
else if (selected.pieceName.equals("Q")){
((Queen)selected).generateValidMoves(game.board);
}
else if (selected.pieceName.equals("K")){
((King)selected).generateValidMoves(game.board);
}
game.p2Selected = selected;
}
}
else { // if already selected
/*for(Move m : game.p1Selected.moves) {
if(m.move == board[r][c]) {
game.p1Selected.move(board[r][c]);
p1Selected.
}
}*/
if(game.p2Selected.move(game.board[r][c])) {
game.p2Selected = null;
game.moveCount++;
}
}
}
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
int keyr = e.getKeyCode();
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
String c = KeyEvent.getKeyText(e.getKeyCode());
//str += " char " + c + " key " + key;
// c = Character.toString((char) key);
}
}//end of adapter
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
int animationDelay = 37;
long time = System.currentTimeMillis();
while (true) {// infinite loop
// spriteManager.update();
repaint();
try {
time += animationDelay;
Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
} catch (InterruptedException e) {
System.out.println(e);
} // end catch
} // end while loop
}// end of run
}//end of class (inner program)
}//end of chessboard class