-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.c
210 lines (168 loc) · 5.23 KB
/
Project.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// The main and bigger project is located in:
// https://github.com/UMMAN2005/GTK-Project-TTT
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void restart(char);
void displayBoard(char board[][10], int size) {
printf("\n ");
for (int i = 1; i <= size; i++) {
printf(" %d ", i);
if (i < size) {
printf("|");
}
}
printf("\n");
for (int i = 0; i < size; i++) {
printf("%d", i + 1);
for (int j = 0; j < size; j++) {
printf(" %c ", board[i][j]);
if (j < size - 1) {
printf("|");
}
}
printf("\n");
if (i < size - 1) {
printf(" ");
for (int j = 0; j < size; j++) {
printf("---");
if (j < size - 1) {
printf("+");
}
}
printf("\n");
}
}
printf("\n");
}
//Function to check if the player has won
int checkWin(char board[][10], int size, char player) {
// Check rows
int count = 0;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (board[i][j] == player) {
count++;
if (count == 3) return 1;
}
else count = 0;
}
}
// Check columns
count = 0;
for (int j = 0; j < size; ++j) {
for (int i = 0; i < size; ++i) {
if (board[i][j] == player) {
count++;
if(count == 3) return 1;
}
else count = 0;
}
}
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if (i + 2 < size && j + 2 < size && board[i][j] == player && board[i + 1][j + 1] == player && board[i + 2][j + 2] == player) {
return 1;
}
if (i + 2 < size && j - 2 >= 0 && board[i][j] == player && board[i + 1][j - 1] == player && board[i + 2][j - 2] == player) {
return 1;
}
}
}
return 0; // No winner yet
}
// Function to make a move on the board
void makeMove(char board[][10], int row, int col, char player) {
board[row][col] = player;
}
void initializeGame(char currentPlayer) {
int size;
// Get the size of the board from the user
do {
printf("Enter the size of the Tic-Tac-Toe board (between 3 and 10): ");
if (scanf("%d", &size) != 1) {
printf("Input is not valid!\n");
while (getchar() != '\n');
};
} while (size < 3 || size > 10);
// Initialize the board
char board[10][10];
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
board[i][j] = ' ';
}
}
int row, col;
// Game loop
int totalMoves = size * size;
int moveCount = 0;
do {
// Display the current state of the board
displayBoard(board, size);
// Get the player's move
do {
printf("Player %c, enter your move (row and column): ", currentPlayer);
scanf("%d %d", &row, &col);
if (row < 1 || row > size || col < 1 || col > size) {
printf("Invalid move. Row and column must be between 1 and %d. Try again.\n", size);
} else if (board[row - 1][col - 1] != ' ') {
printf("This place is not empty. Try again.\n");
}
} while (row < 1 || row > size || col < 1 || col > size || board[row - 1][col - 1] != ' ');
// Make the move
makeMove(board, row - 1, col - 1, currentPlayer);
// Check for a win
if (checkWin(board, size, currentPlayer)) {
displayBoard(board, size);
printf("Player %c wins!\n", currentPlayer);
restart(currentPlayer);
break;
}
moveCount++;
// Check for a draw
if (moveCount == totalMoves) {
displayBoard(board, size);
printf("It's a draw!\n");
break;
}
// Switch to the other player
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
} while (1);
}
void restart(char currentPlayer) {
char* answer;
printf("Would you like to play again? ('yes' or 'no'): ");
scanf("%s", answer);
for (int i = 0; i < strlen(answer); i++) {
answer[i] = tolower(answer[i]);
}
if (strcmp(answer, "yes") == 0) {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
initializeGame(currentPlayer);
} else if (strcmp(answer, "no") == 0) {
printf("Thanks for playing! Goodbye.\n");
} else {
printf("Invalid input. Please enter 'yes' or 'no'!\n ");
restart(currentPlayer);
}
}
int main() {
char player;
do {
printf("Which player should start the first? `X` or `O` (Default `X`): ");
scanf(" %c", &player); // Added a space before %c to consume any leading whitespaces
player = toupper(player);
if (player != 'X' && player != 'O') {
printf("Invalid input. Please enter 'X' or 'O'!\n");
}
} while (player != 'X' && player != 'O');
player = toupper(player);
if ((int)player == 88)
initializeGame('X');
else if ((int)player == 79)
initializeGame('O');
else
initializeGame('X');
return 0;
}