This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 295
/
TicTacToe.c
75 lines (61 loc) · 2 KB
/
TicTacToe.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
#include <stdio.h>
char board[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
//board print
void printBoard() {
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() {
int winning_combinations[8][3] = {
{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, // Rows
{1, 4, 7}, {2, 5, 8}, {3, 6, 9}, // Columns
{1, 5, 9}, {3, 5, 7} // Diagonals
};
for(int i = 0; i < 8; i++) {
if(board[winning_combinations[i][0]] == board[winning_combinations[i][1]] &&
board[winning_combinations[i][1]] == board[winning_combinations[i][2]]) {
return 1;
}
}
for(int i = 1; i <= 9; i++) {
if(board[i] != 'X' && board[i] != 'O') {
return -1; // Game is still running
}
}
return 0; // Game is a draw
}
int main() {
int player = 1, input, status = -1;
char mark;
while(status == -1) {
printBoard();
mark = (player % 2) ? 'X' : 'O'; // Automatically assign 'X' or 'O' based on player turn
printf("Enter the position (1-9) for player %d: ", (player % 2) ? 1 : 2);
scanf("%d", &input);
// check input
if(input < 1 || input > 9 || board[input] == 'X' || board[input] == 'O') {
printf("Invalid input. Please try again.\n");
continue;
}
board[input] = mark;
status = checkWin();
player++;
}
printBoard();
if(status == 1) {
printf("==> Player %d wins the match.\n", --player);
} else {
printf("==> The game is a draw.\n");
}
return 0;
}