forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic_tac_toe.c
More file actions
128 lines (112 loc) · 3.58 KB
/
tic_tac_toe.c
File metadata and controls
128 lines (112 loc) · 3.58 KB
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
/* tictactoe.c
Simple CLI Tic-Tac-Toe for two players
Compile: gcc -o tictactoe tic_tac_toe.c
Run: ./tictactoe
*/
#include <stdio.h>
#include <stdlib.h>
void print_board(char b[3][3]) {
puts("\nCurrent board:");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
char c = b[i][j];
if (c == ' ') {
// show cell index to help players choose
printf(" %d ", i*3 + j + 1);
} else {
printf(" %c ", c);
}
if (j < 2) printf("|");
}
putchar('\n');
if (i < 2) printf("---+---+---\n");
}
putchar('\n');
}
int check_winner(char b[3][3]) {
// returns 1 if X wins, -1 if O wins, 0 otherwise
for (int i = 0; i < 3; ++i) {
// rows
if (b[i][0] != ' ' && b[i][0] == b[i][1] && b[i][1] == b[i][2])
return (b[i][0] == 'X') ? 1 : -1;
// cols
if (b[0][i] != ' ' && b[0][i] == b[1][i] && b[1][i] == b[2][i])
return (b[0][i] == 'X') ? 1 : -1;
}
// diagonals
if (b[0][0] != ' ' && b[0][0] == b[1][1] && b[1][1] == b[2][2])
return (b[0][0] == 'X') ? 1 : -1;
if (b[0][2] != ' ' && b[0][2] == b[1][1] && b[1][1] == b[2][0])
return (b[0][2] == 'X') ? 1 : -1;
return 0;
}
int moves_left(char b[3][3]) {
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
if (b[i][j] == ' ') return 1;
return 0;
}
void clear_stdin(void) {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) { }
}
int main(void) {
char board[3][3];
char play_again = 'n';
do {
// initialize board
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
board[i][j] = ' ';
char player = 'X';
int winner = 0;
printf("Tic-Tac-Toe (CLI)\n");
printf("Players: X and O\n");
printf("Enter cell numbers 1-9 as shown on board to place your mark.\n");
while (!winner && moves_left(board)) {
print_board(board);
int choice = 0;
int ok = 0;
while (!ok) {
printf("Player %c, enter your move (1-9): ", player);
if (scanf("%d", &choice) != 1) {
printf("Invalid input. Please enter a number 1-9.\n");
clear_stdin();
continue;
}
clear_stdin(); // remove trailing newline/extra chars
if (choice < 1 || choice > 9) {
printf("Choose a number between 1 and 9.\n");
continue;
}
int r = (choice - 1) / 3;
int c = (choice - 1) % 3;
if (board[r][c] != ' ') {
printf("Cell already taken. Choose another.\n");
continue;
}
board[r][c] = player;
ok = 1;
}
winner = check_winner(board);
if (winner != 0) break;
if (!moves_left(board)) break;
player = (player == 'X') ? 'O' : 'X';
}
print_board(board);
if (winner == 1) {
puts("Player X wins! 🎉");
} else if (winner == -1) {
puts("Player O wins! 🎉");
} else {
puts("It's a draw! 🤝");
}
printf("Play again? (y/n): ");
if (scanf(" %c", &play_again) != 1) {
play_again = 'n';
}
clear_stdin();
} while (play_again == 'y' || play_again == 'Y');
puts("Thanks for playing! Goodbye.");
return 0;
}