game.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<Windows.h>
#define H 9
#define L 9
#define H2 H+2
#define L2 L+2
#define EASY 10
void setmine(char mine[H2][L2], int h, int l, int n);
void c_mine(char mine[H2][L2], int h, int l);
void PrintForm(char mine[H2][L2], int h, int l);
void c_show(char mine[H2][L2], int h, int l);
int FindZero(char mine[H2][L2], char show[H2][L2], int h, int l)
game.c
#include"game.h"
void c_mine(char Mine[H2][L2], int row, int col)
{
memset(Mine, '0', row*col * sizeof(Mine[0][0]));
}
void c_show(char Mine[H2][L2], int row, int col)
{
memset(Mine, '*', row*col * sizeof(Mine[0][0]));
}
void PrintForm(char mine[H2][L2], int h, int l)
{
int i = 0;
int j = 0;
for (i = 0; i <= l; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= h; i++)
{
for (j = 1; j <= l; j++)
{
if (j == 1)
{
if (mine[i][j] == '0')
{
printf("%d ", i);
}
else
{
printf("%d %c", i, mine[i][j]);
}
}
else
{
if (mine[i][j] == '0')
{
printf(" ");
}
else
{
printf(" %c", mine[i][j]);
}
}
}
printf("\n");
}
}
static void movemine(char mine[H2][L2], int x, int y)
{
int a = 0;
int b = 0;
while (1)
{
a = rand() % H + 1;
b = rand() % L + 1;
if (mine[a][b] != '1')
{
mine[a][b] = '1';
break;
}
}
mine[x][y] = '0';
}
void setmine(char mine[H2][L2], int h, int l, int n)
{
int x = 0;
int y = 0;
while (n)
{
x = rand() % h + 1;
y = rand() % l + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
n--;
}
}
}
static int mineCount(char mine[H2][L2], int x, int y)
{
int add = 0;
add = mine[x - 1][y - 1]
+ mine[x][y - 1]
+ mine[x + 1][y - 1]
+ mine[x + 1][y]
+ mine[x + 1][y + 1]
+ mine[x][y + 1]
+ mine[x - 1][y + 1]
+ mine[x - 1][y]
- 8 * '0';
return add;
}
static int OpenZero(char mine[H2][L2], char show[H2][L2], int x, int y)
{
if (mineCount(mine, x, y) == 0)
{
show[x][y] = '0';
if ((x - 1) >= 1 && (x - 1) <= H && (y - 1) >= 1 && (y - 1) <= L && show[x - 1][y - 1] == '*')
OpenZero(mine, show, x - 1, y - 1);
if ((x) >= 1 && (x) <= H && (y - 1) >= 1 && (y - 1) <= L && show[x][y - 1] == '*')
OpenZero(mine, show, x, y - 1);
if ((x + 1) >= 1 && (x + 1) <= H && (y - 1) >= 1 && (y - 1) <= L && show[x + 1][y - 1] == '*')
OpenZero(mine, show, x + 1, y - 1);
if ((x + 1) >= 1 && (x + 1) <= H && (y) >= 1 && (y) <= L && show[x + 1][y] == '*')
OpenZero(mine, show, x + 1, y);
if ((x + 1) >= 1 && (x + 1) <= H && (y + 1) >= 1 && (y + 1) <= L && show[x + 1][y + 1] == '*')
OpenZero(mine, show, x + 1, y + 1);
if ((x) >= 1 && (x) <= H && (y - 1) >= 1 && (y - 1) <= L && show[x][y - 1] == '*')
OpenZero(mine, show, x, y - 1);
if ((x - 1) >= 1 && (x - 1) <= H && (y + 1) >= 1 && (y + 1) <= L && show[x - 1][y + 1] == '*')
OpenZero(mine, show, x - 1, y + 1);
if ((x - 1) >= 1 && (x - 1) <= H && (y) >= 1 && (y) <= L && show[x - 1][y] == '*')
OpenZero(mine, show, x - 1, y);
}
else
{
show[x][y] = mineCount(mine, x, y) + '0';
}
return 0;
}
static int win(char show[H2][L2], int h, int l)
{
int i = 0;
int j = 0;
int count = 0;
for (i = 1; i <= h; i++)
{
for (j = 1; j <= l; j++)
{
if (show[i][j] == '*')
count++;
}
}
return count;
}
int FindZero(char mine[H2][L2], char show[H2][L2], int h, int l)
{
int x = 0;
int y = 0;
int flag = 1;
while (EASY != win(show, h, l))
{
int count = 0;
printf("请输入坐标:>");
scanf("%d%d", &x, &y);
if (x < 1 || x > h || y < 1 || y > l)
{
printf("非法坐标,");
}
else if (show[x][y] != '*')
{
printf("该坐标已被打开,");
}
else if (mine[x][y] == '1' && flag == 1)
{
movemine(mine, x, y);
OpenZero(mine, show, x, y);
system("cls");
PrintForm(show, H, L);
flag = 0;
}
else if (mine[x][y] == '1' && flag == 0)
{
printf("你被炸死了\n\n");
PrintForm(mine, H, L);
return 0;
}
else if (mine[x][y] == '0')
{
OpenZero(mine, show, x, y);
system("cls");
PrintForm(show, H, L);
flag = 0;
}
}
printf("恭喜你扫雷成功\n");
return 0;
}
test.c
#include"game.h"
void menu()
{
printf("**********************\n");
printf("***** 1. play *****\n");
printf("***** 0. exit *****\n");
printf("**********************\n");
}
void game()
{
char mine[H2][L2] = { 0 };
char show[H2][L2] = { 0 };
c_mine(mine, H2, L2); //初始化雷区
setmine(mine, H, L, EASY); //放置雷
c_show(show, H2, L2);
system("cls");
PrintForm(show, H, L);
FindZero(mine, show, H, L);
}
int main()
{
srand((unsigned)time(NULL));
int input = 0;
do
{
menu();
printf("请输入你的选项:>");
scanf("%d", &input);
switch (input)
{
case 1:game();
break;
case 0: printf("成功退出\n");
break;
default:printf("输入错误,请重新输入\n");
break;
}
} while (input);
system("pause");
return 0;
}