-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
GameUI.java
446 lines (383 loc) · 13.5 KB
/
GameUI.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
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
440
441
442
443
444
445
446
package tic;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeoutException;
import java.util.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import java.io.*;
import javax.imageio.*;
import javax.swing.event.ChangeListener;
class GameUI extends JFrame
{
GamePanel game; //游戏界面
ConnectPanel connectPanel; //连接界面
GameUI()
{
JPanel now = (JPanel)this.getContentPane();
now.setLayout(null);
game = new GamePanel();
connectPanel = new ConnectPanel(game);
now.add(game);
now.add(connectPanel);
game.setBounds(0, 0, 550, 550);
connectPanel.setBounds(550,0,250,600);
this.setSize(800,600);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class GamePanel extends JPanel implements ActionListener
{
JButton[][] gamePage; //表示3x3的棋盘格
public boolean isServer = false;
boolean ismyTurn;
TicGame chessGame;
public SocketHandler soc;
GamePanel()
{
gamePage = new JButton[3][3];
chessGame = new TicGame();
for (int i = 0;i < 3;i++)
for (int j = 0;j < 3;j++)
{
gamePage[i][j] = new JButton();
gamePage[i][j].addActionListener(this);
}
JPanel mask = new JPanel();
//mask.setLayout(new GridBagLayout());
mask.setLayout(new GridLayout(3,3));
for (int i = 0;i < 3;i++)
for (int j = 0;j < 3;j++)
mask.add(gamePage[i][j]);
//mask.add(gamePage[i][j],getGBC(i, j, 1, 1));
this.setLayout(new GridLayout(1,1));
this.add(mask);
this.setSize(550,550);
this.setVisible(true);
}
public void paintComponent(Graphics g)
{
try {
Image img1 = ImageIO.read(getClass().getResourceAsStream("images/image1.png"));
Image img2 = ImageIO.read(getClass().getResourceAsStream("images/image2.png"));
for (int i = 0;i < 3;i++)
for (int j = 0;j < 3;j++)
if (chessGame.chess[i][j] == 1)
this.gamePage[i][j].setIcon(new ImageIcon(img1));
//this.gamePage[i][j].setText("X");
else if (chessGame.chess[i][j] == -1)
this.gamePage[i][j].setIcon(new ImageIcon(img2));
//this.gamePage[i][j].setText("Y");
else this.gamePage[i][j].setText("");
//logic.gamePage[x][y].setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
}
@Override
public void actionPerformed(ActionEvent e)
{
//System.out.println("button pressed");
if (!ismyTurn)
return;
for (int i = 0;i < 3;i++)
for (int j = 0;j <3;j++)
if (e.getSource().equals(this.gamePage[i][j]))
{
//System.out.println(""+i+" "+j);
int value;
JButton temp = gamePage[i][j];
if (!temp.getText().equals(""))
return;
try {
Image img1 = ImageIO.read(getClass().getResourceAsStream("images/image1.png"));
Image img2 = ImageIO.read(getClass().getResourceAsStream("images/image2.png"));
if (isServer)
temp.setIcon(new ImageIcon(img1));
else temp.setIcon(new ImageIcon(img2));
} catch (Exception ex) {
System.out.println(ex);
}
if (isServer)
{
// System.out.println("jhhh");
//temp.setLabel("X");
value = 1;
}
else
{
//System.out.println("hhhhh");
//temp.setLabel("Y");
value = -1;
}
try{
//soc.output.writeBytes("hhh\n");
soc.output.writeBytes(""+i+" "+j+"\n");
System.out.println("发送");
}catch(IOException we)
{
}
chessGame.setPoint(i,j,value);
int result = chessGame.judgeWin();
String re = "";
if (result != 0)
{
if ((isServer && result == 1) ||(!isServer && result == -1))
re = "You win!";
else re = "You lose!";
JOptionPane.showMessageDialog(null, "游戏结束啦:" + re);
System.exit(0);
}
ismyTurn = false;
break;
}
}
}
class ConnectPanel extends JPanel implements ActionListener //游戏界面中的拨号连接界面
{
private boolean judge; //判断连接是否成功
JButton startserver, joinserver, back;
JLabel address, port, status, speed;
JTextField txt_address, txt_port;
int portnum=0; //端口
boolean GameisOn;
GamePanel game;
ConnectPanel(GamePanel l)
{
game = l;
initComponent();
}
public void initComponent()
{
startserver = new JButton("Server");
startserver.addActionListener(this);
joinserver = new JButton("Client");
joinserver.addActionListener(this);
address = new JLabel("Address");
port = new JLabel("Port");
txt_address = new JTextField(12);
txt_port = new JTextField(4);
txt_port.setText(String.valueOf(1234));
JPanel joinaddress = new JPanel();
joinaddress.setLayout(new FlowLayout());
joinaddress.add(address);
joinaddress.add(txt_address);
JPanel joinport = new JPanel();
joinport.setLayout(new FlowLayout());
joinport.add(port);
joinport.add(txt_port);
JPanel btn = new JPanel();
//btn.setLayout(new BoxLayout(btn, BoxLayout.PAGE_AXIS));
btn.setLayout(new FlowLayout());
btn.add(joinserver);
btn.add(startserver);
JPanel input = new JPanel();
input.setLayout(new BoxLayout(input, BoxLayout.Y_AXIS));
JPanel tt = new JPanel();
tt.setLayout(new FlowLayout());
status = new JLabel("Connect Info", JLabel.CENTER);
tt.add(status);
input.add(tt);
input.add(joinaddress);
input.add(joinport);
input.add(btn);
this.add(input);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(this.startserver))
{
game.isServer = true;
game.ismyTurn = true;
game.repaint();
game.setVisible(true);
System.out.println("start server");
try
{
portnum = Integer.parseInt(txt_port.getText());
}catch(NumberFormatException ee)
{
JOptionPane.showMessageDialog(null, "端口输入格式错误", "输入错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (portnum > 65535 || portnum < 0)
{
JOptionPane.showMessageDialog(null, "端口输入范围为0~65535", "输入错误", JOptionPane.ERROR_MESSAGE);
return;
}
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
int checksleeptime = 1000;
ServerSocket server = new ServerSocket(portnum);
//System.out.println(InetAddress.getLocalHost().toString());
txt_address.setText(InetAddress.getLocalHost().toString().split("/")[1]);
txt_port.setText(""+portnum);
status.setText("等待玩家加入");
Socket connect = server.accept();
server.close();
status.setText("玩家" + connect.getInetAddress().toString() + "已连入,同步游戏设定中");
SocketHandler soc = new SocketHandler(true, connect, game);
game.soc = soc;
soc.SynParamServer();
Thread.sleep(checksleeptime);
status.setText("同步成功,游戏即将开始...2");
Thread.sleep(checksleeptime);
status.setText("同步成功,游戏即将开始...1");
StartGame();
}catch (IOException ie)
{
JOptionPane.showMessageDialog(null, "连接错误:" + ie.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
ie.printStackTrace();
}catch(InterruptedException t)
{
}
}
}).start();
}
else
{
game.isServer = false;
game.ismyTurn = false;
game.setVisible(true);
game.isServer = false;
try
{
portnum = Integer.parseInt(txt_port.getText());
} catch (NumberFormatException n)
{
JOptionPane.showMessageDialog(null, "端口输入格式错误", "输入错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (portnum > 65535 || portnum < 0) {
JOptionPane.showMessageDialog(null, "端口输入范围为0~65535", "输入错误", JOptionPane.ERROR_MESSAGE);
return;
}
InetAddress tarip;
try {
tarip = InetAddress.getByName(txt_address.getText());
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "IP地址输入不正确", "输入错误", JOptionPane.ERROR_MESSAGE);
return;
}
txt_address.setText(tarip.getHostAddress());
try
{
Socket connect2 = new Socket(tarip, portnum);
//connect2.setSoTimeout(3000);
//connect2.connect(new InetSocketAddress(tarip, portnum), 3000);
status.setText("已连接");
SocketHandler soc2 = new SocketHandler(false, connect2, game);
game.soc = soc2;
game.soc.SynParamClient();
new Thread(new Runnable(){
@Override
public void run()
{
try
{
Thread.sleep(1000);
status.setText("同步成功,游戏即将开始...2");
Thread.sleep(1000);
status.setText("同步成功,游戏即将开始...1");
Thread.sleep(1000);
StartGame();
}catch (InterruptedException e)
{
e.printStackTrace();
}catch(Exception oe)
{
}
}
}).start();
}catch (SocketTimeoutException r)
{
JOptionPane.showMessageDialog(null, "连接超时" , "错误", JOptionPane.ERROR_MESSAGE);
return;
//ie.printStackTrace();
}
catch (IOException ie)
{
JOptionPane.showMessageDialog(null, "连接错误:" + ie.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
ie.printStackTrace();
}
catch(Exception eee)
{
}
}
}
public void StartGame()
{
game.soc.startGame();
}
}
class TicGame
{
public int[][] chess;
TicGame()
{
chess = new int[3][3];
for (int i= 0;i<3;i++)
for (int j = 0;j < 3;j++)
chess[i][j] = 0; //初始化棋盘
}
int judgeWin() //游戏结束判断
{
for (int i = 0;i < 3;i++)
{
int temp = 0;
for (int j = 0;j < 3;j++)
temp += chess[i][j];
if (temp == 3)
return 1;
else if (temp == -3)
return -1;
}
for (int i = 0;i < 3;i++)
{
int temp = 0;
for(int j = 0;j < 3;j++)
temp += chess[j][i];
if (temp == 3)
return 1;
else if (temp == -3)
return -1;
}
int temp = 0;
int temp2 = 0;
for (int i = 0;i < 3;i++)
{
temp += chess[i][i];
temp2 += chess[i][2-i];
}
if (temp == 3 || temp2 == 3)
return 1;
else if (temp == -3||temp2 == -3)
return -1;
return 0;
}
public void setPoint(int i, int j, int value)
{
chess[i][j] = value;
for (int iii = 0;iii < 3;iii++)
{
for (int jj = 0;jj < 3;jj++)
System.out.print(this.chess[iii][jj]);
System.out.println("");
}
}
}