用C语言写一个扫雷小游戏,要求在一个9 × 9 (初级),16 × 16 (中级),16 × 30 (高级),或自定义大小的矩阵
时间: 2023-06-20 12:08:09 浏览: 130
以下是一个简单的扫雷游戏的C语言代码,支持9x9、16x16、16x30的游戏难度,以及自定义大小:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 30
#define COLS 16
#define MINES 99
int board[ROWS][COLS];
int visible[ROWS][COLS];
void init_board(int rows, int cols, int mines) {
int i, j, k, r, c;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
board[i][j] = 0;
visible[i][j] = 0;
}
}
srand(time(NULL));
for (k = 0; k < mines; k++) {
do {
r = rand() % rows;
c = rand() % cols;
} while (board[r][c] == 9);
board[r][c] = 9;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
if (r + i >= 0 && r + i < rows && c + j >= 0 && c + j < cols && board[r + i][c + j] != 9) {
board[r + i][c + j]++;
}
}
}
}
}
void print_board(int rows, int cols) {
int i, j;
printf("\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (visible[i][j] == 0) {
printf(" # ");
} else if (visible[i][j] == 1) {
printf(" %d ", board[i][j]);
} else if (visible[i][j] == 2) {
printf(" F ");
}
}
printf("\n");
}
}
void reveal(int rows, int cols, int r, int c) {
if (r < 0 || r >= rows || c < 0 || c >= cols || visible[r][c] != 0) {
return;
}
visible[r][c] = 1;
if (board[r][c] == 0) {
reveal(rows, cols, r - 1, c - 1);
reveal(rows, cols, r - 1, c);
reveal(rows, cols, r - 1, c + 1);
reveal(rows, cols, r, c - 1);
reveal(rows, cols, r, c + 1);
reveal(rows, cols, r + 1, c - 1);
reveal(rows, cols, r + 1, c);
reveal(rows, cols, r + 1, c + 1);
}
}
int check_win(int rows, int cols, int mines) {
int i, j, count = 0;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (visible[i][j] == 1) {
count++;
}
}
}
return count == (rows * cols - mines);
}
void play_game(int rows, int cols, int mines) {
int r, c, flag, won = 0, lost = 0;
init_board(rows, cols, mines);
while (!won && !lost) {
print_board(rows, cols);
printf("Enter row and column (e.g. 2 3): ");
scanf("%d %d", &r, &c);
printf("Enter 0 to reveal, 1 to flag: ");
scanf("%d", &flag);
if (flag == 0) {
if (board[r][c] == 9) {
lost = 1;
} else {
reveal(rows, cols, r, c);
won = check_win(rows, cols, mines);
}
} else if (flag == 1) {
visible[r][c] = 2;
}
}
print_board(rows, cols);
if (won) {
printf("You won!\n");
} else if (lost) {
printf("You lost!\n");
}
}
int main() {
int rows, cols, mines;
int level;
printf("Choose a difficulty level:\n");
printf("1. Beginner (9x9, 10 mines)\n");
printf("2. Intermediate (16x16, 40 mines)\n");
printf("3. Expert (16x30, 99 mines)\n");
printf("4. Custom\n");
printf("Enter your choice (1-4): ");
scanf("%d", &level);
switch (level) {
case 1:
rows = 9;
cols = 9;
mines = 10;
break;
case 2:
rows = 16;
cols = 16;
mines = 40;
break;
case 3:
rows = 16;
cols = 30;
mines = 99;
break;
case 4:
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
printf("Enter number of mines: ");
scanf("%d", &mines);
break;
default:
printf("Invalid choice.\n");
return 0;
}
play_game(rows, cols, mines);
return 0;
}
```
阅读全文