扫雷小程序是c语言学习到一个阶段之后的简单应用,c语言初学者学习到数组之后就可以着手开始写这个小代码,代码不长,200+,且属于比较简单的逻辑,主要逻辑如下:
1. 构建棋盘
2. 构造雷盘,并在雷盘上随机放入一定量的的雷数
3. 用户规定一个位置,如果信息为0则继续展开,如果有雷的信息,则输出到棋盘上,不继续展开
4. 失败条件,踩到雷;
5. 胜利条件没有除了雷以外的空位置;
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#define MINE 10
void menu()
{
printf("\t\t******欢 迎 扫 雷******\n");
printf("\t\t******1,开始游戏******\n");
printf("\t\t******0. 退出游戏******\n");
}
void show(int arr[12][12])
{
int i = 0;
int j = 0;
for (i = 1; i <= 10; i++)
{
printf("\t\t*****************************************\n");
printf("\t\t");
for (j = 1; j <= 10; j++)
{
printf("| %2d", arr[i][j]);
}
printf("|\n");
}
printf("\t\t*****************************************\n");
}
void is_board(int arr[][12])
{
int i, j;
for (i = 0; i < 12; i++)
{
for (j = 0; j < 12; j++)
{
arr[i][j] = 0;
}
}
}
void is_board2(int arr[12][12])
{
int i;
int m, n;
is_board(arr);
int count = 0;
for (i = 1; i <= 10; i++)
{
m = rand() % 10;
n = rand() % 10;
if (arr[m + 1][n + 1] == 0)
{
arr[m + 1][n + 1] = 9;
}
else
{
i--;
}
}
//show(arr);
}
void sliem(int arr2[12][12])
{
int i = 0;
int j = 0;
int m, n;
int count = 0;
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10; j++)
{
if (arr2[i][j] != 9)
{
count = 0;
for (m = i - 1; m <= i + 1; m++)
{
for (n = j - 1; n <= j + 1; n++)
{
if (arr2[m][n] == 9 &&( m!=i||n!=j))
count++;
}
}
arr2[i][j] = count;
}
}
}
}
void showEmpty(int a[][12],int boom[][12],int x, int y)
{ //展开空白
if (boom[x][y] == 0)
{
boom[x][y] = 10;
if (x - 1 >= 1&&boom[x-1][y]!=10)
{
showEmpty(a,boom, x - 1, y);
//boom[x-1][y] = 0;
}
if (y - 1 >= 1&&boom[x][y-1]!=10)
{
showEmpty(a, boom, x, y - 1);
//boom[x][y-1] = 0;
}
if (y + 1 <= 10 && boom[x][y + 1] != 10)
{
showEmpty(a, boom, x, y + 1);
//boom[x][y+1] = 0;
}
if (x + 1 <= 10 && boom[x+1][y] != 10)
{
showEmpty(a, boom, x + 1, y);
//boom[x+1][y] = 0;
}
if (x + 1 <= 10 && boom[x + 1][y] != 10)
{
showEmpty(a, boom, x + 1, y);
//boom[x+1][y] = 0;
}
}
else if (boom[x][y] == 10)
{
a[x][y] = 10;
}
a[x][y] = boom[x][y];
}
void find_boom(int arr[12][12], int boom[12][12], int i, int j)
{
arr[i][j] = boom[i][j];
if (boom[i][j] == 0)
{
showEmpty(arr, boom, i, j);
//show(arr);
//真的不会了 8个方向展开 停止条件 遇到边界 有非0 数字 20的循环条件 遇到边界 停止 遇到数字 停止 是0 加入区域 非零 直接输出
}
}
int judge(int arr[][12], int boom[][12], int i, int j)
{
if (boom[i][j] == 9)
return 1;
else
find_boom(arr, boom, i, j);
return 0;
}
int win(int arr[][12], int boom[][12])
{
int i, j;
int count = 0;
for (i = 1; i <= 10; i++)
{
for (j = 1; j <= 10; j++)
{
if (arr[i][j] == boom[i][j])
count++;
if (arr[i][j] == 0 && boom[i][j] == 9)
count++;
}
}
if (count == 100)
return 0;
else
return 1;
}
void game()
{
int arr1[12][12] = { 0 }; //qipan
int boom[12][12] = { 0 }; //雷的信息
int i = 0;
int m, n;
int count = 0;
is_board(arr1);
is_board2(boom);
show(arr1);
sliem(boom);
show(boom);
do{
printf("请输入要扫的位置\n");
printf("提示:10 表示 0颗雷\n");
scanf("%d%d", &m, &n);
count++;
if (arr1[m][n] != 0)
{
printf("\t已经被扫过了请重新输入\n");
}
if ((judge(arr1, boom, m, n) == 1))
{
if (count != 1)
{
printf("你被炸死了\n");
show(boom);
break;
}
else
printf("\t新手保护机制触发,这个位置有雷!!!!\n");
}
show(arr1);
} while (win(arr1, boom) == 1);
if (win(arr1, boom) == 0)
printf("你赢了\n");
}
int main()
{
srand((unsigned int)time(NULL));
int key = 0;
do
{
menu();
printf("请输入->\n");
scanf_s("%d", &key);
switch (key)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误,请重新输入\n");
}
} while (key);
return 0;
}