c语言扫雷游戏双击操作代码
时间: 2023-10-19 13:24:31 浏览: 135
以下是一个简单的 C 语言扫雷游戏双击操作的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 9
#define COLS 9
#define EASY_COUNT 10
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col, int count);
int GetMineCount(char mine[ROWS][COLS], int x, int y);
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
int IsWin(char show[ROWS][COLS], int row, int col, int count);
int main()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
int row = ROWS, col = COLS;
int count = EASY_COUNT;
int x, y;
int flag = 0;
srand((unsigned int)time(NULL));
InitBoard(show, ROWS, COLS, '*');
InitBoard(mine, ROWS, COLS, '0');
SetMine(mine, ROWS, COLS, count);
DisplayBoard(show, ROWS, COLS);
while (1)
{
printf("请输入坐标:>");
scanf("%d%d", &x, &y);
if (x < 1 || x > row || y < 1 || y > col)
{
printf("坐标非法,请重新输入!\n");
continue;
}
if (flag == 0)
{
if (mine[x - 1][y - 1] == '1')
{
SetMine(mine, ROWS, COLS, count);
flag = 1;
}
}
if (show[x - 1][y - 1] != '*')
{
printf("该位置已经打开,请重新输入!\n");
continue;
}
if (mine[x - 1][y - 1] == '1')
{
DisplayBoard(mine, ROWS, COLS);
printf("很遗憾,您踩雷了!\n");
break;
}
int mine_count = GetMineCount(mine, x - 1, y - 1);
if (mine_count == 0)
{
Expand(mine, show, x - 1, y - 1);
}
else
{
show[x - 1][y - 1] = mine_count + '0';
}
DisplayBoard(show, ROWS, COLS);
if (IsWin(show, ROWS, COLS, count))
{
printf("恭喜您,游戏胜利!\n");
break;
}
}
return 0;
}
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf(" ");
for (i = 1; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 0; i < row; i++)
{
int j = 0;
printf("%2d ", i + 1);
for (j = 0; j < col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col, int count)
{
while (count)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int count = 0;
int i, j;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (i >= 0 && i < ROWS && j >= 0 && j < COLS && mine[i][j] == '1')
{
count++;
}
}
}
return count;
}
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
if (x < 0 || x >= ROWS || y < 0 || y >= COLS)
{
return;
}
if (show[x][y] != '*')
{
return;
}
int mine_count = GetMineCount(mine, x, y);
if (mine_count > 0)
{
show[x][y] = mine_count + '0';
}
else
{
show[x][y] = ' ';
Expand(mine, show, x - 1, y);
Expand(mine, show, x + 1, y);
Expand(mine, show, x, y - 1);
Expand(mine, show, x, y + 1);
Expand(mine, show, x - 1, y - 1);
Expand(mine, show, x - 1, y + 1);
Expand(mine, show, x + 1, y - 1);
Expand(mine, show, x + 1, y + 1);
}
}
int IsWin(char show[ROWS][COLS], int row, int col, int count)
{
int i, j;
int num = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (show[i][j] == '*')
{
num++;
}
}
}
return num == count;
}
```
在该代码中,`Expand` 函数是实现双击操作的核心,它会递归地展开周围的方块,直到遇到有数字的方块或者雷。当玩家双击一个空格时,该函数会自动展开周围的方块,直到遇到有数字的方块或者雷。
阅读全文