使用C语言写一个扫雷游戏
时间: 2023-12-20 17:04:37 浏览: 77
好的,以下是一个简单的C语言扫雷游戏的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define COLS 10
#define MINES 10
void print_board(char board[ROWS][COLS], int reveal_all);
int count_adjacent_mines(char board[ROWS][COLS], int row, int col);
void reveal_board(char board[ROWS][COLS], char revealed[ROWS][COLS], int row, int col);
int main() {
char board[ROWS][COLS];
char revealed[ROWS][COLS];
int i, j, row, col, mines_left = MINES, game_over = 0;
// Initialize board and revealed arrays to all ' '
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
board[i][j] = ' ';
revealed[i][j] = ' ';
}
}
// Place mines randomly on the board
srand(time(NULL));
for (i = 0; i < MINES; i++) {
do {
row = rand() % ROWS;
col = rand() % COLS;
} while (board[row][col] == '*');
board[row][col] = '*';
}
printf("Welcome to Minesweeper!\n");
print_board(revealed, 0);
// Game loop
do {
printf("Mines left: %d\n", mines_left);
printf("Enter row and column (e.g. 1 2): ");
scanf("%d %d", &row, &col);
row--; // Convert to 0-based indexing
col--;
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
printf("Invalid row or column.\n");
} else if (revealed[row][col] != ' ') {
printf("Already revealed.\n");
} else if (board[row][col] == '*') {
printf("Game over!\n");
game_over = 1;
reveal_board(board, revealed, row, col);
print_board(revealed, 1);
} else {
int adjacent_mines = count_adjacent_mines(board, row, col);
if (adjacent_mines > 0) {
revealed[row][col] = '0' + adjacent_mines;
} else {
reveal_board(board, revealed, row, col);
}
print_board(revealed, 0);
if (--mines_left == 0) {
printf("You win!\n");
game_over = 1;
}
}
} while (!game_over);
return 0;
}
// Print the game board
void print_board(char board[ROWS][COLS], int reveal_all) {
int i, j;
printf(" ");
for (j = 0; j < COLS; j++) {
printf("%d ", j + 1);
}
printf("\n");
for (i = 0; i < ROWS; i++) {
printf("%d ", i + 1);
for (j = 0; j < COLS; j++) {
if (board[i][j] == '*' && reveal_all) {
printf("* ");
} else {
printf("%c ", board[i][j]);
}
}
printf("\n");
}
}
// Count the number of adjacent mines
int count_adjacent_mines(char board[ROWS][COLS], int row, int col) {
int i, j, count = 0;
for (i = row - 1; i <= row + 1; i++) {
for (j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROWS && j >= 0 && j < COLS && board[i][j] == '*') {
count++;
}
}
}
return count;
}
// Recursively reveal the board
void reveal_board(char board[ROWS][COLS], char revealed[ROWS][COLS], int row, int col) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS || revealed[row][col] != ' ') {
return;
}
int adjacent_mines = count_adjacent_mines(board, row, col);
if (adjacent_mines > 0) {
revealed[row][col] = '0' + adjacent_mines;
} else {
revealed[row][col] = '-';
reveal_board(board, revealed, row - 1, col - 1);
reveal_board(board, revealed, row - 1, col);
reveal_board(board, revealed, row - 1, col + 1);
reveal_board(board, revealed, row, col - 1);
reveal_board(board, revealed, row, col + 1);
reveal_board(board, revealed, row + 1, col - 1);
reveal_board(board, revealed, row + 1, col);
reveal_board(board, revealed, row + 1, col + 1);
}
}
```
这个扫雷游戏是一个简单的控制台应用程序。它使用一个二维字符数组来表示游戏板,其中空格表示未揭示的方块,'*'表示地雷,数字表示相邻方块中的地雷数,'-'表示已揭示但没有地雷的方块。游戏使用递归算法来揭示相邻的方块,直到所有没有地雷的方块都被揭示出来为止。
阅读全文