扫雷小游戏

1.构建菜单

static void Menu()
{
	printf("########################\n");
	printf("# 1. Play       0.Exit #\n");
	printf("########################\n");
}

2.在主函数中生成选项

int main()
{
	int quit = 0;
	int select = 0;
	while (!quit){
		Menu();
		printf("Please Enter# ");
		scanf("%d", &select);
		switch (select){
		case 1:
			Game();
			break;
		case 0:
			quit = 1;
			break;
		default:
			printf("Postion Error, Try Again!\n");
			break;
		}
	}

	printf("byebye!\n");

	system("pause");
	return 0;
}

3.定义宏常量

#define ROW 8
#define COL 8

#define STYLE '?'  //未扫的方格
#define NUM 20 //雷的个数

4.游戏函数

void Game()
{
	srand((unsigned long)time(NULL));  //随机数埋雷

	char show_board[ROW][COL];  //展示棋盘
	char mine_board[ROW][COL];   //用于埋雷棋盘

	memset(show_board, STYLE, sizeof(show_board));
	memset(mine_board, '0', sizeof(mine_board));

	SetMines(mine_board, ROW, COL);  //埋雷

	int count = (ROW - 2)*(COL - 2) - NUM;  //实际棋盘大小

	while (count){
		system("cls");            //每次操作覆盖后展示
		ShowBoard(show_board, ROW, COL);  
		printf("Please Enter Your Postion<x,y># ");
		int x = 0;
		int y = 0;
		scanf("%d %d", &x, &y);
		if (x < 1 || x > 10 || y < 1 || y > 10){
			printf("Postion Error!\n");
			continue;
		}
		if (show_board[x][y] != STYLE){
			printf("Postion Is not *\n");
			continue;
		}
		if (mine_board[x][y] == '1'){
			printf("game over!\n");   //游戏失败后再展示棋盘
			ShowBoard(mine_board, ROW, COL);
			break;
		}
		//['0', '8']
		show_board[x][y] = CountMines(mine_board, x, y);
		count--;
	}

}

5.埋雷

  随机布雷,用 “ 1 ” 表示有雷。

static void SetMines(char board[][COL], int row, int col)
{
	int count = NUM;
	while (count){
		int x = rand() % (row - 2) + 1;
		int y = rand() % (col - 2) + 1;
		if (board[x][y] == '0'){
			board[x][y] = '1';
			count--;
		}
	}
}

6.初始化并展示棋盘

  初始化雷盘,其实这个雷盘只需设计者知道,玩家不必知道。 “ 0 ” 代表没有雷。初始化展示的雷盘,这个是展示给玩家看的,雷盘用 “ 1 ” 覆盖。

static void ShowLine(int col)
{
	int i; 
	for (i = 0; i <= (col - 2); i++){
		printf("----");
	}
	printf("\n");
}
static void ShowBoard(char board[][COL], int row, int col)
{
	int i,j; 
	printf("     ");
	for (i = 1; i <= (col - 2); i++){
		printf("%d   ", i);
	}
	printf("\n");
	ShowLine(col);
	for (i = 1; i <= (row - 2); i++){
		printf("%-3d|", i);
		for (j = 1; j <= (col - 2); j++){
			printf(" %c |", board[i][j]);
		}
		printf("\n");
		ShowLine(col);
	}
}

在这里插入图片描述
  ‘?’表示未扫的格子

7.扫格后展示数字

  坐标周围没雷,可以实现展开。然后统计周围雷的个数。

static char CountMines(char board[][COL], int x, int y)
{
	return board[x - 1][y - 1] + 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] - 7 * '0';
}

在这里插入图片描述
  这里直接减少了两行两列进行数雷,优化了数据的结构,更便于计算。
在这里插入图片描述
  未踩到雷显示周围雷的个数

总结

1.初始化雷盘
2.打印雷盘
3.自行随机设置雷的分布,可选择游戏难易程度
4.统计坐标位置周围的雷数
5.扩展式排雷,展开周围的非雷区
6.给所选坐标位置做标记

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值