算法思路
(1)布雷数组mine设置大小要小于排雷数组show大小防止溢出
(2)初始化两个数组mine全置 '0',show全置 '*'
(3)打印数组,每次排雷都需要show数组打印一次
(4)布置雷,使用time通过时间生成两个数,构成坐标,将mine中相应的坐标置‘1’,
设置 EASY_COUNT 个雷
(5)排雷算法
数组show根据mine相应位置计算出周围‘1’个数count
返回字符数字show[x][y] = count + '0'
'1' - 48 = '1' - '0' = 1
count = mine[x-1][y] + mine[x-1][y-1]
+ mine[x][y-1] + mine[x+1][y]
+ mine[x+1][y+1] + mine[x+1][y-1]
+ mine[x][y+1] + mine[x-1][y+1] - 8*'0'
(6)输赢判断
输入坐标的可能有show[x][y] = ' ' || '*' || '#' (mine[x][y] = '0' || '1')
在输入坐标过程随时可以进行切换到标记雷,只需要输入Q但每次只能标记一个雷点
雷点标记为show[x][y] = '#'
两个局部变量 win + search = ROW * COL
win = row*col - EASY_COUNT + 8
search = EASY_COUNT
两种都可胜利!!!!
>>test.c
#include "game.h"
/*
1、布雷(一个数组存放)
2、排雷(另一个数值存放)
*/
void menu(){
printf("************************************\n");
printf("******* 扫雷 ***********\n");
printf("******* 1.play ***********\n");
printf("******* 0.exit ***********\n");
printf("*****输入Q可从排雷进入标记雷点******\n");
printf("************************************\n");
}
void game(){
//printf("扫雷\n");
//布置雷
char mine[ROWS][COLS] = {0};
//排查雷
char show[ROWS][COLS]= {0};
//初始化
InitBoard(mine,ROWS,COLS,'0');
InitBoard(show,ROWS,COLS,'*');
//打印
//DisplayBoard(mine,ROW,COL);
//布置雷
SetMine(mine,ROW,COL);
//DisplayBoard(mine,ROW,COL);
//扫雷
DisplayBoard(show,ROW,COL);
FindMine(mine,show,ROW,COL);
}
void test(){
int input = 0;
srand((unsigned int)time(NULL));
do{
menu();
printf("请输入您的选择:>");
scanf("%d",&input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("输入格式错误,请重新输入!\n");
break;
}
}while(input);
}
int main(){
test();
return 0;
}
>>game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void test();
void game();
void menu();
void searchMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);
void InitBoard(char board[ROWS][COLS],int rows,int cols,char set);
void DisplayBoard(char board[ROWS][COLS],int row,int col);
void SetMine(char board[ROWS][COLS],int row,int col);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);
int get_mine_count(char mine[ROWS][COLS],int x,int y);
void ExpandBoard(char mine[ROWS][COLS], char show[ROWS][COLS],int x, int y,int *win);
>>game.c
#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;
for(i = 0;i<=col;i++){
printf("%d ",i);
}
printf("\n");
for(i = 1;i <= row;i++){
printf("%d ",i);
for(j = 1;j <= col;j++){
printf("%c ",board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS],int row,int col){
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if(board[x][y] == '0'){
board[x][y] = '1';
count--;
}
}
}
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col){
int x = 0;
int y = 0;
int win = 0;
while(win < (row*col - EASY_COUNT + 8)){
if(getchar() == 'Q'){
searchMine(mine,show,row,col);
}
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] == '*' || show[x][y] == '#'){//计算xy坐标周围有几个雷
int count = get_mine_count(mine,x,y);
if(count == 0)
ExpandBoard(mine,show,x,y,&win);
show[x][y] = count + '0';
system("cls");
Sleep(10);
DisplayBoard(show,row,col);
win++;
}
else if(show[x][y] == ' '){
system("cls");
show[x][y] = '0';
DisplayBoard(show,row,col);
}
// else{
// printf("已排除!\n");
// }
}
else{
printf("输入坐标非法,请重新输入\n");
}
}
if(win == (row*col - EASY_COUNT + 8))
{
printf("恭喜你排雷成功!\n");
DisplayBoard(mine,row,col);
}
}
// '1' -'0' = 1
// '3' -'0' = 3
int get_mine_count(char mine[ROWS][COLS],int x,int y){
return(mine[x-1][y] + mine[x-1][y-1] + mine[x][y-1] + mine[x+1][y] + mine[x+1][y+1] + mine[x+1][y-1] + mine[x][y+1] + mine[x-1][y+1] - 8*'0');
}
//坐标不是雷
//该坐标周围也没有雷
void ExpandBoard(char mine[ROWS][COLS], char show[ROWS][COLS],int x, int y,int *win)
{
int count = get_mine_count(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';//没有雷的坐标赋值为空格
(*win)++;
//递归周围的八个格子
if (show[x - 1][y - 1] == '*' && x - 1 > 0 && x - 1 < ROWS && y - 1 > 0 && y - 1 < COLS && mine[x-1][y-1] != '1')
ExpandBoard(mine, show, x - 1, y - 1,win);
if (show[x - 1][y] == '*' && x - 1 > 0 && x - 1 < ROWS && y > 0 && y < COLS && mine[x-1][y] != '1')
ExpandBoard(mine, show, x - 1, y,win);
if (show[x - 1][y + 1] == '*' && x - 1 > 0 && x - 1 < ROWS && y + 1 > 0 && y + 1 < COLS && mine[x -1][y+1] != '1')
ExpandBoard(mine, show, x - 1, y + 1,win);
if (show[x][y - 1] == '*' && x > 0 && x < ROWS && y - 1 > 0 && y - 1 < COLS && mine[x][y-1] != '1')
ExpandBoard(mine, show, x, y - 1,win);
if (show[x][y + 1] == '*' && x > 0 && x < ROWS && y + 1 > 0 && y + 1 < COLS && mine[x][y+1] != '1' )
ExpandBoard(mine, show, x, y + 1,win);
if (show[x + 1][y - 1] == '*' && x + 1 > 0 && x + 1 < ROWS && y - 1 > 0 && y - 1 < COLS && mine[x+1][y-1] != '1')
ExpandBoard(mine, show, x + 1, y - 1,win);
if (show[x + 1][y] == '*' && x + 1 > 0 && x + 1 < ROWS && y > 0 && y < COLS && mine[x+1][y] != '1')
ExpandBoard(mine, show, x + 1, y,win);
if (show[x + 1][y + 1] == '*' && x + 1 > 0 && x + 1 < ROWS && y + 1 > 0 && y + 1 < COLS && mine[x+1][y+1] != '1')
ExpandBoard(mine, show, x + 1, y + 1,win);
}
}
void searchMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col){
int x = 0;
int y = 0;
static int search = 0;
printf("请输入标记雷的坐标:>");
scanf("%d%d",&x,&y);
if(x >= 1 && x <= row && y >= 1 && y <= col)
{
if(show[x][y] == '*'){
show[x][y] = '#';
if(mine[x][y] == '1') search++;
system("cls");
DisplayBoard(show,row,col);
}
else{
printf("已排除,不可在标记为雷点!\n");
}
}
else{
printf("输入坐标非法,请重新输入\n");
}
if(search == EASY_COUNT)
{
printf("恭喜你排雷成功!\n");
DisplayBoard(mine,row,col);
return;
}
}
运行结果: