-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleClient.java
More file actions
147 lines (122 loc) · 4.24 KB
/
Copy pathBattleClient.java
File metadata and controls
147 lines (122 loc) · 4.24 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class BattleClient {
public static int turn = -1;
public static void main(String[] args) {
GameBoard playerBoard = new GameBoard();
boolean winCondition = true;
Scanner input = new Scanner(System.in);
System.out.println("Enter the Server IP Address");
String ipaddress = input.nextLine();
try {
//connect to host
System.out.println("Client Started");
Socket soc = new Socket(ipaddress, 9806);
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(soc.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
//get number of boats
System.out.println("Waiting for host to set-up game");
int boatsNumber = Integer.parseInt(in.readLine());
System.out.println(boatsNumber);
turn = 2;
playerBoard.generateBoats(boatsNumber);
//print boards
System.out.println("\nPlayer Board \t\t\tEnemyBoard: ");
playerBoard.printBoard();
while (turn > 0) {
//turn 0 = win/lose, turn 1 = player 1, turn 2 = player 2
if (turn == 1) {
// input coordinate
System.out.println("Enter Coordinates:");
// check if coordinates are valid (A-H 0-9)
boolean validInput = false;
String coords = userInput.readLine();
int xcoord = -1, ycoord = -1;
while (!validInput) {
//2 characters only
if (coords.length() == 2 &&
(coords.charAt(0) >= 65 && coords.charAt(0) <= 74) &&
(coords.charAt(1) >= 48 && coords.charAt(1) <= 57)) {
String y = coords.charAt(1) + "";
xcoord = (int) coords.charAt(0) - 65;
ycoord = Integer.parseInt(y);
validInput = true;
} else {
coords = userInput.readLine();
}
}
//send coordinates to client
System.out.println("Shooting at: (" + coords.charAt(0) + "," + ycoord + ")");
out.println(coords);
//receive response
String hitMiss = in .readLine();
boolean isHit = true;
clearScreen();
//convert string to boolean
if (hitMiss.equals("false")) {
isHit = false;
} else if (hitMiss.equals("true")) {
isHit = true;
}
playerBoard.returnHit(xcoord, ycoord, isHit);
//check if win
if (playerBoard.score == boatsNumber) {
clearScreen();
playerBoard.matchResult(false);
winCondition = true;
turn = 0;
} else {
//print enemy boards again
System.out.println("\nPlayer Board \t\t\tEnemyBoard: ");
playerBoard.printBoard();
//end turn
turn = 2;
}
} else if (turn == 2) {
//send waiting message
System.out.println("Waiting for other player...");
// receive coordinates from player 1
String coords = in .readLine();
//convert string to x,y coordinates
int xcoord = (int) coords.charAt(0) - 65;
int ycoord = Integer.parseInt(coords.charAt(1) + "");
System.out.println("Enemy fired at: (" + coords.charAt(0) + "," + ycoord + ")");
boolean isHit = playerBoard.checkhit(xcoord, ycoord);
//send to player 1
out.println(isHit);
//check if lose
if (playerBoard.boats == 0) {
winCondition = false;
turn = 0;
} else {
//end turn
turn = 1;
}
}
}
//closing everything
soc.close();
userInput.close();
in.close();
out.close();
input.close();
} catch (Exception e) {
}
//print match results
if (winCondition) {
clearScreen();
playerBoard.matchResult(false);
} else {
clearScreen();
playerBoard.matchResult(true);
}
}
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}