-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTicTacToeFirstPlayer.java
More file actions
231 lines (197 loc) · 5.7 KB
/
TicTacToeFirstPlayer.java
File metadata and controls
231 lines (197 loc) · 5.7 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
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
223
224
225
226
227
228
229
230
231
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class TicTacToeFirstPlayer extends JFrame {
private JLabel msglbl = new JLabel("");
private boolean isXorZero = true;
private boolean yourTurn = false;
private JPanel contentPane;
ServerSocket server;
final int PORT = 9001;
DataInputStream input ;
DataOutputStream output;
private void initialize() throws IOException{
server = new ServerSocket(PORT);
msglbl.setText("Waiting for the Second Player to Join the Game...");
Socket socket = server.accept();
msglbl.setText("Player2 Also Join , Let's Start the Game");
input= new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
}
private void recieveInput() throws IOException{
String recInput = "";
while(!recInput.equalsIgnoreCase("exit") ){
yourTurn= true;
msglbl.setText("Now Your Turn ");
recInput = input.readUTF(); //Hang
System.out.println("Rec Input "+recInput);
placeX(recInput);
}
}
private void placeX(String recInput){
System.out.println("Server Side PlaceX Call "+recInput);
if(recInput.trim().length()>0){
String array[]=recInput.split(",");
int position = Integer.parseInt(array[0]);
String buttonValue = array[1];
switch(position){
case 1:
one.setText("X");
break;
case 2:
two.setText("X");
break;
case 3:
three.setText("X");
break;
case 4:
four.setText("X");
break;
case 5:
fifth.setText("X");
break;
case 6:
six.setText("X");
break;
case 7:
seven.setText("X");
break;
case 8:
eight.setText("X");
break;
case 9:
nine.setText("X");
break;
}
}
}
public static void main(String[] args) throws Exception {
TicTacToeFirstPlayer frame = new TicTacToeFirstPlayer();
frame.setVisible(true);
frame.initialize();
frame.recieveInput();
}
/**
* Create the frame.
*/
JButton one = new JButton("");
JButton two = new JButton("");
JButton three = new JButton("");
JButton four = new JButton("");
JButton fifth = new JButton("");
JButton six = new JButton("");
JButton seven = new JButton("");
JButton eight = new JButton("");
JButton nine = new JButton("");
public TicTacToeFirstPlayer() {
setTitle("TicTacToe- Player One");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 640, 425);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendZero(1,one);
}
});
one.setFont(new Font("Lucida Grande", Font.PLAIN, 35));
one.setBounds(31, 73, 97, 71);
contentPane.add(one);
two.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendZero(2,two);
}
});
two.setFont(new Font("Lucida Grande", Font.PLAIN, 35));
two.setBounds(236, 73, 97, 71);
contentPane.add(two);
three.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendZero(3,three);
}
});
three.setFont(new Font("Lucida Grande", Font.PLAIN, 35));
three.setBounds(463, 73, 97, 71);
contentPane.add(three);
four.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendZero(4,four);
}
});
four.setFont(new Font("Lucida Grande", Font.PLAIN, 35));
four.setBounds(31, 194, 97, 71);
contentPane.add(four);
fifth.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendZero(5,fifth);
}
});
fifth.setFont(new Font("Lucida Grande", Font.PLAIN, 35));
fifth.setBounds(236, 194, 97, 71);
contentPane.add(fifth);
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendZero(6,six);
}
});
six.setFont(new Font("Lucida Grande", Font.PLAIN, 35));
six.setBounds(463, 194, 97, 71);
contentPane.add(six);
seven.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendZero(7,seven);
}
});
seven.setFont(new Font("Lucida Grande", Font.PLAIN, 35));
seven.setBounds(31, 302, 97, 71);
contentPane.add(seven);
eight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendZero(8,eight);
}
});
eight.setFont(new Font("Lucida Grande", Font.PLAIN, 35));
eight.setBounds(236, 302, 97, 71);
contentPane.add(eight);
nine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendZero(9,nine);
}
});
nine.setFont(new Font("Lucida Grande", Font.PLAIN, 35));
nine.setBounds(463, 302, 97, 71);
contentPane.add(nine);
msglbl.setFont(new Font("Lucida Grande", Font.BOLD, 20));
msglbl.setBounds(20, 22, 580, 39);
contentPane.add(msglbl);
}
private void sendZero(int buttonPosition,JButton button){
if(button.getText().trim().length()==0){
if(yourTurn){
button.setText("0");
yourTurn =false;
msglbl.setText("Now Player Two Turn....Wait for Your Turn...");
try {
output.writeUTF(buttonPosition+","+"0");
System.out.println("SendZero Call Pos "+buttonPosition+" Value 0 "+" Your Turn "+yourTurn);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}