-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
198 lines (193 loc) · 4.69 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class Client extends Frame
{
ObjectOutputStream oout;
ObjectInputStream oin;
Socket socket;
boolean turn;
boolean keepchatting;
T3Controller controller;
Client()
{
super("Player 2", false);
turn = false;
keepchatting = false;
controller = new T3Controller();
}
public void actionPerformed(ActionEvent ae)
{
if(this.turn)
{
this.setLabel("Player 1's Turn!");
JButton tile_pressed = (JButton) ae.getSource();
int row = (int) tile_pressed.getClientProperty("row");
int col = (int) tile_pressed.getClientProperty("column");
//tile[row][col].setEnabled(false);
int state = this.controller.determineState(CommunicationObject.PLAYER_2,row, col);
CommunicationObject co = new CommunicationObject(state, row, col);
this.disableButton(co, "X");
try
{
this.sendMessage(co);
}
catch (IOException e)
{
//System.out.println("Can't write");
e.printStackTrace();
}
if(state == CommunicationObject.WON)
{
this.setLabel("You Won!");
this.keepchatting = false;
}
this.turn = false;
//System.out.println("Completed!");
}
}
public void sendMessage(CommunicationObject co) throws IOException
{
this.oout.writeObject(co);
this.oout.flush();
}
public void disableButton(CommunicationObject co, String sign)
{
int row = co.getRow();
int col = co.getCol();
String resource = "resources/X.png";
if(sign == "O")
{
resource = "resources/O.png";
}
ImageIcon img = new ImageIcon(getClass().getResource(resource));
tile[row][col].setIcon(img);
tile[row][col].setEnabled(false);
}
public void windowClosing(WindowEvent arg0)
{
System.out.println("Closing..");
this.keepchatting = false;
if(this.keepchatting)
{
CommunicationObject co = new CommunicationObject(CommunicationObject.CLOSE_WIN, -1, -1);
try
{
this.sendMessage(co);
}
catch (IOException e1)
{
e1.printStackTrace();
}
try
{
this.closeStream();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
this.closeServer();
}
catch (IOException e)
{
e.printStackTrace();
}
}
this.frame.dispose();
System.exit(0);
}
private void closeServer() throws IOException
{
this.socket.close();
}
private void closeStream() throws IOException
{
this.oout.close();
this.oin.close();
}
protected void setUpTile()
{
super.setUpTile();
}
public static void main(String[] args) throws UnknownHostException, ClassNotFoundException
{
Client client = new Client();
try
{
client.socket = new Socket("localhost", 6066);
client.oout=new ObjectOutputStream(client.socket.getOutputStream());
client.oout.flush();
client.oin = new ObjectInputStream(client.socket.getInputStream());
client.setUpTile();
CommunicationObject co = null;
client.keepchatting = true;
while(client.keepchatting)
{
co = (CommunicationObject) client.oin.readObject();
if(co.getType() != CommunicationObject.KEEP_PLAYING)
{
client.turn = false;
//System.out.println(co.getType());
client.keepchatting = false;
break;
}
else
{
client.setLabel("Your turn!");
client.disableButton(co, "O");
}
client.turn = true;
//System.out.println(client.turn);
}
//System.out.println("Server Disconnected!");
//System.out.println("Out of loop"+ co.getType());
if(co.getType() == CommunicationObject.WON)
{
client.disableButton(co, "O");
client.setLabel("You Lost!");
}
else if(co.getType() == CommunicationObject.LOST)
{
client.setLabel("You Won!");
}
else if(co.getType() == CommunicationObject.DRAW)
{
client.setLabel("Match Drawn!");
client.disableButton(co, "O");
}
else
{
client.setLabel("Player 1 Left! You Won!");
}
client.closeStream();
client.closeServer();
}
catch(EOFException e){System.out.println("bye");}
catch(ConnectException ce)
{
client.setLabel("Server is not available. Try again later.");
}
catch(SocketException se)
{
client.setLabel("Player 1 Left! You Won!");
client.keepchatting = false;
}
catch(IOException ioe)
{
//ioe.printStackTrace();
System.out.println("Something Went Wrong!");
}
}
}