-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_main.c
64 lines (49 loc) · 1.69 KB
/
game_main.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
#include "game_logic.h" // Include the header for your game logic
#include <stdio.h>
int main() {
char currentPlayer = 'X';
int row, col;
while (1) {
printf("\nCurrent Grid:\n");
displayGrid();
printf("Player %c's turn. Enter a row (1-3): ", currentPlayer);
row = getValidatedInput();
if(row < 1 || row > 3) {
printf("Invalid row. It must be a number between 1 and 3.\n");
continue; // Skip the rest of the loop and start from the beginning
}
printf("Enter a column (1-3): ");
col = getValidatedInput();
if(col < 1 || col > 3) {
printf("Invalid column. It must be a number between 1 and 3.\n");
continue; // Skip the rest of the loop and start from the beginning
}
// Adjust for 0 indexing
row--;
col--;
if (isValidMove(row, col)) {
grid[row][col] = currentPlayer;
if (checkWin(currentPlayer)) {
printf("\nCurrent Grid:\n");
displayGrid();
printf("Player %c wins!\n", currentPlayer);
saveToFile();
break;
}
if (isDraw()) {
printf("\nCurrent Grid:\n");
displayGrid();
printf("The game is a draw!\n");
saveToFile();
break;
}
// Save current game state after every valid move.
saveToFile();
// Change the player for the next turn
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
} else {
printf("Invalid move. Try again.\n");
}
}
return 0;
}