具有难度选择的C语言扫雷游戏
时间: 2023-06-11 09:10:38 浏览: 143
用C语言编写的扫雷游戏
5星 · 资源好评率100%
以下是一个具有难度选择的C语言扫雷游戏的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define COLS 10
#define EASY_MINES 10
#define NORMAL_MINES 30
#define HARD_MINES 50
void init_board(int board[][COLS], int rows, int cols);
void print_board(int board[][COLS], int rows, int cols, int show_mines);
void place_mines(int board[][COLS], int rows, int cols, int num_mines);
void reveal_square(int board[][COLS], int rows, int cols, int row, int col);
int count_adjacent_mines(int board[][COLS], int rows, int cols, int row, int col);
int check_win(int board[][COLS], int rows, int cols, int num_mines);
void play_game(int board[][COLS], int rows, int cols, int num_mines);
int main() {
int board[ROWS][COLS];
int difficulty;
srand(time(NULL)); // seed the random number generator
printf("Welcome to Minesweeper!\n");
printf("Select difficulty:\n");
printf("1. Easy\n");
printf("2. Normal\n");
printf("3. Hard\n");
scanf("%d", &difficulty);
switch(difficulty) {
case 1:
play_game(board, ROWS, COLS, EASY_MINES);
break;
case 2:
play_game(board, ROWS, COLS, NORMAL_MINES);
break;
case 3:
play_game(board, ROWS, COLS, HARD_MINES);
break;
default:
printf("Invalid difficulty level.\n");
break;
}
return 0;
}
void init_board(int board[][COLS], int rows, int cols) {
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
board[i][j] = 0;
}
}
}
void print_board(int board[][COLS], int rows, int cols, int show_mines) {
int i, j;
printf(" ");
for(j = 0; j < cols; j++) {
printf("%d ", j);
}
printf("\n");
for(i = 0; i < rows; i++) {
printf("%d ", i);
for(j = 0; j < cols; j++) {
if(board[i][j] == -1 && show_mines) {
printf("* ");
} else if(board[i][j] < 0) {
printf(" ");
} else {
printf("%d ", board[i][j]);
}
}
printf("\n");
}
}
void place_mines(int board[][COLS], int rows, int cols, int num_mines) {
int i, j, count = 0;
while(count < num_mines) {
i = rand() % rows;
j = rand() % cols;
if(board[i][j] != -1) {
board[i][j] = -1;
count++;
}
}
}
void reveal_square(int board[][COLS], int rows, int cols, int row, int col) {
if(board[row][col] != 0) {
return;
}
int i, j;
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) {
if(board[i][j] == -1) {
continue;
} else if(board[i][j] > 0) {
board[i][j] *= -1;
} else {
board[i][j] = abs(board[i][j]);
reveal_square(board, rows, cols, i, j);
}
}
}
}
}
int count_adjacent_mines(int board[][COLS], int rows, int 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) {
if(board[i][j] == -1) {
count++;
}
}
}
}
return count;
}
int check_win(int board[][COLS], int rows, int cols, int num_mines) {
int i, j, count = 0;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
if(board[i][j] == -1) {
continue;
} else if(board[i][j] < 0) {
count++;
}
}
}
if(count == rows * cols - num_mines) {
return 1;
} else {
return 0;
}
}
void play_game(int board[][COLS], int rows, int cols, int num_mines) {
int i, j, row, col, game_over = 0, win = 0;
init_board(board, rows, cols);
place_mines(board, rows, cols, num_mines);
while(!game_over && !win) {
print_board(board, rows, cols, 0);
printf("Enter row and column to reveal: ");
scanf("%d %d", &row, &col);
if(board[row][col] == -1) {
game_over = 1;
board[row][col] *= -1;
} else if(board[row][col] == 0) {
board[row][col] = abs(board[row][col]);
reveal_square(board, rows, cols, row, col);
} else {
board[row][col] = abs(board[row][col]);
}
win = check_win(board, rows, cols, num_mines);
}
print_board(board, rows, cols, 1);
if(game_over) {
printf("Game over! You hit a mine.\n");
} else if(win) {
printf("Congratulations! You won the game.\n");
}
}
```
该游戏使用二维数组来表示游戏板,-1表示地雷,0表示空方块,1-8表示周围8个方块中地雷的数量。`init_board`函数初始化游戏板,`print_board`函数打印游戏板,`place_mines`函数随机放置地雷。`reveal_square`函数用于递归展开周围的空方块,`count_adjacent_mines`函数用于计算周围地雷的数量,`check_win`函数用于检查是否获胜。`play_game`函数是主要的游戏循环,循环结束条件是踩到地雷或者获胜。在游戏结束后,调用`print_board`函数以显示所有地雷的位置。
阅读全文