" 以下是基于easyX图形库的C语言扫雷源代码。这个游戏实现了基本的扫雷功能,包括显示地雷、玩家点击格子、判断输赢等。请注意,这个代码可能需要根据您的系统配置和easyX图形库的版本进行相应的调整。
```c
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define COLS 10
#define MINES 10
void gotoxy(int x, int y)
{
COORD pos = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void draw_board(int board[][COLS], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (board[i][j] == 0)
{
printf(".");
}
else if (board[i][j] == 1)
{
printf("M");
}
else
{
printf("%d", board[i][j]);
}
}
printf("\n");
}
}
void reveal(int board[][COLS], int rows, int cols, int x, int y)
{
if (x < 0 || x >= rows || y < 0 || y >= cols || board[x][y] != 0)
{
return;
}
if (board[x][y] == 1)
{
printf("玩家踩到了地雷!游戏结束!\n");
exit(0);
}
int count = 0;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
int nx = x + i;
int ny = y + j;
if (nx < 0 || nx >= rows || ny < 0 || ny >= cols)
{
continue;
}
if (board[nx][ny] == 1)
{
count++;
}
}
}
if (count == 0)
{
board[x][y] = 0;
reveal(board, rows, cols, x + 1, y);
reveal(board, rows, cols, x - 1, y);
reveal(board, rows, cols, x, y + 1);
reveal(board, rows, cols, x, y - 1);
}
else
{
board[x][y] = count;
}
}
int main()
{
srand(time(NULL));
int board[ROWS][COLS] = {0};
int rows = ROWS;
int cols = COLS;
int mines = MINES;
for (int i = 0; i < mines; i++)
{
int x = rand() % rows;
int y = rand() % cols;
while (board[x][y] == 1)
{
x = rand() % rows;
y = rand() % cols;
}
board[x][y] = 1;
}
while (1)
{
system("cls");
draw_board(board, rows, cols);
int x, y;
printf("请输入要打开的格子的坐标(行 列):");
scanf("%d %d", &x, &y);
reveal(board, rows, cols, x - 1, y - 1);
if (board[0][0] == 0)
{
printf("恭喜玩家获胜!\n");
break;
}
if (_kbhit())
{
char c = _getch();
if (c == 'q' || c"