-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtic tac toe.cpp
More file actions
107 lines (94 loc) · 2.5 KB
/
tic tac toe.cpp
File metadata and controls
107 lines (94 loc) · 2.5 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
#include<stdio.h>
#include<stdlib.h>
char box[10]={'1','2','3','4','5','6','7','8','9'};
void creating_board();
void marking_board(int,char);
int check_winner();
main()
{
int choice,player=1,i;
char mark;
do
{
system("cls");
creating_board();
player=(player%2)?1:2;
printf("Player %d , Enter your number : ",player);
scanf("%d",&choice);
mark= (player==1)?'X':'O';
marking_board(choice,mark);
i=check_winner();
player++;
}while(i==-1);
creating_board();
if(i==1)
{ printf("Player %d has won the match",--player);
}
else
{
printf("<-------DRAW------>");
}
}
void creating_board()
{
printf("\t\t\t\t\t\t TIC TAC TOE ");
printf("\n \t\t\t\t\t DEVELOPED BY SHASWAT PANDEY\n\n\n");
printf("PLAYER 1 --> [X]");
printf("PLAYER 2 --> [O]");
printf("\n\n");
printf(" %c | %c | %c \n",box[0],box[1],box[2]);
printf(" ___|________|______\n");
printf(" | | \n");
printf(" %c | %c | %c \n",box[3],box[4],box[5]);
printf(" ___|________|______\n");
printf(" %c | %c | %c \n",box[6],box[7],box[8]);
printf(" | | \n");
}
void marking_board(int choice ,char mark)
{
if(choice==1 && box[0]=='1')
box[0]=mark;
else if(choice==2 && box[1]=='2')
box[1]=mark;
else if(choice==3 && box[2]=='3')
box[2]=mark;
else if(choice==4 && box[3]=='4')
box[3]=mark;
else if(choice==5 && box[4]=='5')
box[4]=mark;
else if(choice==6 && box[5]=='6')
box[5]=mark;
else if(choice==7 && box[6]=='7')
box[6]=mark;
else if(choice==8 && box[7]=='8')
box[7]=mark;
else if(choice==9 && box[8]=='9')
box[8]=mark;
else
{
printf("\n\nInvalid choice");
}
}
int check_winner()
{
if(box[0]==box[1] && box[1]==box[2])
return(1);
else if(box[3]==box[4] && box[4]==box[5])
return(1);
else if(box[6]==box[7] && box[7]==box[8])
return(1);
else if(box[0]==box[3] && box[3]==box[6]) //HORIZONTALLY
return(1);
else if(box[1]==box[4] && box[4]==box[7])
return(1);
else if(box[2]==box[5] && box[5]==box[8])
return(1);
else if(box[0]==box[4] && box[4]==box[8]) //DIAGONALLY
return(1);
else if(box[2]==box[4] && box[4]==box[6])
return(1);
else if(box[0]!='1' && box[1]!='2' && box[2]!='3' && box[3]!='4' && box[4]!='5' && box[5]!='6' && box[6]!='7' && box[7]!='8' && box[8]!='9')
return(0);
else
return(-1);
}