-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic-Tac_Toe.c
133 lines (120 loc) · 3.69 KB
/
Tic-Tac_Toe.c
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
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <stdlib.h>
/* Initialize the base */
char base[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; //"none" is only to match the index so there is no confusion2
int checkWin(); //it will return 1 if there is winner, -1 if the game is in progress, and 0 if game has been drawn
void drawBoard();
/*
initalize player to keep track of player.
Initialy it has the value of 1,
i is for tracking progress of the game (whether the game is in progress or not),
and choice is to tracking user's choice.
*/
int main(){
int player = 1, i, choice;
char mark; //X or O
do {
drawBoard();
player = (player % 2) ? 1 : 2; //if player's value is zero, it will return false and if its 1, it will return true
printf("Player %d, enter the choice : ", player);
scanf("%d", &choice);
mark = (player == 1) ? 'X' : 'O';
// if player's choice is n number, mark n
if(choice == 1 && base[1] == '1'){
base[1] = mark;
}
else if (choice == 2 && base[2] == '2'){
base[2] = mark;
}
else if (choice == 3 && base[3] == '3'){
base[3] = mark;
}
else if (choice == 4 && base[4] == '4'){
base[4] = mark;
}
else if (choice == 5 && base[5] == '5'){
base[5] = mark;
}
else if (choice == 6 && base[6] == '6'){
base[6] = mark;
}
else if (choice == 7 && base[7] == '7'){
base[7] = mark;
}
else if (choice == 8 && base[8] == '8'){
base[8] = mark;
}
else if (choice == 9 && base[9] == '9'){
base[9] = mark;
}
//if player's choice is invalid
else {
printf("Sorry I can't do that please try again.");
player--;
getch(); //return to the game
}
i = checkWin();
player++;
} while (i == -1);
drawBoard();
if(i == 1) {
printf("Player %d Won!!", --player);
}
else {
printf("==>Game draw");
}
getch();
return 0;
}
/*
Tic Tac Toe has 8 winning cases
*/
int checkWin(){
if(base[1] == base[2] && base[2] == base[3]){
return 1;
}
else if(base[4] == base[5] && base[5] == base[6]){
return 1;
}
else if(base[7] == base[8] && base[8] == base[9]){
return 1;
}
else if(base[1] == base[4] && base[4] == base[7]){
return 1;
}
else if(base[2] == base[5] && base[5] == base[8]){
return 1;
}
else if(base[3] == base[6] && base[6] == base[9]){
return 1;
}
else if(base[1] == base[5] && base[5] == base[9]){
return 1;
}
else if(base[3] == base[5] && base[5] == base[7]){
return 1;
}
else if(base[1] != '1' && base[2] != '2' && base[3] != '3' && base[4] != '4' && base[5] != '5' && base[6] != '6' && base[7] != '7' && base[8] != '8' && base[9] != '9'){
return 0; //we have no winner
}
else
return -1; //game is still in the progress
}
void drawBoard(){
system("cls");
printf(" Tic Tac Toe \n\n");
printf("Plyer1 (X) - Player2 (O) \n\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", base[1], base[2], base[3]);
printf(" ____|____|____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", base[4], base[5], base[6]);
printf(" ____|____|____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", base[7], base[8], base[9]);\
printf(" | | \n");
printf("\n");
printf("\n");
}