-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetJudge.java
More file actions
246 lines (216 loc) · 7.51 KB
/
Copy pathNetJudge.java
File metadata and controls
246 lines (216 loc) · 7.51 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
package net;
import data.Game;
import judge.Judge;
import objects.Chess;
import objects.Player;
import objects.PlayerBlack;
import objects.PlayerWhite;
import ui.UI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;
public class NetJudge {
public static final int COMMEND_STOP = -1;
public static final int COMMEND_START = 0;
public static final int SHOW_MAIN_UI = 1;
public static final int WRITE_CHESS = 2;
public static final int WIN = 3;
public static final int LOSE = 4;
public static final int IS_CONNECTED = 5;
public static final int SET_BLACK_COLOR = 6;
public static final int SET_WHITE_COLOR = 7;
public static final int SET_IF_GAME_END = 8;
Socket socket = null;
Integer tag = null;
Integer color = null;
JFrame mainUI = null;
Integer timeOutCount = 10;
Player player = null;
public static NetJudge netJudge = null;
public NetJudge(Socket socket, int TAG) {
netJudge = this;
this.socket = socket;
this.tag = TAG;
getRequest();
if (TAG == 0) {
chooseColor();
UI.frame.setTitle("五子棋-服务端");
} else {
UI.frame.setTitle("五子棋-客户端");
JOptionPane.showMessageDialog(UI.frame, "等待房主选择棋子颜色");
}
setIfGameEnd(false);
}
public void initClient(int color){
try {
HashMap<String, Object> data = new HashMap<>();
data.put("color", color);
data.put("ifGameEnd", Game.ifGameEnd);
data.put("chessArray", Game.chessArray);
data.put("showMainUI", true);
ObjectOutputStream outputStream = (ObjectOutputStream) socket.getOutputStream();
outputStream.writeObject(data);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void setIfGameEnd(boolean flag){
Game.ifGameEnd = false;
try {
OutputStream outputStream = socket.getOutputStream();
outputStream.write(new byte[]{SET_IF_GAME_END, (byte) (flag?1:0)});
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// private void setTimeOutThread() {
// Thread thread = new Thread() {
// @Override
// public void run() {
// while (true) {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// timeOutCount = timeOutCount + 1;
//
// if (timeOutCount == 10) {
// JOptionPane.showMessageDialog(UI.frame, "超时");
// }
// }
// }
// };
// thread.setDaemon(true);
// thread.start();
// }
//
// private void setIsConnectedThread() {
// try {
// OutputStream stream = socket.getOutputStream();
// stream.write(new byte[]{IS_CONNECTED});
// stream.close();
// } catch (IOException ignored) {
//
// }
// }
private void reConnect() {
}
private void getRequest() {
Thread thread = new Thread() {
@Override
public void run() {
while (true) {
try {
if (!socket.isClosed()) {
InputStream stream = socket.getInputStream();
byte[] commends = {-2};
byte commend = commends[0];
try {
commends = stream.readAllBytes();
commend = commends[0];
} catch (IOException | ArrayIndexOutOfBoundsException ignored) {
}
switch (commend) {
case COMMEND_STOP:
break;
case COMMEND_START:
break;
case SHOW_MAIN_UI:
UI.frame.setVisible(true);
break;
case WRITE_CHESS:
int row = commends[1];
int column = commends[2];
int color = commends[3];
writeChess(row, column, color);
break;
case IS_CONNECTED:
timeOutCount = 0;
break;
case SET_IF_GAME_END:
Game.ifGameEnd= commends[1] != 0;
case -2:
break;
}
}
Thread.sleep(200);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
break;
}
}
}
};
thread.setDaemon(true);
thread.start();
}
private void initPlayer(int color) {
this.color = color;
if (color == 0) {
player = new PlayerBlack();
} else {
player = new PlayerWhite();
}
}
private void initPlayer() {
if (color == 0) {
player = new PlayerBlack();
} else {
player = new PlayerWhite();
}
}
private synchronized void chooseColor() {
JFrame frame = new JFrame("请选择你的棋子颜色");
frame.setLayout(new FlowLayout());
frame.setSize(400, 130);
frame.setLocationRelativeTo(null);
JButton block = new JButton("黑色");
JButton white = new JButton("白色");
class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("黑色")) {
color = 0;
} else if (e.getActionCommand().equals("白色")) {
color = 1;
}
initPlayer();
frame.setVisible(false);
UI.frame.setVisible(true);
initClient(color==0?1:0);
}
}
Listener listener = new Listener();
block.addActionListener(listener);
white.addActionListener(listener);
frame.add(block);
frame.add(white);
frame.setVisible(true);
}
private void writeChess(int row, int column, int color) {
}
public void addChess(int row, int column, int color) {
if (tag == 1) {
try {
OutputStream outputStream = socket.getOutputStream();
outputStream.write(new byte[]{WRITE_CHESS, (byte) row, (byte) column, (byte) color});
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}else if (tag==0){
Game.chessArray.add(new Chess(row, column, color));
}
}
}