forked from CubixPro/Tic-Tac-Toe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TIC_TAC_TOE.java
189 lines (168 loc) · 5.43 KB
/
TIC_TAC_TOE.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
import java.util.*;
class TIC_TAC_TOE
{
char ch[][]=new char[3][3];
char a;
void display()
{
System.out.println(" --- --- ---");
for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
System.out.print("| "+ch[i][j] +" ");
}
System.out.println("|");
}
System.out.println(" --- --- ---");
}
void initialize(int i,char sym )
{
switch(i)
{
case 1: ch[0][0]=sym;break;
case 2: ch[0][1]=sym;break;
case 3: ch[0][2]=sym;break;
case 4: ch[1][0]=sym;break;
case 5: ch[1][1]=sym;break;
case 6: ch[1][2]=sym;break;
case 7: ch[2][0]=sym;break;
case 8: ch[2][1]=sym;break;
case 9: ch[2][2]=sym;break;
}
}
void input()
{
int c1=0,c2=0;
int turn = 1;String name1="",name2="";
Scanner sc = new Scanner(System.in);
System.out.println("\nDo you want to enter your NAME player 1 ?");
String chc1 = sc.next();
chc1 = chc1.toLowerCase();
if (chc1.equals("yes")||chc1.equals("yeah")||chc1.equals("ya")||chc1.equals("yup")||chc1.equals("yeai")||chc1.startsWith("y"))
{
System.out.println("Enter your name !");
name1 = sc.next();
}
else
{
name1 = "unkn_pl1";
}
System.out.println("\nDo you want to enter your NAME player 2?");
String chc11 = sc.next();
chc11 = chc11.toLowerCase();
if (chc11.equals("yes")||chc11.equals("yeah")||chc11.equals("ya")||chc11.equals("yup")||chc11.equals("yeai")||chc11.startsWith("y"))
{
System.out.println("Enter your name !");
name2 = sc.next();
}
else
{
name2 = "unkn_pl2";
}
while(true)
{
char i1 = '1';
for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
ch[i][j] = (char)i1;
i1++;
}
}
display();
while(true)
{
System.out.println("\n"+name1+":where do you want to enter?");
int x = sc.nextInt();
initialize(x,'0');
display();
if(check()==1)
{
System.out.println("\n"+name1+"(0) wins");c1++;break;
}
else if(check()==2)
{
System.out.println("\nno one wins");break;
}
System.out.println("\n"+name2+":where do you want to enter?");
int y = sc.nextInt();
initialize(y,'X');
display();
if(check()==1)
{
System.out.println("\n"+name2+"(X) wins");c2++;
break;
}
else if(check()==2)
{
System.out.println("\nno one wins");break;
}
}
System.out.println("\n\nWanna play again ? :D !!!!!!!!!!");
String chc = sc.next();
chc = chc.toLowerCase();
if(chc.equals("yes")||chc.equals("yeah")||chc.equals("ha")||chc.equals("yup")||chc.equals("yeai")||chc.equals("y")||chc.startsWith("y")||chc.startsWith("h"))
{
turn++; String temp = name2;
name2 = name1;
name1 = temp;
continue;
}
else
{
break;
}
}
System.out.println("\n\nSCORES:-");
if(c1>c2)
{
System.out.println("\n\n"+name1+"\t"+name2);
System.out.println(c1+"\t"+c2);System.out.println("\n"+name1+" WINS");
display2();
}
else
{
System.out.println("\n\n"+name2+"\t"+name1);
System.out.println(c2+"\t"+c1);System.out.println("\n"+name2+" WINS");
display2();
}
}
int check()
{
int check=0;int m = 0;
for(int i = 0;i<3;i++)
{
for(int j = 0;j<3;j++)
{
if(ch[i][j]>48&&ch[i][j]<=57)
{
m++;break;
}
}
}
if((ch[0][0]==ch[0][1]&&ch[0][1]==ch[0][2])||(ch[1][0]==ch[1][1]&&ch[1][1]==ch[1][2])||(ch[2][0]==ch[2][1]&&ch[2][1]==ch[2][2])||(ch[0][0]==ch[1][0]&&ch[1][0]==ch[2][0])||(ch[0][1]==ch[1][1]&&ch[1][1]==ch[2][1])||(ch[2][0]==ch[2][1]&&ch[2][1]==ch[2][2])||(ch[0][0]==ch[1][1]&&ch[1][1]==ch[2][2])||(ch[0][2]==ch[1][1]&&ch[1][1]==ch[2][0]))
{
check = 1;return check;
}
if(m==0)
{
check = 2;return check;
}
return 0;
}
void display2()
{
System.out.println("\nIf you enjoyed playing please rate us out of five ->");
Scanner sc = new Scanner(System.in);
double rate = sc.nextDouble();
System.out.println("\n Thanks Again ");
System.out.println("\n\n"+(char)(169)+" Cubix Pro Enterprises Private Limited");
}
public static void main(String args[])
{
TIC_TAC_TOE t1 = new TIC_TAC_TOE();
t1.input();
}
}