1.往期作品
2.作者祝福
各位朋友元旦快乐!转眼间我们又度过了新的一年,希望大家在新的一年心想事成!最近有朋友私信我能不能把我的扫雷源码分享出来,我很高兴朋友们在背后默默支持我,因此我很乐意将源码分享给你们,希望大家多多练习,写出更好的代码!
3.代码展现
1.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("****************************\n");
printf("** 0.exit 1.play **\n");
printf("****************************\n");
printf("please choose:\n");
}
void game()
{
srand((unsigned)time(NULL));
char mine[Rows][Cols] ;
char show[Rows][Cols] ;
Initboard(mine,Rows,Cols,'0');
Initboard(show,Rows,Cols,'*');
Setmine(mine);
#ifdef Debug
Showboard(mine,Row, Col);//测试结果是否正确
#endif
Findmine(mine,show);
}
int main()
{
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("exit successfully\n");
break;
default:
printf("invalid input,please input again\n");
break;
}
} while (input);
return 0;
}
2.game.h
#define _CRT_SECURE_NO_WARNINGS 1
#define Row 10
#define Col 10
#define Rows Row+2
#define Cols Col+2
#define Mine_number 2
//#define Debug 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void Initboard(char board[Rows][Cols], int row, int col, char flag);//初始化数组
void Showboard(char board[Rows][Cols], int row, int col);//在屏幕上显示
void Setmine(char mine[Rows][Cols]);//设置雷
void Caculate(char mine[Rows][Cols], char show[Rows][Cols], int x, int y);//递归计算雷的个数
int Getminenum(char mine[Rows][Cols],int x, int y);//计算周围的雷的个数
int Check_win(char mine[Rows][Cols], char show[Rows][Cols], int row, int col);//判断是否胜利
void Findmine(char mine[Rows][Cols], char show[Rows][Cols]);//找雷
3.game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Initboard(char board[Rows][Cols], int row, int col, char flag)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = flag;
}
}
}
void Showboard(char board[Rows][Cols], int row, int col)
{
int i = 1,j = 1;
printf("\n");
for ( i = 1; i <= row; i++)
{
printf("%-2d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("---");
}
printf("\n");
for ( i = 1; i <= row; i++)
{
for ( j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("| %d\n", i);
}
}
void Setmine(char mine[Rows][Cols])
{
int count = Mine_number;
while (count)
{
int x = rand() % Row + 1;
int y = rand() % Col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void Caculate(char mine[Rows][Cols],char show[Rows][Cols],int x,int y)
{
int num = Getminenum(mine, x, y);
show[x][y] = '0' + num;
if (num == 0)
{
if (x >= 1 && x <= Row && y >= 1 && y <= Col)
{
if (show[x - 1][y] == '*')
{
Caculate(mine, show, x - 1, y);
}
if (show[x][y - 1] == '*')
{
Caculate(mine, show, x, y - 1);
}
if (show[x][y + 1] == '*')
{
Caculate(mine, show, x, y + 1);
}
if (show[x + 1][y] == '*')
{
Caculate(mine, show, x + 1, y);
}
}
}
}
int Getminenum(char mine[Rows][Cols], int x, int y)
{
return mine[x - 1][y - 1] +
mine[x - 1][y] +
mine[x - 1][y + 1] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';
}
int Check_win(char mine[Rows][Cols],char show[Rows][Cols],int row,int col)
{
int count = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if (show[i][j] >= '0' && show[i][j] <= '8')
{
count++;
}
}
}
return count;
}
void Findmine(char mine[Rows][Cols], char show[Rows][Cols])
{
int win = Row * Col - Mine_number;
Showboard(show, Row, Col);
while (1)
{
printf("please input x and y\n");
int x, y;
scanf("%d %d", &x, &y);
if (x >= 1 && x <= Row && y >= 1 && y <= Col)
{
if (mine[x][y] == '1')
{
printf("game over\n");
Showboard(mine, Row, Col);
break;
}
else
{
Caculate(mine, show, x, y);
Showboard(show, Row, Col);
int count = Check_win(mine, show, Row, Col);
if (count == win)
{
break;
}
}
}
else
{
printf("please input correct x and y\n");
}
}
if (Check_win(mine, show, Row, Col) == win)
{
printf("you win\n");
}
}