较为复杂的扫雷小游戏C语言
时间: 2023-07-24 17:16:35 浏览: 99
以下是一个较为复杂的扫雷小游戏的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 10
#define COL 10
#define MINE_NUM 10
#define EASY 0
#define NORMAL 1
#define HARD 2
void game();
void init(char board[][COL], char show_board[][COL], int row, int col);
void set_mine(char board[][COL], int row, int col, int num);
void print_board(char board[][COL], char show_board[][COL], int row, int col);
int get_mine_count(char board[][COL], int row, int col, int x, int y);
void expand_board(char board[][COL], char show_board[][COL], int row, int col, int x, int y);
void play_game(char board[][COL], char show_board[][COL], int row, int col, int num);
int check_win(char board[][COL], char show_board[][COL], int row, int col, int num);
void set_difficulty(int *row, int *col, int *num);
void game_info();
int check_input(char input[]);
int main()
{
game();
return 0;
}
void game()
{
char board[ROW][COL];
char show_board[ROW][COL];
int num = MINE_NUM;
int row = ROW;
int col = COL;
int diff = EASY;
int option;
char input[10];
srand((unsigned int)time(NULL));
game_info();
while (1)
{
printf("请选择难度(0-简单,1-普通,2-困难):");
fgets(input, 10, stdin);
if (check_input(input))
{
diff = atoi(input);
break;
}
else
{
printf("输入有误,请重新输入!\n");
}
}
set_difficulty(&row, &col, &num);
init(board, show_board, row, col);
set_mine(board, row, col, num);
print_board(board, show_board, row, col);
play_game(board, show_board, row, col, num);
}
void init(char board[][COL], char show_board[][COL], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = 0;
show_board[i][j] = '?';
}
}
}
void set_mine(char board[][COL], int row, int col, int num)
{
int i, j;
int x, y;
int count = 0;
while (count < num)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == 0)
{
board[x][y] = -1;
count++;
}
}
}
void print_board(char board[][COL], char show_board[][COL], int row, int col)
{
int i, j;
printf(" ");
for (i = 0; i < col; i++)
{
printf("%d ", i + 1);
}
printf("\n");
for (i = 0; i < row; i++)
{
printf("%d ", i + 1);
for (j = 0; j < col; j++)
{
if (show_board[i][j] == '!')
{
printf("%c ", show_board[i][j]);
}
else if (show_board[i][j] == '?')
{
printf("%c ", show_board[i][j]);
}
else
{
printf("%d ", show_board[i][j]);
}
}
printf("\n");
}
}
int get_mine_count(char board[][COL], int row, int col, int x, int y)
{
int count = 0;
int i, j;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (i >= 0 && i < row && j >= 0 && j < col && board[i][j] == -1)
{
count++;
}
}
}
return count;
}
void expand_board(char board[][COL], char show_board[][COL], int row, int col, int x, int y)
{
if (show_board[x][y] == '!')
{
return;
}
int count = get_mine_count(board, row, col, x, y);
if (count == 0)
{
show_board[x][y] = ' ';
int i, j;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (i >= 0 && i < row && j >= 0 && j < col && show_board[i][j] == '?')
{
expand_board(board, show_board, row, col, i, j);
}
}
}
}
else
{
show_board[x][y] = count + '0';
}
}
void play_game(char board[][COL], char show_board[][COL], int row, int col, int num)
{
char input[10];
int x, y;
int win = 0;
while (1)
{
printf("请输入要排查的坐标,格式为x,y:");
fgets(input, 10, stdin);
if (check_input(input))
{
x = atoi(&input[0]) - 1;
y = atoi(&input[2]) - 1;
if (x >= 0 && x < row && y >= 0 && y < col)
{
if (board[x][y] == -1)
{
printf("你踩到地雷了,游戏结束!\n");
show_board[x][y] = '!';
print_board(board, show_board, row, col);
break;
}
else if (show_board[x][y] == '?')
{
expand_board(board, show_board, row, col, x, y);
print_board(board, show_board, row, col);
win = check_win(board, show_board, row, col, num);
if (win)
{
printf("恭喜你,排雷成功!\n");
print_board(board, show_board, row, col);
break;
}
}
else
{
printf("该位置已排查,请重新输入!\n");
}
}
else
{
printf("输入有误,请重新输入!\n");
}
}
else
{
printf("输入有误,请重新输入!\n");
}
}
}
int check_win(char board[][COL], char show_board[][COL], int row, int col, int num)
{
int i, j;
int count = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (show_board[i][j] == '?' && board[i][j] != -1)
{
return 0;
}
if (show_board[i][j] == '!')
{
count++;
}
}
}
if (count == num)
{
return 1;
}
return 0;
}
void set_difficulty(int *row, int *col, int *num)
{
if (*row == 10)
{
*row = 15;
*col = 15;
*num = 30;
}
else if (*row == 15)
{
*row = 20;
*col = 20;
*num = 50;
}
}
void game_info()
{
printf("***************************************\n");
printf("*********** 扫雷小游戏 ***********\n");
printf("*********** 1. 输入坐标 ***********\n");
printf("*********** 2. 检查地雷 ***********\n");
printf("*********** 3. 标记地雷 ***********\n");
printf("*********** 4. 退出游戏 ***********\n");
printf("*********** 5. 选择难度 ***********\n");
printf("***************************************\n");
}
int check_input(char input[])
{
if (input[0] >= '0' && input[0] <= '9' &&
input[1] == ',' &&
input[2] >= '0' && input[2] <= '9' &&
input[3] == '\n' && input[4] == '\0')
{
return 1;
}
return 0;
}
```
该代码实现了一个相对复杂的扫雷小游戏,具有基本的游戏功能和难度选择,同时还添加了对输入的检查和对游戏结果的判断。在游戏中,玩家需要输入坐标来排查雷,如果排查的是地雷,则游戏结束;如果排查的不是地雷,则会显示周围地雷的数量,如果周围没有地雷,则自动扩展周围的格子。玩家可以标记已经排查过的地雷,以免重复排查。游戏胜利的条件是所有地雷都被排查,并且所有格子都被扩展。
阅读全文