延拓版扫雷

来源:互联网 发布:办公室网络综合布线 编辑:程序博客网 时间:2024/06/03 21:09

扫雷延拓

    关于扫雷这个小游戏,我想大家都很熟悉了吧!说起扫雷就让我想起了,我那充满乐趣的童年。但让我最恼火的是,第一步扫雷就被炸死,于是乎,进行我就进行了扫雷游戏的延拓版用c语言进行了编写,希望小伙伴们喜欢,并找回童年时候的乐趣。

    对于扫雷,本次我对其进行了俩个方面的延拓:第一方面,为使玩家有足够的乐趣,防止第一步被炸死,进行了优化;另一方面,为使玩家得到一定的挑战,每次选择后只显示所选位置周围的少部分区域。下面,我将给小伙伴们呈现优化后的c语言代码:

Game.h

#include <stdio.h>

#include <stdlib.h>

#define rows 11

#define cols 11

#define row rows-1

#define col cols-1

#include<time.h>

void display(char show[rows][cols]);//呈现扫雷图

void init(char *p, char r);//初始化

void set_mine(char mine[rows][cols]);//布雷

int get_num(char mine[rows][cols], int x, int y);

void expand(char mine[rows][cols], char show[rows][cols], int x, int y);//拓展

void game();

Game.c

#include"game.h"

void manu()

{

printf("******************************************\n");

printf("*****    1 play       0 exit   ***********\n");

printf("******************************************\n");

 

}

int main()

{

int a = 0;

manu();

printf("请选择:>");

scanf("%d", &a);

 

getchar();

if (1 == a)

{

game();

}

else if (0 == a)

{

exit;

}

else

printf("选择错误,请重新进行选择:>");

getchar();

return 0;

}

Text.c

#include"game.h"

void game()

{

int sum = 0;//判断是否结束而设定的变量

int con = 1;//防止第一次被炸死设定的变量

int x = 0;

int y = 0;

char mine[rows][cols] = { 0 };

char show[rows][cols] = { 0 };

init(show, '*');

init(mine, '0');

set_mine(mine);

while (1)

{

display(mine);

display(show);

printf("请输入想要扫的地方:>");

scanf("%d %d", &x, &y);

getchar();

if (((x > 0) && (x < rows - 1) && (y>0) && (y < cols - 1)) != 1)

{

printf("输入地址不存在,请重新输入:>");

}

    else if (mine[x][y] == '1')

{

if (con == 1)//防止第一次扫雷被炸死

{

mine[x][y] = '0';

if (mine[x + 1][y] == '0'&&(x+1)<rows-1)

{

mine[x + 1][y] ='1';

}

else if (mine[x - 1][y] == '0' && (x - 1) > 0)

{

mine[x - 1][y] = '1';

}

else if (mine[x ][y+1] == '0' && (y+1) <cols-1)

{

mine[x][y+1] ='1';

}

else if (mine[x][y-1] == '0' && (y-1) > 0)

{

mine[x][y-1] = '1';

}

show[x][y] = get_num(mine, x, y) + '0';

expand(mine, show, x, y);

}

else

{

printf("选择的地方有地雷,扫雷失败!");

return 0;

}

}

else

{

show[x][y] = get_num(mine, x, y)+'0';

expand(mine, show, x, y);

}

con++;

for (x = 1; x < rows - 1; x++)//判断是否结束

{

for (y = 1; y < cols - 1; y++)

{

if (show[x][y] == '*')

sum++;

}

}

if (sum == 71)

{

printf("恭喜你!扫雷成功!");

return 0;

}

}

}

//扩展函数

void expand(char mine[rows][cols], char show[rows][cols], int x, int y)

{

if (((x > 0) && (x < rows - 1) && (y>0) && (y < cols - 1)))

{

if (mine[x - 1][y] == '0')

{

show[x - 1][y] = get_num(mine, (x - 1), y) + '0';

}

else

{

/*show[x - 1][y] = '0'*/;

}

if (mine[x - 1][y - 1] == '0')

{

show[x - 1][y - 1] = get_num(mine, (x - 1), (y - 1)) + '0';

}

else

{

/*show[x - 1][y - 1] = '0'*/;

}

if (mine[x][y - 1] == '0')

{

show[x][y - 1] = get_num(mine, x, (y - 1)) + '0';

}

else

{

/*show[x][y - 1] = '0'*/;

}

if (mine[x + 1][y - 1] == '0')

{

show[x + 1][y - 1] = get_num(mine, (x + 1), (y - 1)) + '0';

}

else

{

/*show[x + 1][y - 1] = '0'*/;

}

if (mine[x + 1][y] == '0')

{

show[x + 1][y] = get_num(mine, (x + 1), y) + '0';

}

else

{

/*show[x + 1][y] = '0'*/;

}

if (mine[x + 1][y + 1] == '0')

{

show[x + 1][y + 1] = get_num(mine, (x + 1), (y + 1)) + '0';

}

else

{

/*show[x + 1][y + 1] = '0'*/;

}

if (mine[x][y + 1] == '0')

{

show[x][y + 1] = get_num(mine, x, (y + 1)) + '0';

}

else

{

/*show[x][y + 1] = '0'*/;

}

if (mine[x - 1][y + 1] == '0')

{

show[x - 1][y + 1] = get_num(mine, (x - 1), (y + 1)) + '0';

}

else

{

/*show[x - 1][y + 1] = '0'*/;

}

}

}

int get_num(char mine[rows][cols],int x,int y)

{

int count = mine[x-1][y]+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]-'0'*8;

return count;

}

void display(char show[rows][cols])

{

int i = 0;

int j = 0;

printf("图:>\n");

for (i = 0; i < rows-1; i++)

{

printf(" %-2d ",i);

}

printf("\n");

for (i = 1; i < rows-1; i++)

{

printf("%2d  ",i);

for (j = 1; j < cols-1; j++)

{

printf("%2c  ", show[i][j]);

}

printf("\n");

}

}

 

void init(char show[rows][cols], char r)

{

int x = 0;

int y = 0;

int i = 0;

int j = 0;

srand((unsigned int)time(NULL));

for (i = 0; i < rows; i++)

for (j = 0; j < cols; j++)

{

show[i][j]= r;

}

}

 

void set_mine(char mine[rows][cols])

{

srand((unsigned int)time(NULL));

int count = 10;

int x = 0;

int y = 0;

while (count)

{

x = rand() % 9 + 1;

y = rand() % 9 + 1;

if (mine[x][y]== '0')

{

mine[x][y] = '1';

count--;

}

}

}

对于本次的扫雷,希望让小伙伴们都想起自己那充满快乐的童年!!!