C语言编写一个简单的扫雷
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <time.h>
# define M 10//····竖坐标
# define N 20//····横坐标
# define S 30//····地雷数量
struct ZL {
int x;
int y;
}zb[S];//·····坐标值
void MAP(int(*lei)[N + 2], int(*map)[N + 2]);//·····打印游戏界面
void LEI(int(*lei)[N + 2]);//····地雷界面
int main() {
int lei[M + 2][N + 2] = { 0 }, map[M + 2][N + 2] = { 0 };
int i, j, m, n, x, y, s, t;
LEI(lei);
while (1) {
s = 0;
MAP(lei, map);
printf("\n请输入坐标(格式:X空格Y):\n");
scanf("%d%d", &m, &n);
if (m > M || n > N) {
continue;
}
if (lei[m][n] == 1) {
system("cls");
for (i = 0, x = -1; i <= 2 * (M + 1); i++) {
if (i % 2 == 0) {
x++;
}
for (j = 0, y = -1; j <= 2 * (N + 1); j++) {
if (j % 2 == 0) {
y++;
}
if ((i == 0 || i == 2 * (M + 1)) && (j == 0 || j == 2 * (N + 1))) {
printf("+");
if (i == 0 && j == 2 * (N + 1)) {
printf("------y");
}
}
else {
if (i % 2 == 0) {
if (j % 2 == 0) {
printf("-");
}
else {
printf("---");
}
}
else {
if (j % 2 == 0) {
printf("|");
}
else {
if (i == 1) {
printf("%-3d", (j - 1) / 2);
}
else if (j == 1) {
printf("%-3d", (i - 1) / 2);
}
else {
if (lei[x][y] != 1) {
printf(" %d ", lei[x][y] / 2);
}
else {
printf(" * ");
}
}
}
}
}
}
printf("\n");
}
printf("|\n|\n|\nX\n\n\n\t\tGame Over!\n");
break;
}
else {
map[m][n] = 1;
while (1) {
t = 1;
for (i = 1; i < M + 2; i++) {
for (j = 1; j < N + 2; j++) {
if (map[i][j] == 1 && lei[i][j] == 0) {
if (i - 1 != 0 && j - 1 != 0 && map[i - 1][j - 1] != 1) {
map[i - 1][j - 1] = 1;
t = 0;
}
if (i - 1 != 0 && map[i - 1][j] != 1) {
map[i - 1][j] = 1;
t = 0;
}
if (i - 1 != 0 && j + 1 != N + 1 && map[i - 1][j + 1] != 1) {
map[i - 1][j + 1] = 1;
t = 0;
}
if (j - 1 != 0 && map[i][j - 1] != 1) {
map[i][j - 1] = 1;
t = 0;
}
if (j + 1 != N + 1 && map[i][j + 1] != 1) {
map[i][j + 1] = 1;
t = 0;
}
if (i + 1 != M + 1 && j - 1 != 0 && map[i + 1][j - 1] != 1) {
map[i + 1][j - 1] = 1;
t = 0;
}
if (i + 1 != M + 1 && map[i + 1][j] != 1) {
map[i + 1][j] = 1;
t = 0;
}
if (i + 1 != M + 1 && j + 1 != N + 1 && map[i + 1][j + 1] != 1) {
map[i + 1][j + 1] = 1;
t = 0;
}
}
}
}
if (t == 1) {
break;
}
}
}
for (i = 1; i < M + 1; i++) {
for (j = 1; j < N + 1; j++) {
if (map[i][j] == 0) {
s++;
}
}
}
if (s == S) {
printf("\t\t恭喜你成功了!!!\n");
break;
}
}
return 0;
}
//·····打印游戏界面
void MAP(int(*lei)[N + 2], int(*map)[N + 2]) {
int i, j, x, y;
system("cls");
for (i = 0, x = -1; i <= 2 * (M + 1); i++) {
if (i % 2 == 0) {
x++;
}
for (j = 0, y = -1; j <= 2 * (N + 1); j++) {
if (j % 2 == 0) {
y++;
}
if ((i == 0 || i == 2 * (M + 1)) && (j == 0 || j == 2 * (N + 1))) {
printf("+");
if (i == 0 && j == 2 * (N + 1)) {
printf("------y");
}
}
else {
if (i % 2 == 0) {
if (j % 2 == 0) {
printf("-");
}
else {
printf("---");
}
}
else {
if (j % 2 == 0) {
printf("|");
}
else {
if (i == 1) {
printf("%-3d", (j - 1) / 2);
}
else if (j == 1) {
printf("%-3d", (i - 1) / 2);
}
else {
if (map[x][y] == 1) {
printf("%-3d", lei[x][y] / 2);
}
else {
printf("[*]");
}
}
}
}
}
}
printf("\n");
}
printf("|\n|\n|\nX\n");
}
//······················生成地雷地图
void LEI(int(*lei)[N + 2]) {
int i, j, x, y;
srand(time(NULL));
for (i = 0; i < S; i++) {//··········生成地雷坐标
x = zb[i].x = abs(rand() % M) + 1;
y = zb[i].y = abs(rand() % N) + 1;
if (lei[x][y] == 1) {
i--;
continue;
}
lei[x][y] = 1;
//printf("%-5d%-5d\n", zb[i].x, zb[i].y);
}
for (i = 1; i <= M; i++) {//·········生成地雷周围数字
for (j = 1; j <= N; j++) {
if (lei[i][j] == 1) {
continue;
}
if (lei[i - 1][j - 1] == 1) {
lei[i][j]++;
}
if (lei[i - 1][j] == 1) {
lei[i][j]++;
}
if (lei[i - 1][j + 1] == 1) {
lei[i][j]++;
}
if (lei[i][j - 1] == 1) {
lei[i][j]++;
}
if (lei[i][j + 1] == 1) {
lei[i][j]++;
}
if (lei[i + 1][j - 1] == 1) {
lei[i][j]++;
}
if (lei[i + 1][j] == 1) {
lei[i][j]++;
}
if (lei[i + 1][j + 1] == 1) {
lei[i][j]++;
}
lei[i][j] *= 2;
}
}
}
版权声明:本文为wzh1378008099原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。