voidInitBoard(char board[ROWS][COLS], int rows, int cols, charset);
"game.c" voidInitBoard(char board[ROWS][COLS], int rows, int cols, charset) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = set; } } }
voidDisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("-----------扫雷游戏-----------\n"); for (i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("-----------扫雷游戏-----------\n"); }
优化一下棋盘打印:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
voidDisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("-----------扫雷游戏-----------\n"); // 打印列号 for (i = 0; i <= col; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("-----------扫雷游戏-----------\n"); }
voidSetMine(char board[ROWS][COLS], int row, int col, int count) { // 在棋盘上布置雷 while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } }