生成一个扫雷代码,使用c语言
时间: 2023-06-09 10:04:05 浏览: 86
好的,我可以为您生成一个扫雷代码,使用C语言编写。以下是代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define COLS 10
#define MINES 10
#define COVER '*'
#define UNCOVERED ' '
#define MINE 'X'
void setupBoard(char board[][COLS], int rows, int cols);
void populateMines(char board[][COLS], int rows, int cols, int num_mines);
void printBoard(char board[][COLS], int rows, int cols);
int getAdjacentMines(char board[][COLS], int row, int col);
void revealSafeSpaces(char board[][COLS], int row, int col);
int getBoardState(char board[][COLS], int rows, int cols);
int main() {
char board[ROWS][COLS];
int row, col, mines_left = MINES, board_state;
setupBoard(board, ROWS, COLS);
populateMines(board, ROWS, COLS, MINES);
printf("Welcome to Minesweeper!\n");
do {
printf("There are %d mines left.\n", mines_left);
printBoard(board, ROWS, COLS);
printf("Enter a row and column to uncover (e.g. 2 3): ");
scanf("%d %d", &row, &col);
if (board[row][col] == MINE) {
printf("You hit a mine! Game over.\n");
board[row][col] = '*';
printBoard(board, ROWS, COLS);
break;
} else {
revealSafeSpaces(board, row, col);
}
board_state = getBoardState(board, ROWS, COLS);
if (board_state == 1) {
printf("Congratulations, you win!\n");
printBoard(board, ROWS, COLS);
break;
} else if (board_state == -1) {
printf("You hit a mine! Game over.\n");
printBoard(board, ROWS, COLS);
break;
}
mines_left = MINES;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == MINE) {
mines_left--;
}
}
}
} while (1);
return 0;
}
void setupBoard(char board[][COLS], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
board[i][j] = COVER;
}
}
}
void populateMines(char board[][COLS], int rows, int cols, int num_mines) {
int row, col;
srand(time(NULL));
for (int i = 0; i < num_mines; i++) {
row = rand() % rows;
col = rand() % cols;
if (board[row][col] == MINE) {
i--;
} else {
board[row][col] = MINE;
}
}
}
void printBoard(char board[][COLS], int rows, int cols) {
printf("\n");
printf(" ");
for (int i = 0; i < cols; i++) {
printf("%d ", i);
}
printf("\n");
for (int i = 0; i < rows; i++) {
printf("%d ", i);
for (int j = 0; j < cols; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
int getAdjacentMines(char board[][COLS], 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 < ROWS && j >= 0 && j < COLS && board[i][j] == MINE) {
count++;
}
}
}
return count;
}
void revealSafeSpaces(char board[][COLS], int row, int col) {
if (board[row][col] == UNCOVERED || board[row][col] == MINE) {
return;
}
int num_adjacent_mines = getAdjacentMines(board, row, col);
board[row][col] = num_adjacent_mines + '0';
if (num_adjacent_mines == 0) {
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROWS && j >= 0 && j < COLS) {
revealSafeSpaces(board, i, j);
}
}
}
}
}
int getBoardState(char board[][COLS], int rows, int cols) {
int num_uncovered = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == UNCOVERED) {
num_uncovered++;
}
}
}
if (num_uncovered == rows * cols - MINES) {
return 1;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == MINE && num_uncovered > 0) {
return 0;
} else if (board[i][j] == MINE && num_uncovered == 0) {
return -1;
}
}
}
return 0;
}
```
这是一个简单的扫雷游戏的模板。在这个模板中,您可以设置游戏界面的行数和列数,还可以设置地雷的数量。生成扫雷代码后,您可以根据自己的需要轻松修改配置,以生成您所需的扫雷游戏。
阅读全文