程序比较简单,需要输入坐标x,y进行翻牌。当时现场演示时不到50分钟边讲边写的,实现了基本的扫雷功能,只是没有计分计时排名之类的辅助功能。由于程序较简单,也没必要再加工了。另外本程序的特点是第一次输入一定不会是雷。此外,如果翻牌后周围没有雷,即为0,将会搜索周围所以为0的牌。此处用到了dfs。仅供参考。
项目版:
mine.h
#pragma once
#define BOOM 10
#define SIZE 10
void init(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int a, int b);//初始化扫雷页面
void dfs(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int i, int j);//如果展开面为0,通过深搜将周围所以为0的区域展开
int check(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int x, int y);//判断胜利条件
void show(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int x, int y);//展示扫雷页面
mine.cpp
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"mine.h"
int stemp[8][2] = { { 0,1 },{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,-1 },{ 1,-1 },{ -1,1 } };//深搜时上下左右四个方向搜索
void init(char (*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int a, int b) {
int x, y;
visit[a][b] = 0;
for (int i = 0; i < 10; i++) {
do {
x = rand() % SIZE + 1;
y = rand() % SIZE + 1;
} while (visit[x][y] == 0);
if (visit[x][y] == -1) {
visit[x][y] == 0;
MAP[x][y] = '*';
}
}
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
if (MAP[i][j] != '*') {
MAP[i][j] = '0';
for (int k = 0; k < 8; k++) {
if (MAP[i + stemp[k][0]][j + stemp[k][1]] == '*')
MAP[i][j]++;
}
}
}
}
}
void dfs(char(*MAP)