-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.c
75 lines (66 loc) · 1.78 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>
#define ROWS 3
#define COLUMNS 3
void printBoard(char board[ROWS][COLUMNS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLUMNS; j++) {
printf("|%c|", board[i][j]);
}
printf("\n");
}
}
int checkWin(char board[ROWS][COLUMNS]) {
int i, j;
for (i = 0; i < ROWS; i++) { // Check rows
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {
return 1;
}
}
for (j = 0; j < COLUMNS; j++) { // Check columns
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ') {
return 1;
}
}
// Check diagonal
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {
return 1;
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != ' ') {
return 1;
}
return 0;
}
int main(){
int i, j, x, y, moveCount = 0;
char player = 'X', board[ROWS][COLUMNS];
// Initialize the board
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLUMNS; j++) {
board[i][j] = ' ';
}
}
while (1) {
printf("Player %c, enter your move: Row SPACE Column): ", player);
scanf("%d%d", &x, &y);
x--; y--; //substracts 1 from each input so it can be used as coordinates for the 2d array
if (x >= 0 && x < ROWS && y >= 0 && y < COLUMNS && board[x][y] == ' ') {
board[x][y] = player;
moveCount++;
if (checkWin(board)) {
printf("Player %c wins!\n", player);
printBoard(board);
break;
}
if (moveCount == ROWS * COLUMNS) {
printf("Draw.\n");
printBoard(board);
break;
}
player = (player == 'X') ? 'O' : 'X'; //switches players
} else {
printf("Invalid move.\n");
}
printBoard(board);
}
}