-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainFrame.java
383 lines (359 loc) · 12.7 KB
/
MainFrame.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
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
//class for the frame containing all components of the application
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.net.*;
public class MainFrame extends JFrame
{
//static constants for pieces indexes
public static final int W_ROOK1 = 24, W_KNIGHT1 = 25, W_BISHOP1 = 26, W_QUEEN = 27, W_KING = 28,
W_BISHOP2 = 29, W_KNIGHT2 = 30, W_ROOK2 = 31, W_PAWN_MIN = 16, W_PAWN_MAX = 23, W_MIN = 16;
public static final int B_ROOK1 = 0, B_KNIGHT1 = 1, B_BISHOP1 = 2, B_QUEEN = 3, B_KING = 4,
B_BISHOP2 = 5, B_KNIGHT2 = 6, B_ROOK2 = 7, B_PAWN_MIN = 8, B_PAWN_MAX = 15, B_MIN = 0;
//variables tracking the state of the game
private PieceAbstract[] pieces;
private char turn;
private boolean gameOver = false;
//all panels contained in this frame
private BoardPanel boardPanel;
private HistoryPanel historyPanel;
private MenuPanel menuPanel;
//network menu variables
private JMenuItem hostItem;
private JMenuItem joinItem;
private JMenuItem quitItem;
private JLabel statusLabel;
//network variables
private String host="localhost";
private int port=5000;
private PrintWriter out=null;
private boolean connected=false;
private boolean yourTurn=true;
private ServerSocket serverSocket;
public MainFrame()
{
//call super class's constructor and set title
super("Chess");
//create array of chess pieces
pieces = new PieceAbstract[32];
//setup game state variables
turn = 'W';
gameOver = false;
initializePieces();
//create board panel
boardPanel = new BoardPanel(this, pieces);
add(boardPanel,BorderLayout.CENTER);
//create a history panel
historyPanel = new HistoryPanel(pieces);
add(historyPanel,BorderLayout.EAST);
//create menu panel
menuPanel = new MenuPanel(this);
add(menuPanel,BorderLayout.WEST);
//create menu, its items, and action handler
ActionHandler ah=new ActionHandler();
JMenuBar jmb=new JMenuBar();
setJMenuBar(jmb);
JMenu networkMenu=new JMenu("Network");
jmb.add(networkMenu);
hostItem=new JMenuItem("Host Game...");
hostItem.addActionListener(ah);
networkMenu.add(hostItem);
joinItem=new JMenuItem("Join Game...");
joinItem.addActionListener(ah);
networkMenu.add(joinItem);
quitItem=new JMenuItem("Quit Game");
quitItem.addActionListener(ah);
quitItem.setEnabled(false);
networkMenu.add(quitItem);
statusLabel=new JLabel(" ");
add(statusLabel,BorderLayout.SOUTH);
//setup this frame's behavior/appearance
setResizable(false);
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//if someone closes window in network send quit
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
super.windowClosing(e);
if(connected)
out.println("quit");
}
});
}
private void initializePieces()
{
for(int i = 0; i < pieces.length; i++)
{
// white pieces
if(i == W_ROOK1 || i == W_ROOK2)
pieces[i] = new Rook(i%8, 7, 'W', this, pieces);
else if(i == W_KNIGHT1 || i == W_KNIGHT2)
pieces[i] = new Knight(i%8, 7, 'W', this, pieces);
else if(i == W_BISHOP1 || i == W_BISHOP2)
pieces[i] = new Bishop(i%8, 7, 'W', this, pieces);
else if(i == W_QUEEN)
pieces[i] = new Queen(i%8, 7, 'W', this, pieces);
else if(i == W_KING)
pieces[i] = new King(i%8, 7, 'W', this, pieces);
else if(i >= W_PAWN_MIN && i <= W_PAWN_MAX)
pieces[i] = new Pawn(i%8, 6, 'W', this, pieces);
// black pieces
else if(i >= B_PAWN_MIN && i <= B_PAWN_MAX)
pieces[i] = new Pawn(i%8, 1, 'B', this, pieces);
else if(i == B_ROOK1 || i == B_ROOK2)
pieces[i] = new Rook(i%8, 0, 'B', this, pieces);
else if(i == B_KNIGHT1 || i == B_KNIGHT2)
pieces[i] = new Knight(i%8, 0, 'B', this, pieces);
else if(i == B_BISHOP1 || i == B_BISHOP2)
pieces[i] = new Bishop(i%8, 0, 'B', this, pieces);
else if(i == B_QUEEN)
pieces[i] = new Queen(i%8, 0, 'B', this, pieces);
else if(i == B_KING)
pieces[i] = new King(i%8, 0, 'B', this, pieces);
}
}
//reset the application to beginning state
public void reset()
{
initializePieces();
turn = 'W';
menuPanel.updateMenuLabels();
menuPanel.setNewGameEnabled(false);
gameOver = false;
historyPanel.clearHistory();
boardPanel.repaint();
}
//get the current player's turn
public char getTurn()
{
return turn;
}
//set the current player's turn
public void setTurn(char turn)
{
this.turn = turn;
}
//get the current game over state
public boolean isGameover()
{
return gameOver;
}
//set the current game over state
public void setGameover(boolean gameOver)
{
this.gameOver = gameOver;
menuPanel.setNewGameEnabled(true);
}
//update the menus in the menu panel
public void updateMenu()
{
menuPanel.updateMenuLabels();
}
//add move to history in the history panel
public void addMove(PieceAbstract piece, int moveType, int startX, boolean isPromoting)
{
historyPanel.addMove(piece, moveType, startX, isPromoting);
}
//add a promotion to history in the history panel
public void addPromotion(PieceAbstract piece)
{
historyPanel.addPromotion(piece);
}
private class ActionHandler implements ActionListener
{
//actionhandler for each of the buttons in the network menu
public void actionPerformed(ActionEvent e)
{
//host
if(e.getSource()==hostItem)
{
String s=JOptionPane.showInputDialog(MainFrame.this
,"Enter the port to use",""+port);
if(s==null) return;
port=Integer.parseInt(s);
hostItem.setEnabled(false);
joinItem.setEnabled(false);
quitItem.setEnabled(true);
yourTurn = false;
new Server().start();
reset();
}
//join
else if(e.getSource()==joinItem)
{
String s=JOptionPane.showInputDialog(MainFrame.this
,"Enter the hostname",""+host);
if(s==null) return;
host=s;
s=JOptionPane.showInputDialog(MainFrame.this
,"Enter the port to use",""+port);
if(s==null) return;
port=Integer.parseInt(s);
hostItem.setEnabled(false);
joinItem.setEnabled(false);
quitItem.setEnabled(true);
new Client().start();
reset();
}
//quit
else if(e.getSource()==quitItem)
{
connected=false;
if(serverSocket!=null)
{
try
{
serverSocket.close();
serverSocket=null;
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
hostItem.setEnabled(true);
joinItem.setEnabled(true);
quitItem.setEnabled(false);
if(out!=null) out.println("quit");
}
}
}
//sends move information to the other player on network
public void sendMove(int selected, int currX, int currY,int promotionPiece)
{
out.println(selected + "," + currX + "," + currY+ ","+promotionPiece);
yourTurn=false;
statusLabel.setText("Opponent's turn.");
}
//sends forfeit information to other player on network
public void sendForfeit()
{
out.println("Forfeit");
}
//send reset information to the other on network
public void sendReset()
{
out.println("Reset");
}
//accessor for other methods
public Boolean getYourTurn()
{
return yourTurn;
}
//accessor for other methods
public Boolean getConnected()
{
return connected;
}
//play who hosts a game runs the server
private class Server extends Thread
{
public void run()
{
try
{
serverSocket=new ServerSocket(port);
statusLabel.setText("Waiting for opponent to connect...");
Socket s=serverSocket.accept();
statusLabel.setText("Opponent's turn.");
connected=true;
BufferedReader in=new BufferedReader(new InputStreamReader(
s.getInputStream()));
out=new PrintWriter(s.getOutputStream(),true);
String line;
while(connected&&(line=in.readLine())!=null)
{
if(line.equals("quit")) break;
if(line.equals("Forfeit"))
{
JOptionPane.showMessageDialog(null,"You Win","Forfeit!",JOptionPane.INFORMATION_MESSAGE);
break;
}
if(line.equals("Reset"))
{
setGameover(true);
reset();
yourTurn = false;
statusLabel.setText("Opponent's turn.");
continue;
}
String[] sa=line.split(",");
boardPanel.doMove(Integer.parseInt(sa[0]),Integer.parseInt(sa[1]),Integer.parseInt(sa[2]),Integer.parseInt(sa[3]));
if(gameOver) break;
yourTurn=true;
statusLabel.setText("Your turn.");
}
connected=false;
statusLabel.setText("Game ended.");
menuPanel.setNewGameEnabled(true);
if(serverSocket!=null) serverSocket.close();
serverSocket=null;
s.close();
}
catch(IOException ioe)
{}
hostItem.setEnabled(true);
joinItem.setEnabled(true);
quitItem.setEnabled(false);
}
}
//player that joins a game runs the client
private class Client extends Thread
{
public void run()
{
try
{
Socket s=new Socket(host,port);
yourTurn = true;
statusLabel.setText("Your turn.");
connected=true;
BufferedReader in=new BufferedReader(new InputStreamReader(
s.getInputStream()));
out=new PrintWriter(s.getOutputStream(),true);
String line;
while(connected&&(line=in.readLine())!=null)
{
if(line.equals("quit")) break;
if(line.equals("Forfeit"))
{
JOptionPane.showMessageDialog(null,"You Win","Forfeit!",JOptionPane.INFORMATION_MESSAGE);
break;
}
if(line.equals("Reset"))
{
setGameover(true);
reset();
yourTurn = true;
statusLabel.setText("Your turn.");
continue;
}
String[] sa=line.split(",");
boardPanel.doMove(Integer.parseInt(sa[0]),Integer.parseInt(sa[1]),Integer.parseInt(sa[2]),Integer.parseInt(sa[3]));
if(gameOver) connected = false;
yourTurn=true;
statusLabel.setText("Your turn.");
}
connected=false;
statusLabel.setText("Game ended.");
menuPanel.setNewGameEnabled(true);
s.close();
}
catch(IOException ioe)
{}
hostItem.setEnabled(true);
joinItem.setEnabled(true);
quitItem.setEnabled(false);
}
}
//start the application
public static void main(String[] args)
{
new MainFrame();
}
}