c语言扫雷源代码

  • 格式:docx
  • 大小:37.54 KB
  • 文档页数:6

c语言扫雷源代码

扫雷游戏是一种非常经典的单人益智游戏,玩家需要根据数字提示找出地雷的位置。下面给出一个简单的C语言扫雷游戏的源代码示例:

```c

#include

#include

#include

#define SIZE 10 // 扫雷游戏的大小

#define MINE_COUNT 10 // 地雷的数量

typedef struct {

int x;

int y;

} Position;

typedef struct {

int isMine; // 是否是地雷

int isFlagged; // 是否被标记为地雷

int isRevealed; // 是否已被翻开

int adjacentMines; // 相邻地雷的数量

} Cell;

Cell board[SIZE][SIZE];

void initializeBoard() {

int i, j; // 初始化游戏面板

for (i = 0; i < SIZE; i++) {

for (j = 0; j < SIZE; j++) {

board[i][j].isMine = 0;

board[i][j].isFlagged = 0;

board[i][j].isRevealed = 0;

board[i][j].adjacentMines = 0;

}

}

}

void generateMines() {

int i, j;

int count = 0;

srand(time(NULL)); // 以当前时间作为随机数种子

while (count < MINE_COUNT) {

i = rand() % SIZE;

j = rand() % SIZE;

// 如果该格子已经是地雷,则重新生成随机位置

if (!board[i][j].isMine) {

board[i][j].isMine = 1;

count++;

}

}

}

void calculateAdjacentMines() {

int i, j, k, l;

int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};

int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

for (i = 0; i < SIZE; i++) {

for (j = 0; j < SIZE; j++) {

if (!board[i][j].isMine) {

for (k = 0; k < 8; k++) {

int ni = i + dx[k];

int nj = j + dy[k];

if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE

&& board[ni][nj].isMine) {

board[i][j].adjacentMines++;

}

}

}

}

}

}

void displayBoard() {

int i, j;

printf(" ");

for (i = 0; i < SIZE; i++) {

printf("%d ", i);

}

printf("\n");

for (i = 0; i < SIZE; i++) {

printf("%d ", i);

for (j = 0; j < SIZE; j++) {

if (board[i][j].isRevealed) {

if (board[i][j].isMine) { printf("M ");

} else {

printf("%d ", board[i][j].adjacentMines);

}

} else if (board[i][j].isFlagged) {

printf("F ");

} else {

printf(". ");

}

}

printf("\n");

}

}

void revealAdjacentEmptyCells(Position pos) {

int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};

int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int i;

for (i = 0; i < 8; i++) {

int ni = pos.x + dx[i];

int nj = pos.y + dy[i];

if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE

&& !board[ni][nj].isRevealed && !board[ni][nj].isMine) {

board[ni][nj].isRevealed = 1;

if (board[ni][nj].adjacentMines == 0) {

Position newPos;

newPos.x = ni;

newPos.y = nj;

revealAdjacentEmptyCells(newPos); }

}

}

}

int main() {

Position pos;

char cmd;

int isSuccess = 0;

initializeBoard();

generateMines();

calculateAdjacentMines();

displayBoard();

while (!isSuccess) {

printf("Enter command (R: reveal, F: flag): ");

fflush(stdout);

scanf(" %c", &cmd);

printf("Enter position (x, y): ");

fflush(stdout);

scanf("%d %d", &(pos.x), &(pos.y));

if (cmd == 'R') {

board[pos.x][pos.y].isRevealed = 1;

if (board[pos.x][pos.y].isMine) {

printf("Game Over!\n");

isSuccess = 1;

} else if (board[pos.x][pos.y].adjacentMines == 0) { revealAdjacentEmptyCells(pos);

}

} else if (cmd == 'F') {

board[pos.x][pos.y].isFlagged = 1;

}

displayBoard();

}

return 0;

}

```

上述代码实现了一个简单的C语言扫雷游戏。游戏面板大小为10x10,共有10个地雷。玩家可以输入命令和位置,使用'R'命令翻开对应位置的方块,使用'F'命令将对应位置标记为地雷。游戏会根据玩家的操作,在终端上实时显示游戏进程,直到游戏结束。

上述代码主要使用了数组、结构体和递归等基本的C语言技术实现了扫雷游戏的核心逻辑。玩家可以基于这个代码进行扩展和优化,例如添加计时功能、增加不同难度级别、实现图形界面等等。