-
Notifications
You must be signed in to change notification settings - Fork 5
/
Client.java
142 lines (133 loc) · 3.4 KB
/
Client.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
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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public final class Client extends Frame {
private static final long serialVersionUID = 0L;
private Server.States[][] grid = new Server.States[3][3];
private Socket server;
private boolean done = false, won, tie = false;
private Getter get = new Getter();
public Client(String server) throws UnknownHostException, IOException {
this(server, Protocol.DEFAULTPORT);
}
public Client(String server, int port) throws UnknownHostException, IOException {
super("Tic-Tac-Toe");
setSize(300, 300);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((int)(Math.random()*(dim.getHeight()-300)), (int)(Math.random()*(dim.getHeight()-300)));
setResizable(false);
try {
this.server = new Socket(server, port);
} catch (IOException e) {
dispose();
throw e;
}
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing (WindowEvent e) {
done = true;
get.interrupt();
try {
Client.this.server.close();
} catch (IOException io) {}
dispose();
}
});
addMouseListener(new Mouse());
Server.clearGrid(grid);
get.start();
}
private void set(int x, int y) {
try {
int b = (x&3) << 2;
b |= (y&3);
server.getOutputStream().write(b);
} catch (IOException e) {
new MessageBox(getTitle(), "Connection failed: " + e.getMessage());
setVisible(false);
}
}
@Override
public void paint(Graphics g) {
g.drawLine(100, 0, 100, 300);
g.drawLine(200, 0, 200, 300);
g.drawLine(0, 100, 300, 100);
g.drawLine(0, 200, 300, 200);
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == Server.States.X) {
g.drawString("X", i*100+50, j*100+50);
} else if (grid[i][j] == Server.States.O) {
g.drawString("O", i*100+50, j*100+50);
}
}
}
if (done) {
if (tie) {
g.drawString("TIE", 100, 100);
} else {
if (won) {
g.drawString("WINNER", 100, 100);
} else {
g.drawString("LOSER", 100, 100);
}
}
}
}
private class Getter extends Thread {
@Override
public void run() {
while (!isInterrupted()) {
try {
byte got = (byte)server.getInputStream().read();
int x = Protocol.x(got);
int y = Protocol.y(got);
if (Protocol.opcode(got) == Protocol.SETX) {
grid[x][y] = Server.States.X;
} else if (Protocol.opcode(got) == Protocol.SETO) {
grid[x][y] = Server.States.O;
}
if (Protocol.opcode(got) == Protocol.WIN) {
done = true;
won = true;
}
if (Protocol.opcode(got) == Protocol.LOSS) {
done = true;
won = false;
}
if (Protocol.opcode(got) == Protocol.TIE) {
done = true;
tie = true;
}
if (Protocol.opcode(got) == Protocol.NEWROUND) {
done = false;
tie = false;
won = false;
Server.clearGrid(grid);
}
if (Protocol.opcode(got) == Protocol.FAIL) {
new MessageBox(getTitle(), "Kicked by Server");
setVisible(false);
break;
}
repaint();
} catch (IOException e) {
if (!isInterrupted()) {
new MessageBox(getTitle(), "Connection failed: " + e.getMessage());
setVisible(false);
}
break;
}
}
}
}
private class Mouse extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
if (!done)
set(e.getX()/100, e.getY()/100);
}
}
}