用C语言写一个扫雷程序
时间: 2023-12-04 18:03:28 浏览: 39
好的,我知道了。下面是一个简单的扫雷程序的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 8 // 行数
#define COLS 8 // 列数
#define MINES 10 // 地雷数
void initBoard(int board[][COLS], int rows); // 初始化游戏面板
void printBoard(int board[][COLS], int rows); // 打印游戏面板
void layMines(int board[][COLS], int rows, int mines); // 布雷
void updateBoard(int board[][COLS], int row, int col); // 更新游戏面板
int playGame(int board[][COLS], int rows, int mines); // 开始游戏
int main()
{
int board[ROWS][COLS]; // 游戏面板
int result; // 游戏结果
srand(time(NULL)); // 随机数种子
initBoard(board, ROWS); // 初始化游戏面板
layMines(board, ROWS, MINES); // 布雷
result = playGame(board, ROWS, MINES); // 开始游戏
if (result == 1)
printf("Congratulations! You have won the game!\n");
else
printf("Sorry, you have lost the game!\n");
return 0;
}
// 初始化游戏面板
void initBoard(int board[][COLS], int rows)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < COLS; j++) {
board[i][j] = '*'; // 初始化为 '*'
}
}
}
// 打印游戏面板
void printBoard(int board[][COLS], int rows)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < COLS; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
// 布雷
void layMines(int board[][COLS], int rows, int mines)
{
int count = 0; // 已布雷数
int row, col;
while (count < mines) {
row = rand() % rows;
col = rand() % COLS;
if (board[row][col] != 'M') {
board[row][col] = 'M'; // '*' 表示未翻开,'M' 表示布雷
count++;
}
}
}
// 更新游戏面板
void updateBoard(int board[][COLS], int row, int col)
{
int i, j;
for (i = row-1; i <= row+1; i++) {
for (j = col-1; j <= col+1; j++) {
if (i >= 0 && i < ROWS && j >= 0 && j < COLS) {
if (board[i][j] != 'M')
board[i][j]++;
}
}
}
}
// 开始游戏
int playGame(int board[][COLS], int rows, int mines)
{
int i, j;
int count = 0; // 已翻开方块数
int x, y;
char action;
while (count < ROWS*COLS-mines) {
printf("Please input the row, col and action ('F' for flag, 'O' for open): ");
scanf("%d%d %c", &x, &y, &action);
x--;
y--;
if (action == 'F') { // 标旗
board[x][y] = 'F';
printBoard(board, ROWS);
}
else if (action == 'O') { // 翻开方块
if (board[x][y] == 'M') { // 踩雷
board[x][y] = 'X';
printBoard(board, ROWS);
return 0;
}
else if (board[x][y] != '*') { // 重复翻开
printf("The square has already been opened!\n");
}
else { // 翻开方块并更新周围数字
updateBoard(board, x, y);
count++;
printBoard(board, ROWS);
}
}
}
return 1;
}
```
希望能对你有帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)