C语言完成扫雷游戏的编程

C语言完成扫雷游戏的编程

首先用一个二维数组来表示玩家所看到的地图,但是实际需要两张地图。
一张是玩家看到的地图,一张则是地雷的分布图。
对于地雷的分布图,我们可以使用char类型的二维数组,其中‘1’表示地雷,‘0’表示不是地雷;而对于玩家看到的地图,我们可以初始化全为‘ ’,所以也用char类型的二维数组。

分析游戏的流程:首先打印玩家看到的地图,提示玩家开始输入坐标;然后检查输入的坐标对应的是不是地雷,若是地雷则游戏结束,若不是地雷则游戏继续并且判断该位置的周围8个地方的地雷数目,然后将这个地雷数赋值给该座标。以此类推,直到所有不是地雷的格子都被翻开,并且是地雷的格子都没有被翻开,则扫雷成功。

我的代码分为三部分:
第一部分是头文件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


void Initboard(char board[ROWS][COLS],int rows,int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void blank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

第二部分是源文件的函数部分game.c:

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//初始化雷盘
void Initboard(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
//打印雷盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (j = 0; j <= col; j++)
	{
		printf(" %d", j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf(" %d", i);
		for (j = 1; j <=col; j++)
		{
			printf(" %c", board[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}
//放置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = 10;
	while (count)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}
//计算周围雷的个数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	return 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] - 7 * '0';
}
//展开周围没有雷的格子
void blank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	if (mine[x][y] == '0')
	{
		show[x][y] = GetMineCount(mine, x, y);
		if (show[x][y] == '0')
		{
			show[x][y] = ' ';
			if (show[x - 1][y - 1] == '*')
			{
				blank(mine, show, x - 1, y - 1);
			}
			if (show[x - 1][y] == '*')
			{
				blank(mine, show, x - 1, y);
			}
			if (show[x - 1][y + 1] == '*')
			{
				blank(mine, show, x - 1, y + 1);
			}
			if (show[x][y + 1] == '*')
			{
				blank(mine, show, x, y + 1);
			}
			if (show[x + 1][y + 1] == '*')
			{
				blank(mine, show, x + 1, y + 1);
			}
			if (show[x + 1][y] == '*')
			{
				blank(mine, show, x + 1, y);
			}
			if (show[x + 1][y - 1] == '*')
			{
				blank(mine, show, x + 1, y - 1);
			}
			if (show[x][y - 1] == '*')
			{
				blank(mine, show, x, y - 1);
			}
		}
	}
}
//扫雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<ROW*COL-10)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x<row && y>=1 && y < col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了,游戏结束\n");
				break;
			}
			else 
			{
				/*int count = *//*GetMineCount(mine, x, y);*/
				show[x][y] = GetMineCount(mine, x, y);
				blank(mine, show, x, y);
				DisplayBoard(mine, ROW, COL);
				DisplayBoard(show, ROW, COL);
			}
		}
		else
		{
			printf("坐标输入有误,请重新输入\n");
		}
	}
	if (win >= ROW*COL - 10)
	{
		printf("恭喜你!扫雷成功\n");
	}
}

第三部分是源文件的游戏测试部分test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void test()
{
	printf("******************************\n");
	printf("******1.play*****0.exit*******\n");
	printf("******************************\n");
}
void game()
{
	char mine[ROWS][COLS];
	char show[ROWS][COLS];
	Initboard(mine, ROWS, COLS, '0');
	Initboard(show, ROWS, COLS, '*');
	DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);
	SetMine(mine, ROW, COL);
	DisplayBoard(mine, ROW, COL);
	FindMine(mine, show, ROW, COL);
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do{
		test();
		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;
}

猜你喜欢

转载自blog.csdn.net/weixin_44376146/article/details/87388347