-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTic.java
More file actions
155 lines (143 loc) · 2.59 KB
/
Tic.java
File metadata and controls
155 lines (143 loc) · 2.59 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
import java.io.*;
class Tic
{
static String n1, n2;
public static void main(String args[])throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
System.out.println("Player X enter your name!");
n1 = in.readLine();
System.out.println("Player O enter your name!");
n2 = in.readLine();
char a[][] = new char[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
a[i][j] = '_';
}
}
//Displaying
display(a);
//Inputting
int x,y,z;
for(int i=1; i<=5; i++)
{
if(i==5)
{
System.out.println(n1+" enter position");
x = Integer.parseInt(in.readLine());
y = Integer.parseInt(in.readLine());
a[x][y] = 'X';
z = check(a);
display(a);
if(z==1)
{
System.out.println("GAME OVER!!");
break;
}
else
{
System.out.println("NO ONE WINS!!");
System.out.println("GAME OVER!!");
break;
}
}
System.out.println(n1+" enter position");
x = Integer.parseInt(in.readLine());
y = Integer.parseInt(in.readLine());
a[x][y] = 'X';
display(a);
z = check(a);
if(z==1)
{
System.out.println("GAME OVER!!");
break;
}
System.out.println(n2+" enter position");
x = Integer.parseInt(in.readLine());
y = Integer.parseInt(in.readLine());
a[x][y] = 'O';
display(a);
z = check(a);
if(z==1)
{
System.out.println("GAME OVER!!");
break;
}
}
}
static int check(char b[][])
{
int j=0; int val =0; int p=0,q=0; String n="";
for(int i=0; i<3; i++)
{
if((b[i][j]=='X') || (b[i][j]=='O'))
{
if((b[i][j] == b[i][j+1]) && (b[i][j] == b[i][j+2]))
{
val=1;
p=i; q=j;
break;
}
}
if((b[j][i]=='X') || (b[j][i]=='O'))
{
if((b[j][i] == b[j+1][i]) && (b[j][i] == b[j+2][i]))
{
val=1;
p=i; q=j;
break;
}
}
}
if(val==1)
{
n = name(b, p, q);
System.out.println(n+" wins!!");
return val;
}
else
{
if(b[1][1]=='X'|| b[1][1]=='O')
{
if(((b[0][0]==b[1][1]) && (b[0][0]==b[2][2])) || ((b[0][2]==b[1][1]) && (b[0][2]==b[2][0])))
{
val = 1;
n = name(b, 1, 1);
System.out.println(n+" wins!!");
return val;
}
}
else
{
return val;
}
}
return val;
}
static void display(char b[][])
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
static String name(char c[][], int n, int m)
{
if(c[n][m] == 'X')
{
return n1;
}
if(c[n][m] == 'O')
{
return n2;
}
return "";
}
}