扫雷游戏

先简单过一下扫雷的游戏过程,方便我们接下来写代码。
首先需要一个菜单界面,再创建两个棋盘,然后将展示棋盘打印出来,接下来就是在炸弹棋盘上随机放入炸弹,然后玩家输入坐标,排查炸弹,这里扩展地写了一个展开函数,最后,还有最重要的一步,在游戏过程中判断输赢。
下面我们开始写代码,这里我们尽可能创建函数来完成游戏的功能:

game.h

#define _CRT_SECURE_NO_WARNINGS 1

#define ROW  9
#define COL  9

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

#define EASY_COUNT 10

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

//初始化棋盘
void InitBoard(char show[ROWS][COLS], char mine[ROWS][COLS]);
//打印游戏棋盘
void DisplayBoard(char show[ROWS][COLS]);
//随机放置炸弹
void SetMine(char mine[ROWS][COLS]);
//玩家找出炸弹
void FineMine(char show[ROWS][COLS], char mine[ROWS][COLS]);

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//初始化棋盘
void InitBoard(char show[ROWS][COLS], char mine[ROWS][COLS])
{
	int x = 0;
	int y = 0;
	for (x = 0; x < ROWS; x++)
	{
		for (y = 0; y < COLS; y++)
		{
			//给玩家展示的棋盘
			show[x][y] = '*';
			//放置炸弹的棋盘
			mine[x][y] = '0';
		}
	}
}

//打印游戏棋盘
void DisplayBoard(char show[ROWS][COLS])
{
	int i = 0;
	int j = 0;
	//打印行列序号和棋盘格式
	printf("  | ");
	for (i = 1; i <= ROW; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	printf("---------------------\n");
	for (i = 1; i <= ROW; i++)
	{
		printf("%d | ", i);
		for (j = 1; j <= COL; j++)
		{
			printf("%c ", show[i][j]);
		}
		printf("\n");
	}
}

//随机放置炸弹
void SetMine(char mine[ROWS][COLS])
{
	int x = 0;
	int y = 0;
	/的数量(全局变量)
	int count = EASY_COUNT;
	while (count)
	{
		x = rand() % ROW + 1;
		y = rand() % COL + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

//计算输入的坐标在放置炸弹的棋盘上的周围有几个炸弹
int get_mine(char mine_map[ROWS][COLS], int x, int y)
{
	int count = 0;
	if (mine_map[x - 1][y] == '1')
	{
		count++;
	}
	if (mine_map[x - 1][y - 1] == '1')
	{
		count++;
	}
	if (mine_map[x][y - 1] == '1')
	{
		count++;
	}
	if (mine_map[x + 1][y - 1] == '1')
	{
		count++;
	}
	if (mine_map[x + 1][y] == '1')
	{
		count++;
	}
	if (mine_map[x + 1][y + 1] == '1')
	{
		count++;
	}
	if (mine_map[x][y + 1] == '1')
	{
		count++;
	}
	if (mine_map[x - 1][y + 1] == '1')
	{
		count++;
	}
	return count;
}

//当输入坐标不是炸弹时,展开一部分不是炸弹的坐标,并计算这些坐标周围有多少炸弹
void open_board(char mine[ROWS][COLS], char show[ROWS][COLS], int i, int j)
{
	if (mine[i][j] == '0'&&i >= 0 && j >= 0 && show[i][j] == '*')
	{
		show[i][j] = get_mine(mine, i, j) + '0';
	}
	if (mine[i][j - 1] == '0'&&i >= 0 && j - 1 >= 0 && show[i][j - 1] == '*')
	{
		show[i][j - 1] = get_mine(mine, i, j - 1) + '0';
		if (get_mine(mine, i, j - 1) == 0)
		{
			open_board(mine, show, i, j - 1);
		}
	}
	if (mine[i][j + 1] == '0'&&i >= 0 && j + 1 >= 0 && show[i][j + 1] == '*')
	{
		show[i][j + 1] = get_mine(mine, i, j + 1) + '0';
		if (get_mine(mine, i, j + 1) == 0)
		{
			open_board(mine, show, i, j + 1);
		}
	}
	if (mine[i - 1][j] == '0'&&i - 1 >= 0 && j >= 0 && show[i - 1][j] == '*')
	{
		show[i - 1][j] = get_mine(mine, i - 1, j) + '0';
		if (get_mine(mine, i - 1, j) == 0)
		{
			open_board(mine, show, i - 1, j);
		}
	}
	if (mine[i - 1][j - 1] == '0'&&i - 1 >= 0 && j - 1 >= 0 && show[i - 1][j - 1] == '*')
	{
		show[i - 1][j - 1] = get_mine(mine, i - 1, j - 1) + '0';
		if (get_mine(mine, i - 1, j - 1) == 0)
		{
			open_board(mine, show, i - 1, j - 1);
		}
	}
	if (mine[i - 1][j + 1] == '0'&&i - 1 >= 0 && j + 1 >= 0 && show[i - 1][j + 1] == '*')
	{
		show[i - 1][j + 1] = get_mine(mine, i - 1, j + 1) + '0';
		if (get_mine(mine, i - 1, j + 1) == 0)
		{
			open_board(mine, show, i - 1, j + 1);
		}
	}
	if (mine[i + 1][j + 1] == '0'&&i + 1 >= 0 && j + 1 >= 0 && show[i + 1][j + 1] == '*')
	{
		show[i + 1][j + 1] = get_mine(mine, i + 1, j + 1) + '0';
		if (get_mine(mine, i + 1, j + 1) == 0)
		{
			open_board(mine, show, i + 1, j + 1);
		}
	}
	if (mine[i + 1][j] == '0'&&i + 1 >= 0 && j >= 0 && show[i + 1][j] == '*')
	{
		show[i + 1][j] = get_mine(mine, i + 1, j) + '0';
		if (get_mine(mine, i + 1, j) == 0)
		{
			open_board(mine, show, i + 1, j);
		}
	}
	if (mine[i + 1][j - 1] == '0'&&i + 1 >= 0 && j - 1 >= 0 && show[i + 1][j - 1] == '*')
	{
		show[i + 1][j - 1] = get_mine(mine, i + 1, j - 1) + '0';
		if (get_mine(mine, i + 1, j - 1) == 0)
		{
			open_board(mine, show, i + 1, j - 1);
		}
	}

}

//玩家找出炸弹
void FineMine(char show[ROWS][COLS], char mine[ROWS][COLS])
{
	int x = 0;
	int y = 0;
	int count = 0;
	while (1)
	{
		printf("请玩家输入要排查的坐标(^-^):");
		scanf("%d %d", &x, &y);
		if (x < 1 || x > ROW || y < 1 || y > COL)
		{
			printf("输入错误!Σ(`д`lll)\n");
			continue;
		}
		else if (mine[x][y] == '1')
		{
			printf("很遗憾,你被炸死了!v( -ι_- )v\n");
			break;
		}
		else
		{
			open_board(mine, show, x, y);
			DisplayBoard(show);
		}
		for (x = 1; x <= ROW; x++)
		{
			for (y = 1; y <= COL; y++)
			{
				if (show[x][y] == '*')
				{
					count++;
				}
			}
		}
		if (count == 10)
		{
			printf("恭喜你,游戏胜利!(*^-^)=3\n");
			break;
		}
		count = 0;
	}
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void meun()
{
	printf("*************************************\n");
	printf("******|-----------------------|******\n");
	printf("******|         1.play        |******\n");
	printf("******|         0.exit        |******\n");
	printf("******|-----------------------|******\n");
	printf("*************************************\n");

}

void game()
{
	srand((unsigned int)time(NULL));
	char show[ROWS][COLS];
	char mine[ROWS][COLS];
	InitBoard(show, mine);
	DisplayBoard(show);//打印游戏棋盘
	SetMine(mine);
	//DisplayBoard(mine);//打印放置炸弹的棋盘,便于测试
	FineMine(show, mine);

}

int main()
{
	int input = 0;
	do
	{
		meun();
		printf("请玩家输入\(^o^)/:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏~( ·︵· )~\n");
			break;
		default:
			printf("输入错误,请重新输入Σ(っ °Д °;)っ\n");
		}
	} while (input);

	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值