-
Notifications
You must be signed in to change notification settings - Fork 0
/
TikTacToe.java
95 lines (92 loc) · 2.39 KB
/
TikTacToe.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
package codeChef;
import java.util.*;
public class TikTacToe {
public static void main(String[] args) {
try{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
sc.nextLine();
for(int T=0;T<t;T++)
{ int xwin=0,owin=0,cx=0,co=0,c_=0;
char[][] c=new char[3][3];
for (int i = 0; i < 3; i++) {
String data = "";
if (sc.hasNext()) {
data = sc.next();
} else {
break;
}
for (int j = 0; j <3; j++)
{
c[i][j] = data.charAt(j);
if(c[i][j]=='X')
cx++;
else if(c[i][j]=='O')
co++;
else
c_++;
}
}
// for(int i=0;i<3;i++)
// {
// for(int j=0;j<3;j++)
// {
// System.out.print(c[i][j]);
// }
// System.out.println();
// }
//System.out.println(cx+" "+co+" "+c_);
if(c[0][0] =='X' && c[0][1] == 'X' && c[0][2]=='X')
xwin=1;
if(c[1][0] =='X' && c[1][1] == 'X' && c[1][2]=='X')
xwin=1;
if(c[2][0] =='X' && c[2][1] == 'X' && c[2][2]=='X')
xwin=1;
if(c[0][0] =='X' && c[1][0] == 'X' && c[2][0]=='X')
xwin=1;
if(c[0][1] =='X' && c[1][1] == 'X' && c[2][1]=='X')
xwin=1;
if(c[0][2] =='X' && c[1][2] == 'X' && c[2][2]=='X')
xwin=1;
if(c[0][0] =='X' && c[1][1] == 'X' && c[2][2]=='X')
xwin=1;
if(c[0][2] =='X' && c[1][1] == 'X' && c[2][0]=='X')
xwin=1;
if(c[0][0] =='O' && c[0][1] == 'O' && c[0][2]=='O')
owin=1;
if(c[1][0] =='O' && c[1][1] == 'O' && c[1][2]=='O')
owin=1;
if(c[2][0] =='O' && c[2][1] == 'O' && c[2][2]=='O')
owin=1;
if(c[0][0] =='O' && c[1][0] == 'O' && c[2][0]=='O')
owin=1;
if(c[0][1] =='O' && c[1][1] == 'O' && c[2][1]=='O')
owin=1;
if(c[0][2] =='O' && c[1][2] == 'O' && c[2][2]=='O')
owin=1;
if(c[0][0] =='O' && c[1][1] == 'O' && c[2][2]=='O')
owin=1;
if(c[0][2] =='O' && c[1][1] == 'O' && c[2][0]=='O')
owin=1;
if((xwin==1 && owin==1) || ((cx-co>1) || (cx-co<0)))
System.out.println("3");
else if(cx==0 && co==0 && c_==0)
System.out.println("2");
else if(xwin==0 && owin==0 && c_>0)
System.out.println("2");
else if(xwin==1 && owin==0 && cx>co)
System.out.println("1");
else if(xwin==0 && owin==1 && cx==co)
System.out.println("1");
else if(xwin==0 && owin==0 && c_==0)
System.out.println("1");
else
System.out.println("3");
}
}
catch(Exception e)
{
return;
}
}
}