-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe-Game.c
188 lines (157 loc) · 4.72 KB
/
Tic-Tac-Toe-Game.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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
// Function declarations
static void singlePlayerMode();
static void twoPlayerMode();
static void placeX(int); // Place 'X' by Player 1
static void placeOComputer(); // Place 'O' by computer
static void placeO(int); // Place 'O' by Player 2
int checkWinner(); // Check for a winner
static char board[9]; // 3x3 Tic-Tac-Toe board
int main()
{
srand((unsigned int)time(NULL));
int playAgain = 0;
do
{
int choice = 0;
// Initialize the board
for (int i = 0; i < 9; i++) board[i] = '*';
// Display menu
printf("***************************************\n");
printf("************* TIC TAC TOE *************\n");
printf("***************************************\n");
printf("******* 1. YOU vs COMPUTER ************\n");
printf("******* 2. YOU vs PLAYER **************\n");
printf("******* 3. EXIT ***********************\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
singlePlayerMode();
break;
case 2:
twoPlayerMode();
break;
default:
printf("THANK YOU! EXITING...\n");
}
printf("Play again? Enter 1 for YES, 0 for NO: ");
scanf("%d", &playAgain);
} while (playAgain == 1);
return 0;
}
void singlePlayerMode()
{
int playerMove;
int filledSlots = 0;
for (int i = 0; i < 9; i++) printf("%c ", board[i % 3 == 0 ? i : i % 3 == 0]);
for (int moveCount = 1; moveCount <= 9; moveCount++)
{
printf("Your move (1-9): ");
scanf("%d", &playerMove);
placeX(playerMove);
if (filledSlots < 4)
{
placeOComputer();
}
for (int i = 0; i < 9; i++) printf("%c ", board[i % 3 == 0 ? i : i % 3 == 0]);
filledSlots++;
int result = checkWinner();
if (result == -1)
{
printf("YOU WIN!\n");
break;
}
else if (result == -2)
{
printf("COMPUTER WINS!\n");
break;
}
if (filledSlots == 4)
{
printf("\nDRAW\n");
break;
}
}
}
void twoPlayerMode()
{
int player1Move, player2Move;
int filledSlots = 0;
for (int i = 0; i < 9; i++) printf("%c ", board[i % 3 == 0 ? i : i % 3 == 0]);
for (int moveCount = 1; moveCount <= 9; moveCount++)
{
printf("Player 1 (X), enter your move: ");
scanf("%d", &player1Move);
placeX(player1Move);
if (filledSlots < 4)
{
printf("Player 2 (O), enter your move: ");
scanf("%d", &player2Move);
placeO(player2Move);
}
for (int i = 0; i < 9; i++) printf("%c ", board[i % 3 == 0 ? i : i % 3 == 0]);
filledSlots++;
int result = checkWinner();
if (result == -1)
{
printf("Player 1 WINS!\n");
break;
}
else if (result == -2)
{
printf("Player 2 WINS!\n");
break;
}
if (filledSlots == 4)
{
printf("\nDRAW\n");
break;
}
}
}
void placeX(int position)
{
while (position < 1 || position > 9 || board[position - 1] != '*')
{
printf("Invalid move. Enter again: ");
scanf("%d", &position);
}
board[position - 1] = 'X';
}
void placeOComputer()
{
int position = rand() % 9;
while (board[position] != '*')
{
position = rand() % 9;
}
board[position] = 'O';
printf("\nComputer placed 'O' at position %d\n", position + 1);
}
void placeO(int position)
{
while (position < 1 || position > 9 || board[position - 1] != '*')
{
printf("Invalid move. Enter again: ");
scanf("%d", &position);
}
board[position - 1] = 'O';
}
int checkWinner()
{
// Check rows, columns, and diagonals
if (board[0] == board[1] && board[1] == board[2]) return (board[0] == 'X') ? -1 : -2;
if (board[3] == board[4] && board[4] == board[5]) return (board[3] == 'X') ? -1 : -2;
if (board[6] == board[7] && board[7] == board[8]) return (board[6] == 'X') ? -1 : -2;
if (board[0] == board[3] && board[3] == board[6]) return (board[0] == 'X') ? -1 : -2;
if (board[1] == board[4] && board[4] == board[7]) return (board[1] == 'X') ? -1 : -2;
if (board[2] == board[5] && board[5] == board[8]) return (board[2] == 'X') ? -1 : -2;
if (board[0] == board[4] && board[4] == board[8]) return (board[0] == 'X') ? -1 : -2;
if (board[2] == board[4] && board[4] == board[6]) return (board[2] == 'X') ? -1 : -2;
return 0;
}