-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdotandbox.c
182 lines (163 loc) · 3.66 KB
/
dotandbox.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROWS 6
#define COLS 7
#define EMPTY 0
#define PLAYER_ONE 1
#define PLAYER_TWO 2
int board[ROWS][COLS];
void init_board()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
board[i][j] = EMPTY;
}
}
}
void print_board()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("|");
switch (board[i][j])
{
case EMPTY:
printf(" ");
break;
case PLAYER_ONE:
printf("X");
break;
case PLAYER_TWO:
printf("O");
break;
}
}
printf("|\n");
}
printf("-----------------------------\n");
}
bool check_win(int player)
{
// Check horizontal
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS - 3; j++)
{
if (board[i][j] == player &&
board[i][j + 1] == player &&
board[i][j + 2] == player &&
board[i][j + 3] == player)
{
return true;
}
}
}
// Check vertical
for (int i = 0; i < ROWS - 3; i++)
{
for (int j = 0; j < COLS; j++)
{
if (board[i][j] == player &&
board[i + 1][j] == player &&
board[i + 2][j] == player &&
board[i + 3][j] == player)
{
return true;
}
}
}
// Check diagonal (down-right)
for (int i = 0; i < ROWS - 3; i++)
{
for (int j = 0; j < COLS - 3; j++)
{
if (board[i][j] == player &&
board[i + 1][j + 1] == player &&
board[i + 2][j + 2] == player &&
board[i + 3][j + 3] == player)
{
return true;
}
}
}
// Check diagonal (down-left)
for (int i = 0; i < ROWS - 3; i++)
{
for (int j = 3; j < COLS; j++)
{
if (board[i][j] == player &&
board[i + 1][j - 1] == player &&
board[i + 2][j - 2] == player &&
board[i + 3][j - 3] == player)
{
return true;
}
}
}
return false;
}
bool check_full()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (board[i][j] == EMPTY)
{
return false;
}
}
}
return true;
}
int get_move(int player)
{
int col;
do
{
printf("Player %d, choose a column (1-7): ", player);
scanf("%d", &col);
col--;
} while (col < 0 || col >= COLS || board[0][col] != EMPTY);
// Insert the player's piece into the selected column
for (int i = ROWS - 1; i >= 0; i--)
{
if (board[i][col] == EMPTY)
{
board[i][col] = player;
return i;
}
}
return -1; // This should never happen if check_full() is called first
}
int main()
{
int turn = PLAYER_ONE;
int row, col;
init_board();
print_board();
while (true)
{
col = get_move(turn);
row = get_move(col);
board[row][col] = turn;
print_board();
if (check_win(turn))
{
printf("Player %d wins!\n", turn);
break;
}
else if (check_full())
{
printf("Game over! It's a tie.\n");
break;
}
turn = (turn == PLAYER_ONE) ? PLAYER_TWO : PLAYER_ONE;
}
return 0;
}