C语言 扫雷

代码思路:

  • 打印菜单,用户选择是否玩游戏;
  • 设置两个数组,数组mine用来布雷(srand()和rand()配合使用,雷用‘1’表示),数组show用于输出;
  • 如果第一次是雷,则将雷移走,如果不是雷,则计算它周围有多少雷并输出;
  • 如果之后又踩雷则炸死,执行row*col-Easy_Count次没有炸死即为成功;
  • 坐标周围没雷,可以实现展开
  • 重新选择是否继续玩游戏;
game.h    函数的声明
  • #define _CRT_SECURE_NO_WARNINGS  1
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define ROW 9              
    #define COL 9
    #define ROWS  ROW+2
    #define COLS  COL+2 
    #define Easy_Count 10    //雷的数量
    void InitBoard(char arr[ROWS][COLS],int rows,int cols,char sz);          //初始化数组
    void SetMine(char mine[ROWS][COLS], int row, int col);                   //布雷
    void DisplayBoard(char arr[ROWS][COLS], int row, int col);               //打印数组
    void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);  //扫雷

test.c   

#include"game.h"
void menu()
{
	printf("*******************\n");
	printf("*****1    play*****\n");
	printf("*****0    exit*****\n");
	printf("*******************\n");
}
void play()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	InitBoard(mine, ROWS, COLS,'0');    //初始化  mine初始化为字符‘0’,show初始化为字符‘*’;
	InitBoard(show, ROWS, COLS,'*');
	SetMine(mine, ROW, COL);            //放雷
	DisplayBoard(show, ROW, COL);       //打印数组show
	FindMine(mine, show, ROW, COL);     //扫雷
	printf("雷阵:\n");
	DisplayBoard(mine, ROW, COL);
}
void test()
{
	int input;
	srand((unsigned)time(NULL));
	do
	{
		menu();
		printf("请做出选择:>"); 
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			play(); 
			break;
		case 0: printf("退出游戏!\n"); break;
		default:printf("输入错误,请重新输入!\n"); break;
		}
	} while (input);
}
int main()
{
	test();
	system("pause");
	return 0;
}

game.c   (注意引头文件--#include"game.h")

初始化函数:

void InitBoard(char arr[ROWS][COLS], int rows, int cols,char sz)
{
	int i, j;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = sz;
		}
	}
}

随机布雷:

void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int i=0;
	while (i < Easy_Count)
	{
		int x = rand() % row + 1;           //rand()%row产生的是0~row-1之间的数
		int y = rand() % col + 1;
		if (mine[x][y] == '0')                 //防止重复放雷
		{
			mine[x][y] = '1';
			i++;
		}
	}
}

打印数组:

为了方便用户输入坐标,在打印前加上序号;

void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
	int i, j;
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d",i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

扫雷函数:(优化:第一次踩雷,不死,将雷挪走)

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0, y = 0;
	int win = 0;
	int i = 1;
	int t, m;
	while (1)
	{
		printf("请输入坐标:>");
		scanf("%d%d", &x, &y);
		if ((x >= 1 && x <= row) && (y >= 1 && y <= col))     
		{
			if (mine[x][y] == '1')
			{
				if (i > 0)                          //第一次是雷,挪走雷,不被炸死
				{ 
					    int count = 0;
						mine[x][y] = '0';
						t = rand() % row + 1;         
						m = rand() % col + 1;
						mine[t][m] = '1';               //重新给雷找位置
						count = CountMine(mine, x, y);      
						show[x][y] = count + '0';
						DisplayBoard(show, ROW, COL);
						i--;
				}
				else
				{
					printf("很遗憾,你被炸死了!\n");
					break;
				}
			}
			else if (show[x][y] == '*')   //统计周围雷的个数,防止一个位置count++两次
			{
				
				int count = 0;
				count = CountMine(mine, x, y);
				win++;
				show[x][y] = count + '0';
				if (show[x][y] = '0')
				{
					open_board(mine,show,x, y);
				}
				DisplayBoard(show, ROW, COL);
				if (win == row*col - Easy_Count)
				{
					printf("恭喜你,闯关成功!\n");
					break;
				}
				i--;
			}
			else
				printf("坐标重复输入,请重新输入:\n");
		}
		else
		{
			printf("输入的坐标无效,请重新输入!\n");
		}
	}
}

扫雷函数中用到的CountMine:

字符1-字符0==数字1;

int CountMine(char mine[ROWS][COLS], int x, int y)
{
	int count;
	count = (mine[x][y - 1] + mine[x][y + 1] + mine[x - 1][y] + 
		mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x + 1][y] + mine[x+1][y - 1] + mine[x + 1][y + 1]) - 8 * '0';
	return count;
}

扫雷函数中用到的open_board(如果坐标周围没雷,可以实现展开)

扫描二维码关注公众号,回复: 1048200 查看本文章
void open_board(char mine[ROWS][COLS], char show[ROWS][COLS],int x, int y)
{
	if (mine[x - 1][y - 1] = '0')
	{
		show[x - 1][y - 1] = CountMine(mine,x-1,y-1) + '0';
	}
	if (mine[x - 1][y] = '0')
	{
		show[x - 1][y] = CountMine(mine, x - 1, y ) + '0';
	}
	if (mine[x - 1][y + 1] = '0')
	{
		show[x - 1][y + 1] = CountMine(mine, x - 1, y + 1) + '0';
	}
	if (mine[x + 1][y - 1] = '0')
	{
		show[x + 1][y - 1] = CountMine(mine, x + 1, y - 1) + '0';
	}
	if (mine[x + 1][y] = '0')
	{
		show[x + 1][y] = CountMine(mine, x + 1, y) + '0';
	}
	if (mine[x + 1][y + 1] = '0')
	{
		show[x + 1][y + 1] = CountMine(mine, x +1, y + 1) + '0';
	}
	if (mine[x ][y - 1] = '0')
	{
		show[x ][y - 1] = CountMine(mine, x , y - 1) + '0';
	}
	if (mine[x ][y + 1] = '0')
	{
		show[x ][y + 1] = CountMine(mine, x, y + 1) + '0';
	}
}

运行如图:





猜你喜欢

转载自blog.csdn.net/zhao_miao/article/details/79839430
今日推荐