-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
106 lines (92 loc) · 3.21 KB
/
Server.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
import java.io.*;
import java.net.*;
public class Server {
static char[] square = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
public static int checkwin() {
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8'
&& square[9] != '9')
return 0;
else
return -1;
}
public static void board() {
System.out.println("\n\n\tTic Tac Toe\n\n");
System.out.println("CLIENT (X) - SERVER (O)\n\n\n");
System.out.println(" | | ");
System.out.println(" " + square[1] + " | " + square[2] + " | " + square[3] + " ");
System.out.println("_____|_____|_____");
System.out.println(" | | ");
System.out.println(" " + square[4] + " | " + square[5] + " | " + square[6] + " ");
System.out.println("_____|_____|_____");
System.out.println(" | | ");
System.out.println(" " + square[7] + " | " + square[8] + " | " + square[9] + " ");
System.out.println(" | | \n\n");
}
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8080);
System.out.println("Server started...");
Socket s = ss.accept();
System.out.println("Client connected...");
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
int i = checkwin();
while (i != 1) {
int choice = dis.readInt();
if (choice == 11) {
System.out.println("You win");
ss.close();
break;
}
char mark = 'X';
if (choice >= 1 && choice <= 9 && square[choice] == (char) (choice + '0')) {
square[choice] = mark;
}
i = checkwin();
System.out.println("\n" + i + "\n");
if (i == 1) {
board();
int flag = 10;
System.out.println("Client wins");
dos.writeInt(flag);
ss.close();
break;
}
board();
System.out.println("SERVER Please Enter Your Choice\n");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
choice = Integer.parseInt(br.readLine());
if (!(choice >= 1 && choice <= 9)) {
System.out.println("Invalid move\n");
System.out.println("Please enter a valid choice between 1-9\n");
choice = Integer.parseInt(br.readLine());
}
mark = 'O';
if (choice >= 1 && choice <= 9 && square[choice] == (char) (choice + '0')) {
square[choice] = mark;
}
dos.writeInt(choice);
}
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}