Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit 4b348b0

Browse files
committed
all note and test are ready
1 parent 400070d commit 4b348b0

38 files changed

+927
-183
lines changed

BattleShip/BattleShip.iml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,37 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library" scope="TEST">
11+
<library name="JUnit5.4">
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-5.4.2.jar!/" />
14+
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-api-5.4.2.jar!/" />
15+
<root url="jar://$MODULE_DIR$/lib/apiguardian-api-1.0.0.jar!/" />
16+
<root url="jar://$MODULE_DIR$/lib/opentest4j-1.1.1.jar!/" />
17+
<root url="jar://$MODULE_DIR$/lib/junit-platform-commons-1.4.2.jar!/" />
18+
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-params-5.4.2.jar!/" />
19+
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-engine-5.4.2.jar!/" />
20+
<root url="jar://$MODULE_DIR$/lib/junit-platform-engine-1.4.2.jar!/" />
21+
</CLASSES>
22+
<JAVADOC />
23+
<SOURCES />
24+
</library>
25+
</orderEntry>
26+
<orderEntry type="module-library">
27+
<library>
28+
<CLASSES>
29+
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-5.4.2.jar!/" />
30+
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-api-5.4.2.jar!/" />
31+
<root url="jar://$MODULE_DIR$/lib/apiguardian-api-1.0.0.jar!/" />
32+
<root url="jar://$MODULE_DIR$/lib/opentest4j-1.1.1.jar!/" />
33+
<root url="jar://$MODULE_DIR$/lib/junit-platform-commons-1.4.2.jar!/" />
34+
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-params-5.4.2.jar!/" />
35+
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-engine-5.4.2.jar!/" />
36+
<root url="jar://$MODULE_DIR$/lib/junit-platform-engine-1.4.2.jar!/" />
37+
</CLASSES>
38+
<JAVADOC />
39+
<SOURCES />
40+
</library>
41+
</orderEntry>
1042
</component>
1143
</module>
2.11 KB
Binary file not shown.
5.77 KB
Binary file not shown.
131 KB
Binary file not shown.
174 KB
Binary file not shown.
500 KB
Binary file not shown.
87.3 KB
Binary file not shown.
139 KB
Binary file not shown.

BattleShip/lib/opentest4j-1.1.1.jar

6.95 KB
Binary file not shown.

BattleShip/src/com/luox6/battleship/Main.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.luox6.battleship.gui.factory.Dialog;
66
import com.luox6.battleship.gui.model.PlayerBoard;
77
import com.luox6.battleship.gui.model.UserSetting;
8-
import com.luox6.battleship.model.GameBoard;
98
import com.luox6.battleship.model.ship.StandbyShip;
109
import com.luox6.battleship.network.Client;
1110
import com.luox6.battleship.network.Connectable;
@@ -15,11 +14,15 @@
1514
import javax.swing.*;
1615
import java.util.ArrayList;
1716
import java.util.List;
18-
import java.util.concurrent.Callable;
1917
import java.util.concurrent.ExecutorService;
2018
import java.util.concurrent.Executors;
2119
import java.util.concurrent.Future;
2220

21+
/**
22+
* Application Battleship main entry
23+
* @author Xinhao Luo
24+
* @version 0.0.1
25+
*/
2326
public class Main {
2427
/**
2528
* This main method is main for test purpose.
@@ -64,6 +67,7 @@ public static void main(String[] args) {
6467
final Protocol[] pr = new Protocol[1];
6568
final Connectable[] c = new Connectable[1];
6669
ExecutorService executor = Executors.newSingleThreadExecutor();
70+
// Sync process
6771
Future<Boolean> res = executor.submit(() -> {
6872
try {
6973
if (m == Protocol.Mode.SERVER) {
@@ -133,6 +137,7 @@ public static void main(String[] args) {
133137

134138
initDialog.setVisible(true);
135139

140+
// Wait for process
136141
try {
137142
Boolean r = res.get();
138143
if (!r) {
@@ -159,12 +164,19 @@ public static void main(String[] args) {
159164
if (response != null) {
160165
c[0].send(response);
161166
}
162-
} while (true);
167+
} while (!c[0].isConnectionClosed());
163168
});
164169
// Launch GUI
165170
guiController.start();
166171
}
167172

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+
*/
168180
private static void awaitSync(Connectable[] c, Protocol[] pr) {
169181
c[0].send(Protocol.GameCommand.NEXT.toString());
170182
do {
@@ -178,7 +190,11 @@ private static void awaitSync(Connectable[] c, Protocol[] pr) {
178190
} while (true);
179191
}
180192

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() {
182198
String[] config = Dialog.clientInitialDialog(null);
183199
if (config == null) {
184200
System.exit(1);
@@ -188,6 +204,7 @@ public static Object[] getClientInfo() {
188204
String address = config[0];
189205
int port = Integer.parseInt(config[1]);
190206
String name = config[2];
207+
validName(name);
191208

192209
if (name.length() < 1) {
193210
throw new Exception("Name must have at least 1 characters");
@@ -207,7 +224,11 @@ public static Object[] getClientInfo() {
207224
}
208225
}
209226

210-
public static Object[] getServerInfo() {
227+
/**
228+
* Dialog for Server Info
229+
* @return [String address, int port]
230+
*/
231+
private static Object[] getServerInfo() {
211232
String[] config = Dialog.serverInitialDialog(null);
212233
if (config == null) {
213234
System.exit(1);
@@ -216,16 +237,13 @@ public static Object[] getServerInfo() {
216237
try {
217238
int port = Integer.parseInt(config[0]);
218239
String name = config[1];
240+
validName(name);
219241

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;
227245

228-
throw new Exception("Name must be at least 1 character");
246+
return res;
229247
} catch (NumberFormatException e) {
230248
Dialog.numberParseDialog(null, e);
231249
return null;
@@ -234,4 +252,28 @@ public static Object[] getServerInfo() {
234252
return null;
235253
}
236254
}
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+
}
237279
}

0 commit comments

Comments
 (0)