-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_four_blank.c
267 lines (221 loc) · 6.84 KB
/
connect_four_blank.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "connect_four.h"
// COLORS
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define BLUE "\x1b[34m"
#define MAGENTA "\x1b[35m"
#define CYAN "\x1b[36m"
#define RESET "\x1b[0m"
int current_token_pos = 0; // GLOBAL VARIABLE TO KEEP TRACK OF TOKEN COLUMN
/*
* print_game
* DESCRIPTION: This function prints out the current state of game board.
* INPUTS: game_board (2D array), piece (0 or 1 depending on whose turn it is)
* OUTPUTS: current state of the game board
* RETURN VALUE: none (this is a void function)
*/
void print_game(int game_board[rows][cols], int piece) {
// OUR TOKEN WILL LOOK LIKE THE LETTER O
char token = 'O';
// ITERATORS
int i, j;
// DRAW THE MOVING TOKEN AT THE TOP
printf("\n");
printf(" ");
for (i = 0; i < cols; i++) {
if (i == current_token_pos) {
if (piece == 0) {
printf(RED" O "RESET);
}
else {
printf(BLUE" O "RESET);
}
}
else {
printf(" "); // top box char
}
}
printf(" ");
printf("\n");
// DRAW THE GAME BOARD
printf("\u2554");
for (i = 0; i < cols * 3; i++) {
printf("\u2550");
}
printf("\u2557\n");
for(i = 0; i < rows; i++){
printf("\u2551");
for(j = 0; j < cols; j++){
if (game_board[i][j] == -1) {
printf(" ");
}
else {
switch( game_board[i][j] ) {
case 0:
printf(" "RED"%c"RESET" ", token);
break;
case 1:
printf(" "BLUE"%c"RESET" ", token);
break;
}
}
}
printf("\u2551\n");
}
printf("\u255A");
for (i = 0; i < cols * 3; i++) {
printf("\u2550");
}
printf("\u255D\n");
}
/*
* place_token
* DESCRIPTION: Places a new token in the game board.
* INPUTS: game_board (2D array), last_available_row (1D array) piece (0 or 1 depending on whose turn it is)
* OUTPUTS: the state of the game board after a new token has been placed
* RETURN VALUE: none (this is a void function)
*/
void place_token(int game_board[rows][cols], int first_available_row[cols], int piece) {
// CHECK IF THERE IS NO AVAILABLE ROW WITH CURRENT COLUMN POSITION
if (first_available_row[current_token_pos] == -1) {
return;
}
//**TODO**
// GET FIRST AVAILABLE ROW IN THE COLUMN
// int availableRow = __________________;
// UPDATE THE LAST AVAILABLE ROW IN THAT COLUMN (Hint: Decrement the value)
// _______________________________;
// PLACE TOKEN
// game_board[availableRow][current_token_pos] = piece;
// CLEAR SCREEN AND PRINT OUT NEW STATE OF THE GAME
system("clear");
print_game(game_board, piece);
}
/*
* isGameOver
* DESCRIPTION: Checks if the game is over (if player has gotten 4 consecutive)
* INPUTS: game_board (2D array), piece (0 or 1 depending on whose turn it is)
* OUTPUTS: none
* RETURN VALUE: 1 if the game is over, 0 if the game is not over
*/
int isGameOver(int game_board[rows][cols], int piece) {
// ITERATORS
int r, c;
//**TODO**//
// Check for horizontal win
// "O" "O" "O" "O"
// UNCOMMENT THE FOLLOWING LINES
// for (r = 0; r < rows; r++) {
// for (c = 0; c < cols - 3; c++) {
// if (game_board[r][c] == piece && ____________ && game_board[r][c + 2] == piece && ____________)
// return ___;
// }
// }
// Check for vertical win
/*
"O"
"O"
"O"
"O"
*/
// UNCOMMENT THE FOLLOWING LINES
// for (c = 0; c < cols; c++) {
// for (r = 0; r < rows - 3; r++) {
// if (game_board[r][c] == piece && ____________ && game_board[r + 2][c] == piece && ____________)
// return ___;
// }
// }
// CHECK FOR POSITIVE DIAGONAL WIN
/*
"O"
"O"
"O"
"O"
*/
// UNCOMMENT THE FOLLOWING LINES
// for (c = 0; c < cols - 3; c++) {
// for (r = 0; r < rows - 3; r++) {
// if (game_board[r][c] == piece && game_board[r + 1][c + 1] == piece && ____________ && ____________)
// return ___;
// }
// }
// CHECK FOR NEGATIVE DIAGONAL WIN
/*
"O"
"O"
"O"
"O"
*/
// UNCOMMENT THE FOLLOWING LINES
// for (c = 0; c < cols - 3; c++) {
// for (r = 3; r < rows; r++) {
// if (game_board[r][c] == piece && game_board[r - 1][c + 1] == piece && ____________ && ____________)
// return ___;
// }
// }
// UNCOMMENT THE FOLLOWING LINE
// return ___;
}
int main() {
system("clear"); // clear the screen
// GET INPUT DIMENSIONS FROM USER
char buf[200];
char garbage[2];
printf("CONNECT FOUR: Enter dimensions (rows columns):");
if (NULL == fgets(buf,200,stdin)) {
printf("\nProgram Terminated.\n");
return 2;
}
if (2 != sscanf(buf,"%d%d%1s", &rows, &cols, garbage) || rows < 0 || cols < 0) {
printf("invalid dimensions\n");
return 2;
}
// INITIALIZE USEFUL VARIABLES
int i, j;
int game_board[rows][cols];
int first_available_row[cols];
// INITIALIZE THE GAME BOARD TO EMPTY CELLS
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
game_board[i][j] = -1; // -1 means that (row, col) is available
}
}
// INITIALIZE THE FIRST AVAILABLE ROW
for (i = 0; i < cols; i++) {
first_available_row[i] = rows - 1; // 0-indexed
}
// VARIABLE TO KEEP TRACK OF PLAYER TURN
int piece = 0; // 0 means Player 1, 1 means Player 2
print_game(game_board, piece);
char c;
do {
c = getch(); // get keyboard input
switch(c) {
case 'a':
if (current_token_pos > 0) {
current_token_pos--;
}
break;
case 'd':
if (current_token_pos < cols - 1) {
current_token_pos++;
}
break;
case '\n':
//**TODO**
// THE USER PRESSED ENTER, SO LET'S PLACE THE TOKEN. (Hint: Call the function place_token)
// ____________;
// CHECK IF GAME IS OVER. (Hint: Call the function isGameOver)
// if (____________) {
// printf("Player %d has won! Game over.\n", piece + 1);
// return 0;
// }
current_token_pos = 0; // reset token column position
piece = (piece + 1) % 2; // update to next player's turn
}
system("clear");
print_game(game_board, piece);
} while (c != 'q');
return 0;
}