-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe(Codic).java
287 lines (168 loc) · 6.8 KB
/
TicTacToe(Codic).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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import java.util.Scanner;
class TicTacToeCodic {
public static void box(int size,String[][] tic){
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print( tic[i][j] );
if (j < size - 1) {
System.out.print("|");
}
}
System.out.println();
if (i < size - 1) {
System.out.println("_____");
}
}
}
public static int input(String[][] tic,int size, int num,String player1,String player2){
Scanner sc = new Scanner(System.in);
System.out.println();
System.out.print(player1 + "'s turn : ");
String cordinate1 = sc.nextLine();
System.out.println();
String[] temp = cordinate1.split(",");
String cor1x = temp[0];
String cor1y = temp[1];
int cordX1 = Integer.parseInt(cor1x);
int cordY1 = Integer.parseInt(cor1y);
if (tic[cordX1][cordY1].compareTo(" ")== 0 ) {
tic[cordX1][cordY1] = "O";
}
else{
System.out.println("place already taken");
}
box(size, tic);
if(check(tic, size)== 0 && num < 8){
System.out.println();
System.out.print(player2 + "'s Turn : ");
String cordinate2 = sc.nextLine();
System.out.println();
if (cordinate2.compareTo(cordinate1) != 0) {
String[] temp2 = cordinate2.split(",");
String cor2x = temp2[0];
String cor2y = temp2[1];
int cordX2 = Integer.parseInt(cor2x);
int cordY2 = Integer.parseInt(cor2y);
if(tic[cordX2][cordY2].compareTo(" ")== 0){
tic[cordX2][cordY2] = "X";
}
else{
System.out.println("Place already Taken");
}
box(size, tic);
}
else{
System.out.println("This place is already taken");
}
}
return 2;
}
public static int check(String[][] tic, int size){
for (int i = 0; i < size; i++) {
if (tic[i][0].compareTo(tic[i][1])== 0 && tic[i][1].compareTo(tic[i][2])== 0 && tic[i][2].compareTo(" ") != 0) {
if (tic[i][0].compareTo("O")== 0) {
return 1 ;
}
else{
return 2;
}
}
if (tic[0][i].compareTo(tic[1][i])== 0 && tic[1][i].compareTo(tic[2][i])== 0 && tic[2][i].compareTo(" ") != 0) {
if (tic[0][i].compareTo("O")== 0) {
return 1;
}
else{
return 2;
}
}
if (tic[0][0].compareTo(tic[1][1])== 0 && tic[1][1].compareTo(tic[2][2])== 0 && tic[2][2].compareTo(" ") != 0) {
if (tic[0][0].compareTo("O")== 0) {
return 1;
}
else{
return 2; }
}
if (tic[0][2].compareTo(tic[1][1])== 0 && tic[1][1].compareTo(tic[2][0])== 0 && tic[2][0].compareTo(" ") != 0) {
if (tic[0][2].compareTo("O")== 0) {
return 1;
}
else{
return 2;
}
}
}
return 0;
}
public static void initialize(String[][] tic,int size){
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
tic[i][j] = " ";
}
}
}
public static void menu(){
System.out.println(" Tic Tac Toe");
System.out.println("Player Vs Player | P V P" );
System.out.println();
}
public static void coordinates(){
System.out.println("0,0|0,1|0,2");
System.out.println("____________");
System.out.println("1,0|1,1|1,2");
System.out.println("____________");
System.out.println("2,0|2,1|0,2");
System.out.println("Coordinates of Table");
System.out.println("Please Enter Coordinates e.g(0,1)");
}
public static void TicTacToeCodix() {
Scanner sc = new Scanner(System.in);
int num1 = 0;
String[][] tic = new String[3][3];
int size= 3;
initialize(tic, size);
menu();
System.out.print("Player - 1's Name: ");
String player1 = sc.nextLine();
System.out.println();
System.out.print("Player - 2's Name: ");
String player2 = sc.nextLine();
System.out.println();
System.out.println();
System.out.println(" " + player1 + " VS " + player2);
System.out.println();
System.out.println(player1 + "'s sign: O ");
System.out.println(player2 + "'s sign: X ");
System.out.println();
coordinates();
while (check(tic, size) == 0 && num1 < 9) {
num1 += input(tic,size,num1,player1,player2);
}
System.out.println();
if (check(tic, size) == 1) {
if (player1.length() > player2.length() ) {
System.out.println("Winner\t\t\t\t\t\t\t\tLoser");
System.out.print("Congrats " + player1 + " You Win \t\t\t");
System.out.print(player2 + " Better Luck Next Time ");
}else {
System.out.println("Winner\t\t\t\t\t\t\tLoser");
System.out.print("Congrats " + player1 + " You Win \t\t\t");
System.out.print(player2 + " Better Luck Next Time ");
}
}
else if(check(tic, size) == 2){
if (player1.length() > player2.length() ) {
System.out.println("Winner\t\t\t\t\t\t\tLoser");
System.out.print("Congrats " + player2 + " You Win \t\t\t");
System.out.print(player1 + " Better Luck Next Time ");
}else {
System.out.println("Winner\t\t\t\t\t\t\t\tLoser");
System.out.print("Congrats " + player2 + " You Win \t\t\t");
System.out.print(player1 + " Better Luck Next Time ");
}
}
else{
System.out.println("Draw");
System.out.println(player1 + " and " + player2 + " Both played Well");
}
}
}