5
5
import com .luox6 .battleship .gui .factory .Dialog ;
6
6
import com .luox6 .battleship .gui .model .PlayerBoard ;
7
7
import com .luox6 .battleship .gui .model .UserSetting ;
8
- import com .luox6 .battleship .model .GameBoard ;
9
8
import com .luox6 .battleship .model .ship .StandbyShip ;
10
9
import com .luox6 .battleship .network .Client ;
11
10
import com .luox6 .battleship .network .Connectable ;
15
14
import javax .swing .*;
16
15
import java .util .ArrayList ;
17
16
import java .util .List ;
18
- import java .util .concurrent .Callable ;
19
17
import java .util .concurrent .ExecutorService ;
20
18
import java .util .concurrent .Executors ;
21
19
import java .util .concurrent .Future ;
22
20
21
+ /**
22
+ * Application Battleship main entry
23
+ * @author Xinhao Luo
24
+ * @version 0.0.1
25
+ */
23
26
public class Main {
24
27
/**
25
28
* This main method is main for test purpose.
@@ -64,6 +67,7 @@ public static void main(String[] args) {
64
67
final Protocol [] pr = new Protocol [1 ];
65
68
final Connectable [] c = new Connectable [1 ];
66
69
ExecutorService executor = Executors .newSingleThreadExecutor ();
70
+ // Sync process
67
71
Future <Boolean > res = executor .submit (() -> {
68
72
try {
69
73
if (m == Protocol .Mode .SERVER ) {
@@ -133,6 +137,7 @@ public static void main(String[] args) {
133
137
134
138
initDialog .setVisible (true );
135
139
140
+ // Wait for process
136
141
try {
137
142
Boolean r = res .get ();
138
143
if (!r ) {
@@ -159,12 +164,19 @@ public static void main(String[] args) {
159
164
if (response != null ) {
160
165
c [0 ].send (response );
161
166
}
162
- } while (true );
167
+ } while (! c [ 0 ]. isConnectionClosed () );
163
168
});
164
169
// Launch GUI
165
170
guiController .start ();
166
171
}
167
172
173
+ /** Helper functions for server/client init **/
174
+
175
+ /**
176
+ * This function used to separate process with NEXT command
177
+ * @param c Connectable Server/Client has already connected to client
178
+ * @param pr Protocol Process response
179
+ */
168
180
private static void awaitSync (Connectable [] c , Protocol [] pr ) {
169
181
c [0 ].send (Protocol .GameCommand .NEXT .toString ());
170
182
do {
@@ -178,7 +190,11 @@ private static void awaitSync(Connectable[] c, Protocol[] pr) {
178
190
} while (true );
179
191
}
180
192
181
- public static Object [] getClientInfo () {
193
+ /**
194
+ * Dialog for client info
195
+ * @return [String address, int port, String name]
196
+ */
197
+ private static Object [] getClientInfo () {
182
198
String [] config = Dialog .clientInitialDialog (null );
183
199
if (config == null ) {
184
200
System .exit (1 );
@@ -188,6 +204,7 @@ public static Object[] getClientInfo() {
188
204
String address = config [0 ];
189
205
int port = Integer .parseInt (config [1 ]);
190
206
String name = config [2 ];
207
+ validName (name );
191
208
192
209
if (name .length () < 1 ) {
193
210
throw new Exception ("Name must have at least 1 characters" );
@@ -207,7 +224,11 @@ public static Object[] getClientInfo() {
207
224
}
208
225
}
209
226
210
- public static Object [] getServerInfo () {
227
+ /**
228
+ * Dialog for Server Info
229
+ * @return [String address, int port]
230
+ */
231
+ private static Object [] getServerInfo () {
211
232
String [] config = Dialog .serverInitialDialog (null );
212
233
if (config == null ) {
213
234
System .exit (1 );
@@ -216,16 +237,13 @@ public static Object[] getServerInfo() {
216
237
try {
217
238
int port = Integer .parseInt (config [0 ]);
218
239
String name = config [1 ];
240
+ validName (name );
219
241
220
- if (name .length () > 0 ) {
221
- Object [] res = new Object [2 ];
222
- res [0 ] = port ;
223
- res [1 ] = name ;
224
-
225
- return res ;
226
- }
242
+ Object [] res = new Object [2 ];
243
+ res [0 ] = port ;
244
+ res [1 ] = name ;
227
245
228
- throw new Exception ( "Name must be at least 1 character" ) ;
246
+ return res ;
229
247
} catch (NumberFormatException e ) {
230
248
Dialog .numberParseDialog (null , e );
231
249
return null ;
@@ -234,4 +252,28 @@ public static Object[] getServerInfo() {
234
252
return null ;
235
253
}
236
254
}
255
+
256
+ /**
257
+ * Validate User input name
258
+ * @param s String name
259
+ * @throws Exception failed reason
260
+ */
261
+ public static void validName (String s ) throws Exception {
262
+ if (s .length () < 1 || s .length () > 20 ) {
263
+ throw new Exception ("Name must be between 1-20 characters" );
264
+ }
265
+
266
+ boolean flag = true ;
267
+ for (int i = 0 ; i < s .length (); i ++) {
268
+ char c = s .charAt (i );
269
+ if (!((c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' ) || (c >= '1' && c <= '9' ))) {
270
+ flag = false ;
271
+ break ;
272
+ }
273
+ }
274
+
275
+ if (!flag ) {
276
+ throw new Exception ("Character can only be a-z, A-Z, 0-9" );
277
+ }
278
+ }
237
279
}
0 commit comments