-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlay.java
222 lines (210 loc) · 5.88 KB
/
Play.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JOptionPane;
/**
* Play class for executing gameplay and interacting with the server
*
* @author Saharsh
*
*/
public class Play {
private Socket socket;
private Scanner in;
private PrintWriter out;
private Client tac;
/**
* Play constructor to update the client
*
* @param toe
*/
public Play(Client toe) {
tac = toe;
}
/**
* Start() to connect to the server and get input and output
*/
public void start() {
try {
this.socket = new Socket("127.0.0.1", 11223);
this.in = new Scanner(socket.getInputStream());
this.out = new PrintWriter(socket.getOutputStream(), true);
} catch(UnknownHostException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
Thread handler = new ClientHandler(socket);
handler.start();
}
/**
* ClientHandler for openening another thread and multi-threading
*
* @author Saharsh
*
*/
public class ClientHandler extends Thread {
private Socket socket;
/**
* ClientHandler constructor
*
* @param socket
*/
public ClientHandler(Socket socket) {
this.socket = socket;
}
/**
* Run() method which is overridden to read and write from the server
*/
public void run() {
try {
tac.pNo = Integer.parseInt(in.nextLine());
writeToServer();
readFromServer();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
/**
* WritetoServer which first updates the name that is inputted to the game and then the commands to the server
*
* @throws Exception
*/
public void writeToServer() throws Exception {
try {
tac.btn_submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!tac.txt_inp.getText().contentEquals("")) {
tac.n = tac.txt_inp.getText();
tac.txt_inp.setEnabled(false);
tac.btn_submit.setEnabled(false);
tac.label_name.setText("WELCOME "+tac.n);
tac.frame.setTitle("Tic Tac Toe-Player: "+tac.n);
tac.pmv = tac.n;
out.println(tac.pmv);
tac.pmv = "";
}
}
});
cmdToServer();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* cmdToServer to write the commands to the server when a button is clicked
*
* @throws Exception
*/
public void cmdToServer() throws Exception {
try {
tac.but00.addActionListener(new alist());
tac.but01.addActionListener(new alist());
tac.but02.addActionListener(new alist());
tac.but10.addActionListener(new alist());
tac.but11.addActionListener(new alist());
tac.but12.addActionListener(new alist());
tac.but20.addActionListener(new alist());
tac.but21.addActionListener(new alist());
tac.but22.addActionListener(new alist());
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* alist is a listener that implements ActionListener to execute the action when a button is clicked
*
* @author Saharsh
*
*/
public class alist implements ActionListener {
public void actionPerformed(ActionEvent e) {
tac.pmv = "";
JButton btn = (JButton) e.getSource();
if(tac.mov == tac.pNo) {
tac.pmv += Integer.toString(tac.pNo);
if(btn==tac.but00) {
tac.pmv += "00";
} else if(btn==tac.but01) {
tac.pmv += "01";
} else if(btn==tac.but02) {
tac.pmv += "02";
} else if(btn==tac.but10) {
tac.pmv += "10";
} else if(btn==tac.but11) {
tac.pmv += "11";
} else if(btn==tac.but12) {
tac.pmv += "12";
} else if(btn==tac.but20) {
tac.pmv += "20";
} else if(btn==tac.but21) {
tac.pmv += "21";
} else if(btn==tac.but22) {
tac.pmv += "22";
}
out.println(tac.pmv);
}
}
}
/**
* reafFromServer to read the commands from other player and compare the outcomes
*
* @throws Exception
*/
public void readFromServer() throws Exception {
try {
while(!in.hasNextLine()) {}
while(in.hasNextLine()) {
String cmd = in.nextLine();
if(cmd.contentEquals("2 players.")) {
tac.but00.setEnabled(true);
tac.but01.setEnabled(true);
tac.but02.setEnabled(true);
tac.but10.setEnabled(true);
tac.but11.setEnabled(true);
tac.but12.setEnabled(true);
tac.but20.setEnabled(true);
tac.but21.setEnabled(true);
tac.but22.setEnabled(true);
}
else if(cmd.contentEquals("Game Ends. One of the players left.")) {
JOptionPane.showMessageDialog(tac.frame, cmd, "Message", JOptionPane.INFORMATION_MESSAGE);
socket.close();
System.exit(0);
} else {
tac.cross(cmd);
if(tac.pNo==tac.mov) {
tac.label_name.setText("Your opponent has moved, now is your turn.");
cmdToServer();
} else {
tac.label_name.setText("Valid Move, wait for your opponent");
}
if(tac.win!=0) {
if(tac.win==tac.pNo) {
JOptionPane.showMessageDialog(tac.frame, "Congratulations. You win.", "Message", JOptionPane.INFORMATION_MESSAGE);
} else if(tac.win==3) {
JOptionPane.showMessageDialog(tac.frame, "Draw.", "Message", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(tac.frame, "You lose.", "Message", JOptionPane.INFORMATION_MESSAGE);
}
System.exit(0);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
socket.close();
}
}
}