C语言之扫雷
参考
https://blog.csdn.net/m0_62391199/article/details/124694375
game.h
c
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//初始化雷区
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col); //只用9*9,但还是要用完整board
//设置雷
void SetMine(char board[ROWS][COLS], int row, int col);
//查找雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
game.c
c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set; //表示要初始化的字符
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("------------------扫雷游戏-------------------\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("------------------游戏结束-------------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
//x = 1~9
//y = 1~9
while (count)
{
int x = rand() % row + 1; //rand() % 9 = 0~8
int y = rand() % col + 1;
//判断该坐标是否已经布置过雷,避免重复不知
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
//获取坐标周围的雷的个数
static int get_mine_count(char board[ROWS][COLS], int x, int y)
{
return (board[x - 1][y - 1] +
board[x - 1][y] +
board[x - 1][y + 1] +
board[x][y - 1] +
board[x][y + 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] - 8 * '0');
}
//int get_mine_count(char board[ROWS][COLS], int x, int y)
//{
// int i = 0;
// int j = 0;
// int count = 0;
// for (i = x - 1; i <= x + 1; i++)
// {
// for(j = y - 1; j <= y + 1;j++)
// {
// if (board[x][y] == '1')
// {
// count++;
// }
// }
// }
// return count;
//}
void ExplosionSpread(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y, int* pw);
void MarkMine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;//找到非雷的个数,用来标记是否取得胜利
int* pw = &win;
char ch = 0; //用来接收是否需要标记雷
while (win < row * col - EASY_COUNT)
{
printf("请输入要排查的坐标");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) //判断坐标的合法性
{
if (mine[x][y] == '1') //这里就是设置雷数组和排查雷数组的交互
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
if (show[x][y] != '*')
{
printf("该坐标一排查过,不能重复排");
continue; //忘记写,直接进入下一次循环
}
else//如果不是雷
{
ExplosionSpread(mine, show, row, col, x, y, pw); //爆炸展开一片
system("cls");
DisplayBoard(show, row, col);
printf("需要标记雷的位置请输入y/Y,否则请按任意键->");
while ((ch = getchar() != '\n'));
scanf("%c", &ch);
if (ch == 'Y' || ch == 'y')
{
MarkMine(show, row, col); //标记雷
system("cls");
DisplayBoard(show, row, col);
}
else
{
continue; //当玩家选择不标记雷,执行continue,跳过当前循环的剩余部分,直接进入下一次循环,等待玩家输入新的坐标
}
}
}
}
else
{
printf("输入坐标错误,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
system("cls");
printf("恭喜你,排雷成功\n");
DisplayBoard(show, ROW, COL); //不是显示DisplayBoard(mine,ROW,COL);
return;
}
}
//基础功能
//1、标记功能
//2、展开一片
void ExplosionSpread(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y, int *pw) //爆炸展开一片
{
if (x >= 1 && x <= row && y >= 1 && y <= col) //判断坐标是否在排查的范围内
{
int num = get_mine_count(mine, x, y); //获取坐标周围雷的个数
(*pw)++; //增加win的值,因为这是一个非雷区域
if (num == 0) //如果没有雷
{
show[x][y] = ' '; //如果没有雷,就把该坐标设置成空格,并向周围八个坐标展开
int i = 0;
int j = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (show[i][j] == '*') //限制递归条件,防止已经排查过的坐标再次递归,从而造成死递归, 别写成show[i][j] = '*,此时会为死递归
{
ExplosionSpread(mine, show, row,col, i, j, pw);
}
}
}
}
else //如果当前坐标周围有雷,但当前坐标本身是非雷区域
{
show[x][y] = num + '0'; //显示周围有多少个雷
}
}
}
void MarkMine(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请选择你需要标记的坐标\n");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)//判断合法性
{
if (board[x][y] == '*')
{
board[x][y] = '!';
printf("是否需要继续标记?(0: 继续标记, 其他: 结束标记): ");
int if_continue_mark = 1;
while (scanf("%d", &if_continue_mark) != 1) // 输入验证
{
printf("输入无效,请输入数字 (0: 继续标记, 其他: 结束标记): ");
while (getchar() != '\n'); // 清空输入缓冲区
}
if (!if_continue_mark)
{
continue; // 继续标记,进入下一次循环
}
else
{
break; // 无需继续标记,跳出循环
}
/*int if_continue_mark = 1;
printf("0:继续标记 其他:无需标记");
scanf("%d", &if_continue_mark);*/
if (!if_continue_mark)
{
continue;
}
else
{
break;
}
}
else if (board[x][y] == '!')
{
board[x][y] = '*';
printf("已取消标记,是否需要继续标记?(0: 继续标记, 其他: 结束标记): ");
int if_continue_mark = 1;
while (scanf("%d", &if_continue_mark) != 1) // 输入验证
{
printf("输入无效,请输入数字 (0: 继续标记, 其他: 结束标记): ");
while (getchar() != '\n'); // 清空输入缓冲区
}
if (!if_continue_mark)
{
continue; // 继续标记,进入下一次循环
}
else
{
break; // 无需继续标记,跳出循环
}
}
else
{
printf("该坐标不能被标记,请重新输入!\n");
}
}
else
{
printf("坐标输入错误,请重新输入");
}
}
}
test.c
c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
printf("*********************************\n");
printf("************* 1. play ***********\n");
printf("************* 0. exit ***********\n");
printf("*********************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 }; //存放布置好的雷的信息
char show[ROWS][COLS] = { 0 }; //存放排查出的雷的信息
//初始化数组的内容为指定的内容
//mine 数组在没有布置雷的情况下 都是'0'
InitBoard(mine, ROWS, COLS, '0');
//show 数组在没有排查雷的时候,都是'*'
InitBoard(show, ROWS, COLS, '*');
//设置雷
SetMine(mine, ROW, COL);
//打印
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
//设置随机数的生成起点
srand((unsigned int)time(NULL)); //用于随机生成坐标的rand函数的种子srand函数只需要在main函数中声明一次
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
//玩游戏
//printf("扫雷\n");
game();
break;
case 0:
//退出
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择");
break;
}
} while (input);
return 0;
}