-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleServer.java
More file actions
167 lines (135 loc) · 4.84 KB
/
Copy pathBattleServer.java
File metadata and controls
167 lines (135 loc) · 4.84 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.net.ServerSocket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.InetAddress;
public class BattleServer {
public static int turn = 1;
public static void main(String[] args) {
// start game (maybe put this in another function?)
GameBoard playerBoard = new GameBoard();
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Your current IP address : " + ip);
} catch (Exception e) {
}
try {
// establishing connection
System.out.println("Waiting for Other Player...");
ServerSocket ss = new ServerSocket(9806);
Socket soc = ss.accept();
System.out.println("Connection established");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); // get user input
PrintWriter out = new PrintWriter(soc.getOutputStream(), true); // output to other player
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream())); // input from other player
//get number of boats
System.out.println("Enter the number of ships");
int boatsNumber = Integer.parseInt(userInput.readLine());
out.println(boatsNumber);
playerBoard.generateBoats(boatsNumber);
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.addscore();
}
playerBoard.returnHit(xcoord, ycoord, isHit);
//check if win
if (playerBoard.score == boatsNumber) {
clearScreen();
playerBoard.matchResult(false);
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) {
playerBoard.matchResult(true);
turn = 0;
} else {
//end turn
turn = 1;
}
}
}
//closing everything
userInput.close();
in .close();
out.close();
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
public void printBoards() {
}
public static boolean isNumeric(String string) {
int intValue;
System.out.println(String.format("Parsing string: \"%s\"", string));
if (string == null || string.equals("")) {
System.out.println("String cannot be parsed, it is null or empty.");
return false;
}
try {
intValue = Integer.parseInt(string);
return true;
} catch (NumberFormatException e) {
System.out.println("Input String cannot be parsed to Integer.");
}
return false;
}
}