-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToeClient.java
59 lines (52 loc) · 1.56 KB
/
TicTacToeClient.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
package tictactoe;
import java.io.*;
import java.net.*;
public class TicTacToeClient {
private int PORT = 45000;
private Socket socket;
private PrintWriter out;
private BufferedReader in;
private Frame gameFrame;
public TicTacToeClient(String hostname) {
try {
socket = new Socket(hostname, PORT);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
gameFrame = new Frame(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public void start() {
new Thread(new Receiver()).start();
}
private class Receiver implements Runnable {
@Override
public void run() {
try {
String line;
while ((line = in.readLine()) != null) {
gameFrame.update(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void submitPlayerName(String playerName) {
out.println("NAME_SUBMITTED");
}
public void sendMove(String move) {
out.println(move);
}
public void sendExitMessage() {
out.println("EXIT_GAME");
}
public void sendRestart() {
out.println("RESTART");
}
public static void main(String[] args) {
TicTacToeClient client = new TicTacToeClient("localhost"); // replace localhost with the server hostname
client.start();
}
}