-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTigerZoneClient.java
More file actions
110 lines (92 loc) · 2.93 KB
/
Copy pathTigerZoneClient.java
File metadata and controls
110 lines (92 loc) · 2.93 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
import java.io.*;
import java.net.*;
public class TigerZoneClient {
public static void main(String[] args) throws IOException {
if (args.length != 5) {
System.err.println(
"Usage: java TigerZoneClient <host name> <port number> <tournament password> <username> <password>");
return;
}
System.err.println("Client opened");
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
String tournamentPassword = args[2];
String username = args[3];
String password = args[4];
try (
Socket tzSocket = new Socket(hostName, portNumber);
PrintWriter networkOut = new PrintWriter(tzSocket.getOutputStream(), true);
BufferedReader networkIn = new BufferedReader(new InputStreamReader(tzSocket.getInputStream()));
) {
String fromServer;
String[] serverText = null;
boolean isWaiting = true;
boolean isPlaying = true;
boolean startingMatch = true;
// Getting verified by server
fromServer = networkIn.readLine();
System.out.println("Server: " + fromServer);
if(fromServer.equals("THIS IS SPARTA!")){
networkOut.println("JOIN " + tournamentPassword);
}
fromServer = networkIn.readLine();
System.out.println("Server: " + fromServer);
if (fromServer.equals("HELLO!")){
networkOut.println("I AM " + username + " " + password);
}
while(startingMatch){
NetworkAdapter netAdapter = new NetworkAdapter();
// Getting game info - Player ID - Starting Tile - Order of Tiles
isWaiting = true;
while(isWaiting){
fromServer = networkIn.readLine();
if (null == fromServer) continue;
serverText = fromServer.split(" ");
System.out.println("Server: " + fromServer);
if(serverText[0].equals("THANK")){
startingMatch = false;
System.exit(0);
}
if(serverText[0].equals("MATCH"))
{
isWaiting = false;
}
else {
netAdapter.parseMatchProtocol(fromServer);
}
}
// Where we start Receiving notification to send move / Send our moves.
isPlaying = true;
while(isPlaying){
fromServer = networkIn.readLine();
System.out.println("Server: " + fromServer);
serverText = fromServer.split(" ");
switch(serverText[0]){
case "MAKE":
String returnMessage = netAdapter.parseMakeMove(fromServer);
System.out.println(returnMessage);
networkOut.println(returnMessage);
break;
case "GAME":
if(serverText[6].equals("FORFEITED:")){
}
else if(serverText[2].equals("OVER")){
}
else{
netAdapter.parseUpdateGameBoard(fromServer);
}
break;
case "END":
isPlaying = false;
break;
}
}
}
}
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
}
System.err.println("Client shutting down");
}
}