C语言----扫雷游戏

编程实现扫雷游戏的步骤主要有以下几个方面:

    首先有两个简单要求:

                           1 .第一次扫雷不能被炸死;

                           2.周围没有雷的时候,可以实现部分展开。

     实现以上要求有以下几个函数:

                           1.初始化棋盘;

                           2.打印雷棋盘;

                           3.设置雷的个数;

                           4.统计点击之处雷数量   

    具体代码如下 :


 game.h

#ifndef __game_h__
#define __game_h__

#include <stdio.h>
#include <time.h>
#include <Windows.h>

#define ROW 9   //设置行
#define COL 9   //设置列

#define ROWS ROW+2
#define COLS COL+2

#define Easy 10
#define Hard 30

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);  //初始化
void PrintBoard(char board[ROWS][COLS], int row, int col);   //打印
void SetMine(char board[ROWS][COLS], int row, int col, int count);  //设置雷
void ClearMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int count);//扫雷

#endif  //__game_h__

   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 PrintBoard(char board[ROWS][COLS], int row, int col)   //打印棋盘
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= col; i++)
	{
		printf(" %d ", i);
		if (i != col)
		{
			printf("|");
		}
	}
	printf("\n---|---|---|---|---|---|---|---|---|---\n");
	for (i = 1; i <= row; i++)
	{
		printf(" %d |", i);
		for (j = 1; j <= col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j != col)
			{
				printf("|");
			}
		}
		if (i != row)
		{
			printf("\n---|---|---|---|---|---|---|---|---|---");
		}
		printf("\n");
	}
	printf("\n");
}

void SetMine(char board[ROWS][COLS], int row, int col, int count)   //设置雷
{
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

int Surround(char board[ROWS][COLS], int x, int y)   //统计周围雷数量
{
	return board[x - 1][y] + board[x - 1][y - 1] + board[x][y - 1] + board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] + board[x][y + 1] + board[x - 1][y + 1] - 8 * '0';
}

void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)  //展开
{
	if (Surround(mine, x, y) == 0)
	{
		show[x][y] = ' ';
		if ((x - 1) > 0 && show[x - 1][y] == '*')
		{
			Expand(mine, show, x - 1, y);
		}
		if ((x - 1) > 0 && (y - 1) > 0 && show[x - 1][y - 1] == '*')
		{
			Expand(mine, show, x - 1, y - 1);
		}
		if ((y - 1) > 0 && show[x][y - 1] == '*')
		{
			Expand(mine, show, x, y - 1);
		}
		if ((x + 1) <= ROW && (y - 1) > 0 && show[x + 1][y - 1] == '*')
		{
			Expand(mine, show, x + 1, y - 1);
		}
		if ((x + 1) <= ROW && show[x + 1][y] == '*')
		{
			Expand(mine, show, x + 1, y);
		}
		if ((x + 1) <= ROW && (y + 1) <= COL && show[x + 1][y + 1] == '*')
		{
			Expand(mine, show, x + 1, y + 1);
		}
		if ((y + 1) <= COL && show[x][y + 1] == '*')
		{
			Expand(mine, show, x, y + 1);
		}
		if ((x - 1) > 0 && (y + 1) <= COL && show[x - 1][y + 1] == '*')
		{
			Expand(mine, show, x - 1, y + 1);
		}
	}
	else
	{
		show[x][y] = Surround(mine, x, y) + '0';
	}
}

void ClearMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int count)   //排雷
{
	int flag = 0;
	while (1)
	{
		int i = 0;
		int j = 0;
		int sum = 0;
		int x = 0;
		int y = 0;
		printf("请输入要排查的x坐标值:");
		scanf("%d", &x);
		printf("请输入要排查的y坐标值:");
		scanf("%d", &y);
		if (x > 0 && x <= row && y > 0 && y <= col)
		{
			if (show[x][y] == '*')
			{
				if (flag == 0 && mine[x][y] == '1')  //保证第一次排雷不会被雷炸
				{
					flag = 1;
					SetMine(mine, ROW, COL, 1);
					mine[x][y] = '0';
				}
				else if (flag != 0 && mine[x][y] == '1')
				{
					printf("你被雷炸了,游戏失败!\n");
					printf("雷阵分布图如下:\n");
					PrintBoard(mine, ROW, COL);
					break;
				}
				else
				{
					flag = 1;
				}
				Expand(mine, show, x, y);  //展开
				system("cls");  //清屏
				PrintBoard(show, ROW, COL);
			}
			else
			{
				printf("该坐标处已排查,请重新输入...\n");
			}
		}
		else
		{
			printf("坐标值超范围,请重新输入...\n");
		}
		for (i = 1; i <= row; i++)
		{
			for (j = 1; j <= col; j++)
			{
				if (show[i][j] != '*')
				{
					sum++;
				}
			}
		}
		if (sum == row*col - count)
		{
			printf("恭喜扫雷成功!\n");
			break;
		}
	}
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void Menu1()  //游戏选择进入退出菜单
{
	printf("********扫雷游戏********\n");
	printf("**                    **\n");
	printf("**       1.Play       **\n");
	printf("**       0.Exit       **\n");
	printf("**                    **\n");
	printf("************************\n");
}

void Menu2()   //游戏难易选择菜单
{
	printf("********游戏难度********\n");
	printf("**                    **\n");
	printf("**   1.Easy (%d个雷)  **\n", Easy);
	printf("**   2.Hard (%d个雷)  **\n", Hard);
	printf("**                    **\n");
	printf("************************\n");
}

void Game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	int choice = 0;
	int count = 0;
	srand((unsigned int)time(NULL));
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	Menu2();
	printf("请选择:");
	scanf("%d", &choice);
	switch (choice)
	{
	case 1:
	{
			  count = Easy;
			  PrintBoard(show, ROW, COL);
			  SetMine(mine, ROW, COL, count);
			  ClearMine(mine, show, ROW, COL, count);
	}
		break;
	case 2:
	{
			  count = Hard;
			  PrintBoard(show, ROW, COL);
			  SetMine(mine, ROW, COL, count);
			  ClearMine(mine, show, ROW, COL, count);
	}
		break;
	default:
		      printf("选择有误,请重新开始...\n"); 
		break;
	}
}

int main()
{
	int input = 0;
	do
	{
		Menu1();
		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/QwQfeifei/article/details/79904253