-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTigerZoneServer.java
91 lines (77 loc) · 3.33 KB
/
TigerZoneServer.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
import java.net.*;
import java.io.*;
public class TigerZoneServer {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java TigerZoneServer <port number>");
return;
}
System.out.println("Server started");
int portNumber = Integer.parseInt(args[0]);
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
String inputLine, outputLine;
// Initiate conversation with client
TigerZoneProtocol tzp = new TigerZoneProtocol();
outputLine = tzp.VerifyAuthentication(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = tzp.VerifyAuthentication(inputLine);
out.println(outputLine);
if (outputLine.equals("WELCOME Red PLEASE WAIT FOR THE NEXT CHALLENGE"))
break;
}
outputLine = "NEW CHALLENGE 1 YOU WILL PLAY 3 MATCHES";
out.println(outputLine);
int i = 0;
while(i < 3){
TigerZoneProtocol tzProtocol = new TigerZoneProtocol();
outputLine = "BEGIN ROUND " + (i+1) + " OF 3";
out.println(outputLine);
outputLine = "YOUR OPPONENT IS PLAYER Blue";
out.println(outputLine);
outputLine = "STARTING TILE IS TLTJ- AT 0 0 0";
out.println(outputLine);
outputLine = tzProtocol.StartGame();
out.println(outputLine);
outputLine = "MATCH BEGINS IN 15 SECONDS";
out.println(outputLine);
/* try {
Thread.sleep(15000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
} */
boolean isGamePlaying = true;
while(isGamePlaying)
{
outputLine = tzProtocol.NotifyPlayer();
out.println(outputLine);
if(outputLine.equals("END OF ROUND"))
isGamePlaying = false;
else{
inputLine = in.readLine();
}
outputLine = tzProtocol.SendGameAMove(inputLine);
out.println(outputLine);
outputLine = tzProtocol.SendGameBMove(inputLine);
out.println(outputLine);
}i++;
outputLine = "PLEASE WAIT FOR NEXT CHALLENGE TO BEGIN";
out.println(outputLine);
}
outputLine = "THANK YOU FOR PLAYING! GOODBYE ";
out.println(outputLine);
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
System.out.println("Server shutting down");
}
}