-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleship.java
369 lines (311 loc) · 10.2 KB
/
Battleship.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
import java.awt.event.*; // Use some events
import java.util.*; // Util
import java.io.*; //I/O
import java.net.*; //Sockets
/**
* This class is the main class for playing Battleships.
*
* @author Jerrett Fowler
* @version 1.2 (August 2013)
*/
public class Battleship
{
private enum GameState
{
INITIALISE, SERVERREADY, SERVERTURN
}
private GameState gameState = GameState.INITIALISE;
private Parser parser;
private Protocol protocol;
Player currentPlayer;
Grid grids;
Fleet fleet;
/**
* Main
*/
public static void main(String[] args) throws IOException
{
int i = 0, portArg;
String arg = new String();
String machine = new String();
boolean vflag = false;
while (i < args.length && args[i].startsWith("-"))
{
arg = args[i++];
if(arg.equals("-host"))
{
if (i < args.length)
{
System.out.println("Host");
portArg = Integer.parseInt(args[i++]);
new Battleship("Host","", portArg);
}
else
{
System.err.println("-host requires a port number");
}
}
else if(arg.equals("-join"))
{
if (i < args.length)
{
System.out.println("Client");
machine = args[i++];
portArg = Integer.parseInt(args[i++]);
new Battleship("Client", machine, portArg);
}
else
{
System.err.println("-host requires a port number");
}
}
else
{
System.err.println("Format: java Battleship [-host/-join] [Machine Name] [Port number]");
}
}
}
/**
* Create the game and initialise the grids
*/
public Battleship(String gameState, String serverName, int portNumber)
{
parser = new Parser();
protocol = new Protocol();
//create player
setPlayer();
//set up fleet
newFleet();
//set up new grids
createGrids();
switch(gameState)
{
case "Client":
client(serverName, portNumber);
//player sets up grids, sends <READY> message
System.out.println("Client Breaks");
break;
case "Host":
host(portNumber);
//host randomly decides <FIRSTGO> and sends message to user on decision
//player sets up grids, sends <READY> message
System.out.println("Host Breaks");
break;
default: System.err.println("That went wrong");
}
}
/**
* Start Client
*/
public void client(String serverName, int portNumber)
{
System.out.println("Gamestate = Client");
Socket connection = null;
//PrintWriter out = null;
//BufferedReader in = null;
boolean end = false;
Command YES = protocol.getCommand("YES");
Command NO = protocol.getCommand("NO");
Command END = protocol.getCommand("END");
//sends connection request to host
try {
System.out.println("Client tries connection on port: " + portNumber);
connection = new Socket(serverName, portNumber);
System.out.println("Connection established");
} catch (IOException e) {
System.err.println("Could not find server. Try again.");
System.exit(1);
}
//responds to <SUPPORTS> message with <YES> or <NO> to each
//items set as <NO> will go unsupported on the host
//Play Game
play();
try {
connection.close();
} catch (IOException e) {
System.err.println("Could not find close connection.");
}
//out.close();
//in.close();
//connectionToTheServer.close();
}
/**
* Start Server
*/
public void host(int portNumber)
{
System.out.println("Gamestate = Host");
//Creates ServerSocket
ServerSocket server = null;
boolean end = false;
Command START = protocol.getCommand("START");
Command SUPPORTS = protocol.getCommand("SUPPORTS");
Command YES = protocol.getCommand("YES");
Command NO = protocol.getCommand("NO");
Command END = protocol.getCommand("END");
try {
server = new ServerSocket(portNumber);
System.out.println("Host set up on port: " + portNumber);
} catch (IOException e) {
System.err.println("Could not listen on port: " +portNumber);
System.exit(1);
}
//listens for connection request
Socket client = null;
try {
System.out.println("Waiting on connection...");
client = server.accept();
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream();
} catch (IOException e) {
System.err.println("Cannot accept connection. Failed.");
System.exit(1);
}
//START.execute(getPlayer());
//sends <START> message to client
//sends a <SUPPORTS> message to client for each feature used
//when host cycles through all <SUPPORTS>, it sends an <END>
//Play Game
play();
try {
server.close();
} catch (IOException e) {
//
}
}
/**
* Main play routine. Loops until one player's fleet is sunk.
*/
public void play()
{
printOpening();
System.out.println(" GAME BOARD");
if(gameState == GameState.INITIALISE) //change this when READY works
{
grids.addShipFromFleet(fleet.getShipInFleet("SUBMARINE1"));
grids.addShipFromFleet(fleet.getShipInFleet("SUBMARINE2"));
grids.addShipFromFleet(fleet.getShipInFleet("SUBMARINE3"));
grids.addShipFromFleet(fleet.getShipInFleet("BATTLESHIP1"));
grids.addShipFromFleet(fleet.getShipInFleet("DESTROYER1"));
grids.addShipFromFleet(fleet.getShipInFleet("DESTROYER2"));
grids.printGrid();
}
System.out.println();
System.out.println("Board is set up for you. Command for Placing a ship is not fully implemented.");
//Main gameplay loop
while(!getPlayer().getEnd())
{
Command command = parser.getCommand();
if(command == null)
{
System.out.println("Please enter your orders again, ");
System.out.println("and remember that orders are in ALL CAPS...");
}
else if(command.getName().equals("READY"))
{
if(gameState == GameState.INITIALISE)
{
command.execute(getPlayer());
}
else
{
System.out.println("Already ready...?");
}
}
else if(command.getName().equals("END"))
{
command.execute(getPlayer());
}
else if(command.getName().equals("HELP"))
{
command.execute(parser.getCommandWords());
}
else if(command.getName().equals("TALK"))
{
command.execute(getPlayer());
//send message to other player
}
else if(command.getName().equals("FIRE"))
{
command.execute(getPlayer());
//send message to other player
}
else if(command.getName().equals("MISS"))
{
command.execute(getPlayer());
//send message to other player
}
else if(command.getName().equals("HIT"))
{
command.execute(getPlayer());
//edit Grid
//send message to other player
}
else if(command.getName().equals("SUNK"))
{
command.execute(getPlayer());
//remove ship from ArrayList
//send message to other player
}
else if(command.getName().equals("ILOSE"))
{
command.execute(getPlayer());
//send message to other player
}
}
printClosing();
}
/**
* Opening statement
*/
private void printOpening()
{
System.out.println();
System.out.println();
System.out.println("Welcome to Battleships!");
System.out.println("Type 'HELP' if you need help.");
System.out.println("All commands are in ALL CAPS.");
System.out.println();
}
/**
* Closing statement
*/
private void printClosing()
{
System.out.println();
System.out.println();
System.out.println("Sorry to see you go soldier.");
System.out.println("Game has ended.");
System.out.println();
}
/**
* A new player
*/
public void setPlayer()
{
Player player = new Player("Player");
currentPlayer = player;
}
/**
* Return the player
*/
public Player getPlayer()
{
return currentPlayer;
}
/**
* Engage your Fleet
*/
public void newFleet()
{
fleet = new Fleet();
}
/**
* Create grids
*/
private void createGrids()
{
// Create player grid
grids = new Grid();
}
}