-
Notifications
You must be signed in to change notification settings - Fork 0
/
BattleshipClient.java
390 lines (384 loc) · 11.7 KB
/
BattleshipClient.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
/* Programmers: Jared McDonald
*Class: CSC 435, Spring 2016
*Instructor: Dr. Cook
* Program Purpose:
* a. Program takes in command line arguments of a hostname and port number.
* b. Program connects to a socket created by a EchoServer using the hostname and port number
* c. Program sends out a READY signal to the socket
* d. Program waits for a READY signal from the socket (sent by a BattleshipHost object)
* e. Program asks player to place ships on board
* f. Program either:
* i. Sends a move to the other player over the socket
* ii. Recieves a move from the other player over the socket
* g. Program sends/recieves a hit or miss over the socket
* h. Program ends when either all ships have been destroyed or the battleship has been destroyed
*/
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class BattleshipClient { //Also known as EchoClient
static String ships[] = {"Battleship (6 spaces)","Carrier (5 spaces)","Destroyer (4 spaces)","Submarine (3 spaces)","Patrol (2 spaces)"};
public static char board[][] = new char[10][10];
public static char fired[][] = new char[10][10];
static int shipsplaced = 0;
static Scanner sc = new Scanner(System.in);
static int battleshiphealth = 6;
static int carrierhealth = 5;
static int destroyerhealth = 4;
static int submarinehealth = 3;
static int patrolhealth = 2;
static int read = 0;
static int row = 0;
static String ship = null;
static int col = 0;
static int length = 0;
static char mode = ' ';
public static void main(String[] args) throws Exception {
if(args.length != 2){
System.err.println("Necessary command line arguments: <hostname> <portNumber>");
System.exit(1);
}
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
while(true){
//SendMode
try{
Socket echoSocket = new Socket(hostName,portNumber);
PrintWriter outSend = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader inSend = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
outSend.println("READY");
System.out.println("Waiting for READY signal...");
while((userInput= inSend.readLine()) != null){
if(userInput.contains("READY")){
System.out.println("READY Recieved");
break;
}
else{
outSend.println("READY");
}
}
fillBoard();
placement();
shipsplaced = 5;
System.out.println("Waiting to Recieve message...");
while((userInput = inSend.readLine()) != null){
//POINT B READING
//At the point the server response is captured within userInput
System.out.println("\tRecieved "+userInput);
if(userInput.contains("MOVE")){
shot(userInput,outSend);
}
else if(userInput.contains("BATTLESHIP")){ //Win condition
System.out.println("You Win!");
System.exit(0);
}
System.out.print("Type Message FORMAT( MOVE {A-J} {1-10} >>> ");
printFired();
userInput = stdIn.readLine();
if(transmit() == 1){
System.out.println("Sent "+userInput+" successfully.");
updateFired(userInput);
outSend.println(userInput);
//Listen for Hit or miss
userInput = inSend.readLine();
System.out.println(userInput);
}
else{
System.out.println("Tried to send "+userInput+" but it failed.");
TimeUnit.SECONDS.sleep(10);
outSend.println("TIMEOUT - EXPECTING A MOVE");
}
//POINT A SENDING
//User Input is withing the userInput variable at this point
//The below function is the variable that is sent to the server
System.out.println("Waiting to Recieve message...");
}
echoSocket.close();
}
catch(UnknownHostException e){
System.err.println("Don't know about host "+hostName);
System.exit(1);
}
catch(IOException e){
System.err.println("Couldn't get I/O for the connection to "+hostName);
System.exit(1);
}
}
}
public static int transmit(){
int chance = 1 + (int)(Math.random() * ((10 - 1) + 1));
if(chance > 1){
return 1;
}
else{
return 0;
}
}
public static void updateFired(String str){ //RECIEVED HIT
str = str.substring(5).trim();
char ch = str.charAt(0);
String temp = str.substring(str.indexOf(" ")).trim();
int col = Integer.parseInt(temp)-1;
int row = (int)ch-65;
fired[row][col] = '!';
}
public static void shot(String str, PrintWriter out){
str = str.substring(4).trim();
char ch = str.charAt(0);
String temp = str.substring(str.indexOf(" ")).trim();
System.out.println("Shot receieved "+str);
int col = Integer.parseInt(temp)-1;
int row = (int)ch-65;
char tile = board[row][col];
if(tile != '~' && tile != '*' && tile != '!'){
hit(tile,out);
board[row][col] = '!';
}
else if(tile == '!'){
System.out.println("Shot missed!");
out.println("MISS.");
}
else{
System.out.println("Shot missed!");
out.println("MISS.");
miss(row,col);
}
}
public static void hit(char c,PrintWriter out){ //
if(c == 'B'){
battleshiphealth -= 1;
if(battleshiphealth == 0){
out.println("YOU SUNK MY BATTLESHIP. YOU WIN! GAME OVER.");
System.out.println("YOU LOSE. BATTLESHIP SUNKEN.");
System.exit(0);
}
else{
System.out.println("Battleship hit!");
out.println("HIT.");
}
}
else if(c == 'C'){
carrierhealth -= 1;
if(carrierhealth == 0){
shipsplaced--;
out.println("YOU SUNK MY CARRIER. I HAVE "+shipsplaced+" SHIPS LEFT.");
}
else{
System.out.println("Carrier hit!");
out.println("HIT.");
}
}
else if(c == 'D'){
destroyerhealth -= 1;
if(destroyerhealth == 0){
shipsplaced--;
out.println("YOU SUNK MY DESTROYER. I HAVE "+shipsplaced+" SHIPS LEFT.");
}
else{
System.out.println("Destroyer hit!");
out.println("HIT.");
}
}
else if(c == 'S'){
submarinehealth -= 1;
if(submarinehealth == 0){
shipsplaced--;
out.println("YOU SUNK MY SUBMARINE. I HAVE "+shipsplaced+" SHIPS LEFT.");
}
else{
System.out.println("Submarine hit!");
out.println("HIT.");
}
}
else if(c == 'P'){
patrolhealth -= 1;
if(patrolhealth == 0){
shipsplaced--;
out.println("YOU SUNK MY PATROL. I HAVE "+shipsplaced+" SHIPS LEFT.");
}
else{
System.out.println("Patrol hit!");
out.println("HIT.");
}
}
}
public static void printFired(){
System.out.println("Tiles fired upon:");
System.out.println("* = MISS, ~ = OPEN WATER, ! = HIT");
for(int i = 0;i < 10;i++){
System.out.print((char)(i+65)+" ");
for(int j = 0;j < 10;j++){
System.out.print(fired[i][j]+" ");
}
System.out.println();
}
}
public static void placement(){
while(shipsplaced != 5){
System.out.println("You still have ships to place on the board");
System.out.println("Press the number next to the ship you want to place.");
for(int i = 0;i < 5;i++){
if(ships[i] != null){
System.out.println(i+". "+ships[i]);
}
}
System.out.println("Ship #: ");
read = sc.nextInt();
if((read >= 11 || read < 0) && ships[read] != null){
System.out.println("Invalid number.");
}
else{
ship = ships[read];
System.out.println("Where do you want to put your "+ships[read]+"? You can put it anywhere there is a ~ (open water).");
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
for(int i = 0;i < 10;i++){
System.out.print(i+" ");
for(int j = 0;j < 10;j++){
System.out.print(board[i][j]+" ");
}
System.out.println();
}
boolean validspot = false;
while(validspot == false){
System.out.println("Which row do you want the head of the ship located? ");
row = sc.nextInt();
System.out.println("Row "+row+". Which column do you want the head of the ship located? ");
col = sc.nextInt();
System.out.println("Row "+row+", Column "+col+". Horizontally (enter H) or Vertically (enter V).");
sc.nextLine();
String temp = sc.nextLine();
mode = temp.charAt(0);
if((0 <= row && row <= 9) && (0 <= col && col <= 9) && (mode == 'h'||mode == 'v'||mode == 'H'||mode == 'V')){
validspot = true;
}
else{
System.err.println("Please enter a number 0-9 for the rows and columns and H or V for orientaion.");
}
}
if(ship.contains("6")){
length = 6;
}
else if(ship.contains("5")){
length = 5;
}
else if(ship.contains("4")){
length = 4;
}
else if(ship.contains("3")){
length = 3;
}
else if(ship.contains("2")){
length = 2;
}
if(TilesEmpty(row,col,length,mode) == true){
addShip(row,col,ship,length,mode);
System.out.println("Added "+ship+".");
printBoard();
shipsplaced++;
ships[read] = null;
}
else{
System.err.println("You cannot put a ship there in that position. Pick somewhere else.");
}
}
}
}
public static void addShip(int row, int col, String ship, int length, char mode) {
char letter = ship.charAt(0);
if(mode == 'V'|| mode == 'v'){ //Vertical
for(int i = row; i < (row+length);i++){
board[i][col] = letter;
}
}
else if(mode == 'H' || mode == 'h'){ //Horizontal
for(int i = col; i <(col+length);i++){
board[row][i] = letter;
}
}
}
public static boolean TilesEmpty(int row, int col, int length, char mode) {
if(row+length > 9 ||col+length >9){
return false;
}
else{
if(mode == 'H'|| mode == 'h'){
int max = row + length;
for(int i = row;i < max;i++){
if(board[i][col] != '~'){
System.err.println("One or more tiles in the selection is occupied.");
return false;
}
if(i == max-1){
return true;
}
}
return true;
}
else{
int max = col + length;
for(int i = col;i < max;i++){
if(board[row][i] != '~'){
System.err.println("One or more tiles in the selection is occupied.");
return false;
}
if(i == max-1){
return true;
}
}
return true;
}
}
}
public static void fillBoard(){
for(int i = 0;i < 10;i++){
for(int j = 0;j < 10;j++){
board[i][j] = '~';
fired[i][j] = '~';
}
}
}
public static void miss(int r,int c){
board[r][c] = '*';
fired[r][c] = '*';
}
public static void printBoards(){
System.out.println("Tiles fired upon:");
System.out.println("* = MISS, ~ = OPEN WATER, ! = HIT");
for(int i = 0;i < 10;i++){
System.out.print(i+" ");
for(int j = 0;j < 10;j++){
System.out.print(fired[i][j]+" ");
}
System.out.println();
}
System.out.println("Your board:");
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
for(int i = 0;i < 10;i++){
System.out.print(i+" ");
for(int j = 0;j < 10;j++){
System.out.print(board[i][j]+" ");
}
System.out.println();
}
System.out.println("* = MISS, ~ = OPEN WATER, ! = HIT");
System.out.println("B = Battleship, C = Carrier, D = Destroyer");
System.out.println("S = Submarine, P = Patrol\n");
}
public static void printBoard(){
System.out.println("Your board:");
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
for(int i = 0;i < 10;i++){
System.out.print(i+" ");
for(int j = 0;j < 10;j++){
System.out.print(board[i][j]+" ");
}
System.out.println();
}
System.out.println("* = MISS, ~ = OPEN WATER, ! = HIT");
System.out.println("B = Battleship, C = Carrier, D = Destroyer");
System.out.println("S = Submarine, P = Patrol\n");
}
}