-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe
122 lines (122 loc) · 3.13 KB
/
tictactoe
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
import java.util.*;
public class tictactoe
{
static Scanner sc=new Scanner(System.in);
static int check(int a[][],int i,int j,int t)
{
int p=0;
if(a[i][1]==t&&a[i][0]==t&&a[i][2]==t)
return(1);
else if(a[0][j]==t&&a[1][j]==t&&a[2][j]==t)
return(1);
else if(i==j)
{
if(a[0][0]==t&&a[1][1]==t&&a[2][2]==t)
return(1);
if(a[0][2]==t&&a[1][1]==t&&a[2][0]==t)
return(1);
}
else if((i==0&&j==2)||(i==2&&j==0))
{
if(a[0][2]==t&&a[1][1]==t&&a[2][0]==t)
return(1);
}
return(p);
}
static char find(int a)
{
if(a==-1)
return(' ');
else if(a==0)
return('o');
else
return('x');
}
static void print(int a[][])
{
int i=0,j=0;
System.out.print("\n ");
System.out.print(find(a[i][j++]));
System.out.print(" | ");
System.out.print(find(a[i][j++]));
System.out.print(" | ");
System.out.println(find(a[i++][j]));
System.out.println("___________");
j=0;
System.out.print(" ");
System.out.print(find(a[i][j++]));
System.out.print(" | ");
System.out.print(find(a[i][j++]));
System.out.print(" | ");
System.out.println(find(a[i++][j]));
System.out.println("___________");
j=0;
System.out.print(" ");
System.out.print(find(a[i][j++]));
System.out.print(" | ");
System.out.print(find(a[i][j++]));
System.out.print(" | ");
System.out.println(find(a[i++][j]));
}
static int startgame(int x,int o)
{
int c,t=0,k=0,ch=0,i=0,j=0;
int a[][]={{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
System.out.println("\nX = "+x+"\nO = "+o+"\n");
System.out.println(" 11 | 12 | 13 \n______________\n 21 | 22 | 23 \n______________\n 31 | 32 | 33 ");
while(1==1)
{
System.out.println("\nEnter choice: ");
c=sc.nextInt();
i=(c/10)-1;
j=(c%10)-1;
if(i<0||i>2||j<0||j>2)
{
System.out.println("\nInvalid Choice.");
print(a);
}
else if(a[i][j]!=-1)
{
System.out.println("\nInvalid Choice.");
print(a);
}
else
{
a[i][j]=t;
print(a);
k++;
if(k>=3)
ch=check(a,i,j,t);
if(ch==1)
{
x+=(t==0)?0:1;
o+=(t==1)?0:1;
System.out.println("\n"+((t==0)?"O":"X")+" wins!\n\nX = "+x+"\nO = "+o);
return(t);
}
t=(t==0)?1:0;
}
if(k==9)
{
System.out.println("\nTie!\n\nX = "+x+"\nO = "+o);
return(-1);
}
}
}
public static void main(String[] args)
{
int c=0,x=0,o=0,s=0;
while(1==1)
{
System.out.println("\nStart Game? ");
c=sc.nextInt();
if(c==0)
break;
s=startgame(x,o);
if(s==1)
x++;
else if(s==0)
o++;
}
}
}