-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
143 lines (118 loc) · 3.61 KB
/
Game.java
File metadata and controls
143 lines (118 loc) · 3.61 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
/**
* Game.java - lab2
* Handles the game logic.
*/
import lib.Board;
import lib.TCPStream;
import lib.UDPStream;
import lib.NetworkStream;
import lib.DataListener;
import java.util.Scanner;
class Game {
private static Board board = new Board();
private static Scanner stdin = new Scanner(System.in);
private static NetworkStream stream;
private static boolean master = false;
private static String myChar;
// randomly generate number to decide who goes first
private final static double A = Math.random();
public static void main(String[] args) {
String rawinput, host;
String[] input;
int port;
do {
System.out.print("Enter the connection string or a port number to listen as TCP server [protocol://host:port/]: ");
rawinput = stdin.nextLine().trim();
} while (!rawinput.matches("[0-9]+") && !rawinput.matches("[a-zA-Z]+\\://[a-zA-Z0-9.]+\\:[0-9]+(/?)"));
input = rawinput.split(":");
if (rawinput.matches("[0-9]+")) {
stream = new TCPStream(Integer.parseInt(rawinput, 10));
System.out.format("\nAwaiting connection @:%s ...\n", rawinput);
} else {
host = input[1].replaceAll("[^a-zA-Z0-9.]+", "");
port = Integer.parseInt(input[2].replaceAll("[^0-9]+", ""), 10);
if (input[0].equals("tcp")) stream = new TCPStream(host, port);
else {
do {
System.out.print("Enter a port to listen on: ");
rawinput = stdin.nextLine();
} while (!rawinput.matches("[0-9]+"));
stream = new UDPStream(host, port, Integer.parseInt(rawinput, 10));
}
System.out.format("\nConnecting to %s:%s ...\n", host, port);
}
// add all appropriate event handlers
final NetworkStream me = stream;
stream.onConnect(new Runnable() {
public void run() {
me.send("M:" + A);
}
});
stream.onDisconnect(new Runnable() {
public void run() {
System.exit(0);
}
});
stream.onError(new DataListener() {
public void eventHandler(String data) {
System.out.format("\nError: %s\n", data);
System.exit(-1);
}
});
stream.receive(new DataListener() {
public void eventHandler(String data) {
String[] input = data.split(":");
switch (input[0]) {
case "M":
double B = Double.parseDouble(input[1]),
C = A + B; // 0 <= C <= 2
master = A > B;
board.setTurn(C >= 1 ? "X" : "O");
myChar = board.getTurn();
if (!master) myChar = myChar.equals("X") ? "O" : "X";
break;
case "P":
input = input[1].split(",");
board.play(
Integer.parseInt(input[0], 10),
Integer.parseInt(input[1], 10)
);
break;
default:
break;
}
nextTurn();
}
});
}
private static void nextTurn() {
int victor;
if ((victor = board.checkWinStatus()) > 0) {
System.out.format("\n%s won!\n", !board.getTurn().equals(myChar) ? "You" : "The opponent");
stream.close();
return;
} else if (victor == -1) {
System.out.println("\nNobody won... try again later.");
stream.close();
return;
}
System.out.format(
"\n%s\nPlayer turn: %s\n%s",
board, board.getTurn(),
board.getTurn().equals(myChar) ? "Enter your place [x,y]: " : "Awaiting opponent's play ...\n"
);
if (!board.getTurn().equals(myChar)) return;
String[] input = stdin.nextLine().split(",");
int x = Integer.parseInt(input[0], 10),
y = Integer.parseInt(input[1], 10);
board.play(x, y);
stream.send("P:" + x + "," + y);
if ((victor = board.checkWinStatus()) > 0) {
System.out.format("\n%s won!\n", !board.getTurn().equals(myChar) ? "You" : "The opponent");
stream.close();
} else if (victor == -1) {
System.out.println("\nNobody won... try again later.");
stream.close();
} else nextTurn();
}
}