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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* Header File Declaration */
#include <stdio.h>
#include <stdlib.h>
/* NO other header files are allowed */
/****************************************/
/****************************************/
/* Given Global Variables and Definition */
#define H 6 // height
#define W 6 // width

char candies[] = {'*', '#', '@', '%'};
/* NO other global variables are allowed */
/****************************************/

/****************************************/

/* Part I Functions */
void initGameBoard(int board[][W], int board_sample[][W]);
void printGameBoard(int board[][W]);
int askForSwap(int board[][W]);
void swap(int board[][6], int row1, int col1, int row2, int col2) ;
int findAndRemoveMatch(int board[][W], int row, int col);
int isMatching(int board[6][6], int row, int col);
/* Part II Functions */
int initGameBoardFromFile(int board[][W], int stacks[]);
void applyGravity(int board[][W]);
int fillEmpty(int board[][W], int stacks[], int current, int numCandies);
int cascade(int board[][6], int stacks[], int current, int numCandies);

/****************************************/
// Function Definitions/

/**
* Main() will call this function in the beginning.
* initGameBoard() load the game board with the pre-defined board_sample array
* @param board The gameboard the game is using
* @param board_sample The given game board sample values
*/

//(Part I)初始化GameBoard (copy board_sample 出黎)
void initGameBoard(int board[H][W], int board_sample[][W]) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
board[i][j] = board_sample[i][j];
}
}
}

/**
* [Part II]
* Main() will call this function in the beginning.
* initGameBoardFromFile will load the file "board.txt" to load two information:
* (1) Game Board ; (2) Candy Stacks
* If the file loading is unsuccessful, quit the program with the error message.
* @param board The Game board the program is using.
* @param stacks The stack array to store candies. It will be inititalized from the file.
* @return The number of candies in the stack.
*/
int initGameBoardFromFile(int board[][W], int stacks[]) {

}

/**
* Various functions will call this.
* printGameBoard() will print the current game board in the specified format
* @param board The game board the game is using
*/
//print當前GameBoard的狀態
void printGameBoard(int board[][W]) {
printf("=====\n");
printf(" | 0 | 1 | 2 | 3 | 4 | 5 |\n");
for (int i = 0; i < H; i++) {
printf(" %d|", i);
for (int j = 0; j < W; j++) {
if (board[i][j] >= 0 && board[i][j] < 4) {
printf(" %c |", candies[board[i][j]]);
}
else {
printf(" |");
}
}
printf("\n");
}
}

/**
* Main() will call this function.
* askForSwap() will perform the major game logic by (1) asking the users which candy to swap
* (2)Validate whether the swapping can be done (3) Perform the swapping
* @param board The game board the game is using.
* @return 0 if the error is encountered (e.g. Coordinates out of bound);
* 1 if swapping is successfully
*/
int askForSwap(int board[][W]) {
int row, col;
char direction;

//輸入coordinates
printf("Enter the coordinate (row, column) of the candy:");
scanf("%d %d", &row, &col);

//1. 檢查input的coordinates有沒有超出邊界
if (row < 0 || row >= H || col < 0 || col >= W) {
printf("Coordinates Out of Bound.\n");
return 0;
}
//2. 檢查是否Empty cell
if (board[row][col] == -1) {
printf("Empty Cell Selected.\n");
return 0;
}

while (1) {
printf("Enter the direction to swap (U for Up, D for Down, L for Left, R for Right):");
scanf(" %c", &direction);



int targetRow = row, targetCol = col;
//switch 是用來分辨輸入了哪個方向就做哪個動作
switch (direction) {
case 'U': targetRow--; break;
case 'D': targetRow++; break;
case 'L': targetCol--; break;
case 'R': targetCol++; break;
//check if user 有輸入正確的U/D/R/L
default:
printf("Wrong Direction Input.\n");
return 0;
}

//檢查移動有沒有超出邊界
if (targetRow < 0 || targetRow >= H || targetCol < 0 || targetCol >= W) {
printf("Move Out of Bound.\n");
return 0;
}
//檢查目標位置是否Empty cell
if (board[targetRow][targetCol] == -1) {
printf("Empty Cell Selected.\n");
return 0;
}
//執行交換
swap(board, row, col, targetRow, targetCol);

//檢查有沒有match
if (findAndRemoveMatch(board, row, col) || findAndRemoveMatch(board, targetRow, targetCol)) {
printGameBoard(board);
return 1;
}
else {
//No match
printf("No Match found!\n");
//還原
swap(board, targetRow, targetCol, row, col);
return 0;
}
}

}

/**
* Various functions will call this
* swap() will perform value swapping of two cells,
* with the given source and destination coordinates respectively.
* @param board the game board the game is using
* @param row1 The row number of the source cell
* @param col1 The column number of the source cell
* @param row2 The row number of the destination cell
* @param col2 The column number of the destination cell
*/
// 交換两个位置的candys
void swap(int board[][W], int row1, int col1, int row2, int col2) {
int temp = board[row1][col1];
board[row1][col1] = board[row2][col2];
board[row2][col2] = temp;
}

/**
* Various functions will call this.
* findAndRemoveMatch() will find possible matches at the given coordinates (row,col)
* for all four directions (Up, Down, Left, Right).
* If found, turn the cell to ASCII 32 (' ').
*
* @param board The game board the game is using
* @param row The row number of the given coordinate
* @param col The column number of the given coordinate
* @return 1 if there is a match in the board, 0 if there is not.
*/
int findAndRemoveMatch(int board[][W], int row, int col) {
int matched = 0;
// 水平匹配检查
//1. 向左邊檢查
//col要大於1才有可能跟左邊(left)的組成三個
if (col > 1 && board[row][col] == board[row][col - 1] && board[row][col] == board[row][col - 2]) {
matched = 1;
//看右邊有沒有其他一樣的candys能組成五個
if (col < (W-2) && board[row][col] == board[row][col + 1] && board[row][col] == board[row][col + 2]) {
board[row][col] = board[row][col - 1] = board[row][col - 2] = board[row][col + 1] = board[row][col + 2] = -1;
printf("Horizontal Match found at row %d!\n", row);
return matched;
}
//看右邊有沒有其他一樣的candys能組成四個
if (col < (W-1) && board[row][col] == board[row][col + 1]) {
board[row][col] = board[row][col - 1] = board[row][col - 2] = board[row][col + 1] = -1;
printf("Horizontal Match found at row %d!\n", row);
return matched;
}
board[row][col] = board[row][col - 1] = board[row][col - 2] = -1;
printf("Horizontal Match found at row %d!\n", row);
return matched;
}
//2. 向右邊檢查
//col要少於W-2才有可能跟右邊(right)的組成三個
if (col < (W-2) && board[row][col] == board[row][col + 1] && board[row][col] == board[row][col + 2]) {
matched = 1;
//看左邊有沒有其他一樣的candys能組成四個
if (col > 0 && board[row][col] == board[row][col - 1]) {
board[row][col] = board[row][col - 1] = board[row][col + 1] = board[row][col + 2] = -1;
printf("Horizontal Match found at row %d!\n", row);
return matched;
}
board[row][col] = board[row][col + 1] = board[row][col + 2] = -1;
printf("Horizontal Match found at row %d!\n", row);
return matched;
}
//3. 檢查左右各一
if (col > 0 && col < (W-1) && board[row][col] == board[row][col + 1] && board[row][col] == board[row][col - 1]) {
matched = 1;
board[row][col] = board[row][col + 1] = board[row][col - 1] = -1;
printf("Horizontal Match found at row %d!\n", row);
return matched;
}

// 垂直匹配检查
//1. 向上邊檢查
//row要大於1才有可能跟上邊(upward)的組成三個
if (row > 1 && board[row][col] == board[row - 1][col] && board[row][col] == board[row - 2][col]) {
matched = 1;
//看下邊有沒有其他一樣的candys能組成五個
if (row < (H-2) && board[row][col] == board[row + 1][col] && board[row][col] == board[row + 2][col]) {
board[row][col] = board[row - 1][col] = board[row - 2][col] = board[row + 1][col] = board[row + 2][col] = -1;
printf("Vertical Match found at column %d!\n", col);
return matched;
}
//看下邊有沒有其他一樣的candys能組成四個
if (row < (H-1) && board[row][col] == board[row + 1][col]) {
board[row][col] = board[row - 1][col] = board[row - 2][col] = board[row + 1][col] = -1;
printf("Vertical Match found at column %d!\n", col);
return matched;
}
board[row][col] = board[row - 1][col] = board[row - 2][col] = -1;
printf("Vertical Match found at column %d!\n", col);
return matched;
}
//2. 向下邊檢查
//row要少於H-2才有可能跟下邊(downward)的組成三個
if (row < (H-2) && board[row][col] == board[row + 1][col] && board[row][col] == board[row + 2][col]) {
matched = 1;
//看上邊有沒有其他一樣的candys能組成四個
if (row > 0 && board[row][col] == board[row - 1][col]) {
board[row][col] = board[row - 1][col] = board[row + 1][col] = board[row + 2][col] = -1;
printf("Vertical Match found at column %d!\n", col);
return matched;
}
board[row][col] = board[row +1][col] = board[row + 2][col] = -1;
printf("Vertical Match found at column %d!\n", col);
return matched;
}
//3. 檢查上下各一
if (row > 0 && col < (H-1) && board[row][col] == board[row + 1][col] && board[row][col] == board[row - 1][col]) {
matched = 1;
board[row][col] = board[row + 1][col] = board[row - 1][col] = -1;
printf("Vertical Match found at column %d!\n", col);
return matched;
}
return matched;
}

/**
* Main() function will call this.
* isGameOver() is to check whether there exists a game over situation, i.e.
* no more match can be made under the current board scenerio.
*
* @param board The game board the game is using.
* @return 1 if it is game over; 0 if it isn't .
*/
int isGameOver(int board[][W]) {
int row;
int col;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
//跟右邊交換位置檢查
if (j < (W-1)){
row = i;
col = j+1;
swap(board, i, j, row, col);
if (isMatching(board, i, j)||isMatching(board, row, col)) {
swap(board, row, col, i, j);
return 1;
}
swap(board, row, col, i, j);
}
//跟下面交換位置檢查
if (i < (H-1)){
row = i+1;
col = j;
swap(board, i, j, row, col);
if (isMatching(board, i, j)||isMatching(board, row, col)) {
swap(board, row, col, i, j);
return 1;
}
swap(board, row, col, i, j);
}
}
return 0;
}
}

/**
* isGameOver() will call this function.
* isMatching will check if there is at least 1 match-three at the given coordinate (row,col)
* for four directions (Top,Down,Left,Right).
* @param board The game board the game is using.
* @param row The row number of the target cell
* @param col The column number of the target cell
* @return 1 if there is at least 1 match in any direction; 0 if there is not.
*/
int isMatching(int board[6][6], int row, int col) {
//閹割版findAndRemoveMatch()沒有remove
int matched = 0;
// 水平匹配检查
//1. 向左邊檢查
//col要大於1才有可能跟左邊(left)的組成三個
if (col > 1 && board[row][col] == board[row][col - 1] && board[row][col] == board[row][col - 2] && board[row][col] != -1) {
matched = 1;
return matched;
}
//2. 向右邊檢查
//col要少於W-2才有可能跟右邊(right)的組成三個
if (col < (W-2) && board[row][col] == board[row][col + 1] && board[row][col] == board[row][col + 2] && board[row][col] != -1) {
matched = 1;
return matched;
}
//3. 檢查左右各一
if (col > 0 && col < (W-1) && board[row][col] == board[row][col + 1] && board[row][col] == board[row][col - 1] && board[row][col] != -1) {
matched = 1;
return matched;
}

// 垂直匹配检查
//1. 向上邊檢查
//row要大於1才有可能跟上邊(upward)的組成三個
if (row > 1 && board[row][col] == board[row - 1][col] && board[row][col] == board[row - 2][col] && board[row][col] != -1) {

matched = 1;
return matched;
}
//2. 向下邊檢查
//row要少於H-2才有可能跟下邊(downward)的組成三個
if (row < (H-2) && board[row][col] == board[row + 1][col] && board[row][col] == board[row + 2][col] && board[row][col] != -1) {
matched = 1;
return matched;
}
//3. 檢查上下各一
if (row > 0 && col < (H-1) && board[row][col] == board[row + 1][col] && board[row][col] == board[row - 1][col] && board[row][col] != -1) {
matched = 1;
return matched;
}
return matched;
}

/**
* [Part II]
* askForSwap() and cascade() will call this function
* applyGravity() will scan the all columns from left to right, and
* if there are empty cells, it will move down the candy. Lastly, print the
* gameboard.
* @param board The game board the game is using
*/
void applyGravity(int board[][6]) {

}

/**
* [Part II]
* askForSwap() and cascade() will call this.
* fillEmpty() will scan the columns from left to right. If there are empty cells,
* it will fill them with candies got from the stacks, in bottom-to-top manner. Lastly,
* print the gameboard.
* @param board the game board the game is using
* @param stacks the 1D array containing the candies
* @param current the counter of the next candy location in stacks array
* @param numCandies the total number of candies in the stacks
* @return the updated current value, i.e. the updated counter pointing to the next
* available candy
*/
int fillEmpty(int board[][W], int stacks[], int current, int numCandies) {

}

/**
* [Part II]
* main() function will call this
* cascade() will continously check the board to see if there is any matches
* and perform removal until no further matches can be found.
* First, you can apply the gravity and fill in the empty cells.
* Then, for each cells on the board and if it is not empty, try to call
* findAndRemoveMatch() to remove matches if any. If there is a match,
* print the specified message.
* Remember to collect the return value of current in fillEmpty() such that
* you return to the main function to update the variable.
* @param board the game board the game is using
* @param stacks the 1D array containing the candies
* @param current the counter of the next candy location in stacks array
* @param numCandies the total number of candies in the stacks
* @return the updated current value, i.e. the updated counter pointing to the next
* available candy
*/
int cascade(int board[][6], int stacks[], int current, int numCandies) {

}
/* Main Function */
/**
* The main function will hold the game loop and hold the game logic.
* main() will call various functions to accompolish various tasks such as
* initializing the game board, asking for Input, and etc.
* It will hold an infinite loop to repeatly hold the game. If it is a game over status,
* i.e. isGameOver() returns one (1), then it will break out of the loop and the program
* finishes.
*
* @return 0
*/
int main(void) {
// This is the variable holding a sample board value
int board_sample[6][6] = {
{1,3,2,0,1,0},
{2,2,0,1,1,3},
{1,3,3,2,3,0},
{3,0,2,1,0,2},
{1,0,2,3,3,2},
{3,2,1,0,3,3}
};

int board[H][W] = {0}; // The game board, initialized to 0
int stacks[100] = {0}; // Stacks array containing candy, Maximum capacity 100
int numCandies = 0; // Number of candies in the stacks
int current = 0; // the counter pointing to the next available candy
// You may declare more variables if needed.

// 1. First, you may initialize the board first
// For part 1, you may use initGameBoard() while for part 2, initGameBoardFromFile()
initGameBoard(board, board_sample);

// Then, you may hold a loop to keep the game running
while (1) {

// printf for your reference
//printf("=====\n");
//printf("New Round:\n");
printf("=====\nNew Round:\n");

// Print the board using printGameBoard()
printGameBoard(board);

// Call askForSwap() to ask which candy to swap and perform swapping if successful.
// If not, print error message, shown for your reference
if (!askForSwap(board)) {
// printf("Please try again.\n");
printf("Please try again.\n");
}

// Otherwise, continue the game logic by applying gravity and filling empty cells
// For part II, you may replace the above two to cascade(), as cascade() will be
// responsible to call at that moment.




// Check if it is a game over status. If yes, print the message and break the loop
// printf is left for your reference.
if (isGameOver(board) == 0) {
printf("Game Over! No more possible moves.\n");
break;
}
// printf("Game Over! No more possible moves.\n");
}
// Hope you enjoy the game : )
return 0;
}