程序比较简单,需要输入坐标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
#include
#include
#include
#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)[SIZE + 2], int(*visit)[SIZE + 2], int i, int j) {
visit[i][j] = 1;
for (int k = 0; k < 8; k++) {
if (MAP[i + stemp[k][0]][j + stemp[k][1]] == '0'&&visit[i + stemp[k][0]][j + stemp[k][1]] != 1)
dfs(MAP, visit, i + stemp[k][0], j + stemp[k][1]);
else if (MAP[i + stemp[k][0]][j + stemp[k][1]] != '*'&&visit[i + stemp[k][0]][j + stemp[k][1]] != 1)
visit[i + stemp[k][0]][j + stemp[k][1]] = 1;
}
}
int check(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int x, int y) {
if (MAP[x][y] == '*') {
visit[x][y] = 1;
return 1;
}
else if (MAP[x][y] == '0') {
dfs(MAP, visit, x, y);
}
else
visit[x][y] = 1;
int temp = 0;
for (int i = 1; i <= SIZE; i++)
for (int j = 1; j <= SIZE; j++) {
if (visit[i][j] == -1)
temp++;
}
if (temp == BOOM)
return 2;
return 0;
}
void show(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int x, int y) {
printf("-------------------------------------------------\n");
for (int i = 0; i <= SIZE + 1; i++) {
printf("|");
for (int j = 0; j <= SIZE + 1; j++) {
if (visit[i][j] == 1) {
printf(" %c ", MAP[i][j]);
printf("|");
}
else {
printf(" ");
printf("|");
}
}
printf("\n");
printf("-------------------------------------------------\n");
}
}
main.cpp
#include
#include
#include
#include
#include"mine.h"
char MAP[SIZE + 2][SIZE + 2];
int visit[SIZE + 2][SIZE + 2];
int x, y;//坐标
int main() {
srand(time(0));
memset(visit, -1, sizeof(visit));
MAP[0][0] = ' ';
for (int i = 1; i <= SIZE; i++) {
MAP[0][i] = 'A' + i - 1;
MAP[i][0] = 'a' + i - 1;
MAP[SIZE + 1][i] = 'A' + i - 1;
MAP[i][SIZE + 1] = 'a' + i - 1;
}
for (int i = 1; i <= SIZE + 1; i++) {
visit[0][i] = 1;
visit[i][0] = 1;
visit[SIZE + 1][i] = 1;
visit[i][SIZE + 1] = 1;
}
show(MAP, visit, x, y);
scanf("%d%d", &y, &x);
init(MAP,visit, x, y);
memset(visit, -1, sizeof(visit));
for (int i = 1; i <= SIZE + 1; i++) {
visit[0][i] = 1;
visit[i][0] = 1;
visit[SIZE + 1][i] = 1;
visit[i][SIZE + 1] = 1;
}
check(MAP, visit, x, y);
system("cls");
show(MAP, visit, x, y);
while (MAP[x][y] != '*') {
scanf("%d%d", &y, &x);
int res = check(MAP, visit, x, y);
system("cls");
show(MAP, visit, x, y);
if (res == 2) {
printf("Win\n");
system("pause");
return 0;
}
else if (res == 1) {
printf("Lose\n");
system("pause");
return 0;
}
}
return 0;
}
另附原始版:
#include
#include
#include
#include
#define BOOM 10
#define SIZE 10
char MAP[SIZE + 2][SIZE + 2];
int visit[SIZE + 2][SIZE + 2];
int x, y;
int stemp[8][2] = { {0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{1,-1}, {-1,1} };
void init(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(int i, int j) {
visit[i][j] = 1;
for (int k = 0; k < 8; k++) {
if (MAP[i + stemp[k][0]][j + stemp[k][1]] == '0'&&visit[i + stemp[k][0]][j + stemp[k][1]] != 1)
dfs(i + stemp[k][0], j + stemp[k][1]);
else if (MAP[i + stemp[k][0]][j + stemp[k][1]] != '*'&&visit[i + stemp[k][0]][j + stemp[k][1]] != 1)
visit[i + stemp[k][0]][j + stemp[k][1]] = 1;
}
}
int check() {
if (MAP[x][y] == '*') {
visit[x][y] = 1;
return 1;
}
else if (MAP[x][y] == '0') {
dfs(x, y);
}
else
visit[x][y] = 1;
int temp = 0;
for (int i = 1; i <= SIZE; i++)
for (int j = 1; j <= SIZE; j++) {
if (visit[i][j] == -1)
temp++;
}
if (temp == BOOM)
return 2;
return 0;
}
void show() {
printf("-------------------------------------------------\n");
for (int i = 0; i <= SIZE + 1; i++) {
printf("|");
for (int j = 0; j <= SIZE + 1; j++) {
if (visit[i][j] == 1) {
printf(" %c ", MAP[i][j]);
printf("|");
}
else {
printf(" ");
printf("|");
}
}
printf("\n");
printf("-------------------------------------------------\n");
}
}
int main() {
srand(time(0));
memset(visit, -1, sizeof(visit));
MAP[0][0] = ' ';
for (int i = 1; i <= SIZE; i++) {
MAP[0][i] = 'A' + i - 1;
MAP[i][0] = 'a' + i - 1;
MAP[SIZE + 1][i] = 'A' + i - 1;
MAP[i][SIZE + 1] = 'a' + i - 1;
}
for (int i = 1; i <= SIZE + 1; i++) {
visit[0][i] = 1;
visit[i][0] = 1;
visit[SIZE + 1][i] = 1;
visit[i][SIZE + 1] = 1;
}
show();
scanf("%d%d", &x, &y);
init(x, y);
memset(visit, -1, sizeof(visit));
for (int i = 1; i <= SIZE + 1; i++) {
visit[0][i] = 1;
visit[i][0] = 1;
visit[SIZE + 1][i] = 1;
visit[i][SIZE + 1] = 1;
}
check();
system("cls");
show();
while (MAP[x][y] != '*') {
scanf("%d%d", &x, &y);
int res = check();
system("cls");
show();
if (res == 2) {
printf("Win\n");
system("pause");
return 0;
}
else if (res == 1) {
printf("Lose\n");
system("pause");
return 0;
}
}
return 0;
}