-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe.c
113 lines (100 loc) · 2.37 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
#include <stdio.h>
#include <conio.h>
void printBoard();
int checkWin();
void system();
char board[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
void main()
{
int player = 1, input, status = -1;
printBoard();
while (status == -1)
{
player = (player % 2 == 0) ? 2 : 1;
char mark = (player == 1) ? '%' : '&';
printf("Please enter Number For Player %d\n", player);
scanf("%d", &input);
if (input < 1 || input > 9)
{
printf("invalid input");
}
board[input] = mark;
printBoard();
int result = checkWin();
if (result == 1)
{
printf("Player %d is the Winner", player);
return;
}
else if (result == 0)
{
printf("draw");
return;
}
player++;
}
}
void printBoard()
{
system("cls");
printf("\n\n");
printf("=== TIC TAC TOE ===\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", board[1], board[2], board[3]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", board[4], board[5], board[6]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", board[7], board[8], board[9]);
printf(" | | \n");
printf("\n\n");
}
int checkWin()
{
if (board[1] == board[2] && board[2] == board[3])
{
return 1;
}
if (board[1] == board[4] && board[4] == board[7])
{
return 1;
}
if (board[7] == board[8] && board[8] == board[9])
{
return 1;
}
if (board[3] == board[6] && board[6] == board[9])
{
return 1;
}
if (board[1] == board[5] && board[5] == board[9])
{
return 1;
}
if (board[3] == board[5] && board[5] == board[7])
{
return 1;
}
if (board[2] == board[5] && board[5] == board[8])
{
return 1;
}
if (board[4] == board[5] && board[5] == board[6])
{
return 1;
}
int count = 0;
for (int i = 1; i <= 9; i++)
{
if (board[i] == 'X' || board[i] == 'O')
{
count++;
}
}
if (count == 9)
{
return 0;
}
return -1;
}