linux简易扫雷c语言代码,C语言实现简易版扫雷的完整过程
时间: 2023-12-03 22:29:08 浏览: 55
扫雷C语言代码
5星 · 资源好评率100%
下面是一个简单的 Linux 下扫雷游戏的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MINE -1
#define UNOPENED 9
#define FLAG 10
int ROW, COL, MINES, UNOPENED_COUNT;
int **board;
int **visible_board;
void print_board() {
printf("\n");
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (visible_board[i][j] == UNOPENED)
printf(" * ");
else if (visible_board[i][j] == MINE)
printf(" X ");
else if (visible_board[i][j] == FLAG)
printf(" F ");
else
printf(" %d ", visible_board[i][j]);
}
printf("\n");
}
}
void place_mines() {
int count = 0;
while (count < MINES) {
int x = rand() % ROW;
int y = rand() % COL;
if (board[x][y] != MINE) {
board[x][y] = MINE;
count++;
}
}
}
int count_adjacent_mines(int row, int col) {
int count = 0;
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROW && j >= 0 && j < COL && board[i][j] == MINE)
count++;
}
}
return count;
}
void update_board(int row, int col) {
if (board[row][col] == MINE) {
printf("\nYou stepped on a mine! Game over.\n");
print_board();
exit(0);
} else {
visible_board[row][col] = count_adjacent_mines(row, col);
UNOPENED_COUNT--;
if (UNOPENED_COUNT == MINES) {
printf("\nCongratulations, you won!\n");
print_board();
exit(0);
}
if (visible_board[row][col] == 0) {
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROW && j >= 0 && j < COL && visible_board[i][j] == UNOPENED)
update_board(i, j);
}
}
}
}
}
void place_flag(int row, int col) {
if (visible_board[row][col] == UNOPENED)
visible_board[row][col] = FLAG;
else if (visible_board[row][col] == FLAG)
visible_board[row][col] = UNOPENED;
else
printf("\nInvalid move, try again.\n");
}
int main() {
srand(time(NULL));
printf("Enter the number of rows, columns and mines separated by spaces: ");
scanf("%d %d %d", &ROW, &COL, &MINES);
board = (int**)malloc(ROW * sizeof(int*));
visible_board = (int**)malloc(ROW * sizeof(int*));
for (int i = 0; i < ROW; i++) {
board[i] = (int*)malloc(COL * sizeof(int));
visible_board[i] = (int*)malloc(COL * sizeof(int));
for (int j = 0; j < COL; j++) {
board[i][j] = 0;
visible_board[i][j] = UNOPENED;
}
}
UNOPENED_COUNT = ROW * COL;
place_mines();
while (1) {
print_board();
printf("\nEnter the row and column of the cell you want to open or flag (separated by a space), or enter -1 to quit: ");
int row, col;
scanf("%d", &row);
if (row == -1)
break;
scanf("%d", &col);
if (row < 0 || row >= ROW || col < 0 || col >= COL)
printf("\nInvalid move, try again.\n");
else {
printf("\nEnter 1 to open the cell or 2 to place or remove a flag: ");
int action;
scanf("%d", &action);
if (action == 1)
update_board(row, col);
else if (action == 2)
place_flag(row, col);
else
printf("\nInvalid move, try again.\n");
}
}
for (int i = 0; i < ROW; i++) {
free(board[i]);
free(visible_board[i]);
}
free(board);
free(visible_board);
return 0;
}
```
这个程序使用了动态内存分配来创建扫雷游戏的主要和可见的板。使用随机数来放置地雷并计算相邻地雷的数量。游戏通过递归更新相邻的空单元格来进行。玩家可以标记地雷,以便稍后参考。程序在控制台中运行,需要用户输入行、列和地雷的数量。
阅读全文