用C语言程序做一个扫雷游戏
时间: 2023-12-29 10:17:15 浏览: 84
好的,以下是一个简单的扫雷游戏的C语言代码,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 9
#define COLS 9
#define MINE_NUM 10
// 扫雷格子结构体
typedef struct _grid {
int row;
int col;
int is_mine;
int is_revealed;
int is_flagged;
int neighbor_mine_count;
} Grid;
// 创建一个扫雷格子
Grid create_grid(int row, int col) {
Grid grid;
grid.row = row;
grid.col = col;
grid.is_mine = 0;
grid.is_revealed = 0;
grid.is_flagged = 0;
grid.neighbor_mine_count = 0;
return grid;
}
// 初始化扫雷棋盘
void init_board(Grid board[][COLS]) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
board[row][col] = create_grid(row, col);
}
}
}
// 随机生成地雷位置
void generate_mines(Grid board[][COLS]) {
int count = 0;
while (count < MINE_NUM) {
int row = rand() % ROWS;
int col = rand() % COLS;
if (!board[row][col].is_mine) {
board[row][col].is_mine = 1;
count++;
}
}
}
// 检查是否越界
int is_out_of_bounds(int row, int col) {
return row < 0 || row >= ROWS || col < 0 || col >= COLS;
}
// 更新邻居雷数
void update_neighbor_mine_count(Grid board[][COLS]) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (board[row][col].is_mine) {
continue;
}
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) {
continue;
}
int r = row + i;
int c = col + j;
if (!is_out_of_bounds(r, c) && board[r][c].is_mine) {
count++;
}
}
}
board[row][col].neighbor_mine_count = count;
}
}
}
// 显示扫雷棋盘
void display_board(Grid board[][COLS], int is_game_over) {
printf(" ");
for (int col = 0; col < COLS; col++) {
printf("%d ", col);
}
printf("\n");
printf(" +");
for (int col = 0; col < COLS; col++) {
printf("-+");
}
printf("\n");
for (int row = 0; row < ROWS; row++) {
printf("%d |", row);
for (int col = 0; col < COLS; col++) {
Grid grid = board[row][col];
if (is_game_over && grid.is_mine) {
printf("*|");
} else if (grid.is_revealed) {
if (grid.neighbor_mine_count == 0) {
printf(" |");
} else {
printf("%d|", grid.neighbor_mine_count);
}
} else if (grid.is_flagged) {
printf("F|");
} else {
printf(" |");
}
}
printf("\n");
printf(" +");
for (int col = 0; col < COLS; col++) {
printf("-+");
}
printf("\n");
}
}
// 当前位置是否可扫
int is_valid_reveal(Grid board[][COLS], int row, int col) {
if (is_out_of_bounds(row, col) || board[row][col].is_revealed || board[row][col].is_flagged) {
return 0;
}
return 1;
}
// 扫描当前位置
void reveal(Grid board[][COLS], int row, int col) {
if (board[row][col].is_mine) {
printf("你踩雷了!游戏结束!\n");
display_board(board, 1);
exit(0);
}
board[row][col].is_revealed = 1;
if (board[row][col].neighbor_mine_count == 0) {
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int r = row + i;
int c = col + j;
if (is_valid_reveal(board, r, c)) {
reveal(board, r, c);
}
}
}
}
}
// 插旗
void flag(Grid board[][COLS], int row, int col) {
if (board[row][col].is_revealed) {
printf("该位置已经扫描过了!\n");
return;
}
board[row][col].is_flagged = 1;
}
// 撤销插旗
void unflag(Grid board[][COLS], int row, int col) {
if (board[row][col].is_revealed) {
printf("该位置已经扫描过了!\n");
return;
}
board[row][col].is_flagged = 0;
}
// 判断是否胜利
int is_win(Grid board[][COLS]) {
int count = 0;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (board[row][col].is_revealed || board[row][col].is_mine) {
count++;
}
}
}
return count == ROWS * COLS;
}
// 主函数
int main() {
srand(time(0));
Grid board[ROWS][COLS];
init_board(board);
generate_mines(board);
update_neighbor_mine_count(board);
display_board(board, 0);
while (1) {
int row, col;
char action[10];
printf("请输入行和列,以及操作(r表示扫描,f表示插旗,u表示撤销插旗):");
scanf("%d%d%s", &row, &col, action);
if (!is_out_of_bounds(row, col)) {
if (action[0] == 'r') {
reveal(board, row, col);
display_board(board, 0);
if (is_win(board)) {
printf("你胜利了!\n");
display_board(board, 1);
break;
}
} else if (action[0] == 'f') {
flag(board, row, col);
display_board(board, 0);
} else if (action[0] == 'u') {
unflag(board, row, col);
display_board(board, 0);
} else {
printf("无效操作!\n");
}
} else {
printf("越界!\n");
}
}
return 0;
}
```
这个程序实现了一个简单的扫雷游戏,包括初始化棋盘、随机生成地雷位置、更新邻居雷数、显示棋盘、扫描、插旗、撤销插旗、判断是否胜利等功能。你可以根据自己的需求进行修改和扩展。
阅读全文