C语言小游戏——扫雷

扫雷大致思路

1.打印期盼.
2.藏雷。
3.扫雷
具体步骤参考代码

text.c:大的步骤

#include "game.h"
void menu()
{
	printf("***********************************\n");
	printf("***********   1.play    ***********\n");
	printf("***********   1.exit    ***********\n");
	printf("***********************************\n");
}
int main()
{
	srand((unsigned)time(NULL));   //时间戳,用于后续rand函数取随机值
	int a = 0;
	char Board1[Rows][Cols];
	char Board2[Rows][Cols];
	do
	{
		menu();                 //打印选择菜单
		printf("请输入:>");
		scanf("%d",&a);
		if (a == 1)
		{
			printf("-------游戏开始-------\n");
			InitBoard1(Board1, Rows, Cols);           //初始化藏雷棋盘
			InitBoard2(Board2, Rows, Cols);        //初始化显示期盼
			DisplayBoard(Board2, Row, Col);        //打印棋盘
			/*DisplayBoard(Board1, Row, Col);*/
			DepositRadar(Board1, Row, Col);          //埋雷
			SweepRadar(Board1, Board2, Rows, Cols);   //扫雷
		}
	} while (a);
	return 0;

game.c 具体实现

#include "game.h"
void InitBoard1(char board[Rows][Cols], int row, int col)
{
	char board1 = '0';                         //将埋雷棋盘初始化为全0
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = board1;
		}
	}
}

void InitBoard2(char board[Rows][Cols], int row, int col)
{
	char board1 = '*';              //将显示棋盘初始化为全*
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = board1;
		}
	}
}

void DisplayBoard(char board[Rows][Cols], int row, int col)  //打印显示棋盘
{           
	for (int i = 1; i<10; i++)
	{
		for (int j = 1; j<10; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void DepositRadar(char board[Row][Col], int row, int col) //埋雷
{
	int count = 10;
	int x,y;
	while (count)
	{
		x = rand() % 9;         //产生随即坐标x
		y = rand() % 9;         //产生随即坐标y
		if (board[x][y] == '0')  //检查此坐标是否没有雷,有的话重新埋
		{
			board[x][y] = '1';
			count--;
		}
	}
}

void SweepRadar(char Board1[Rows][Cols], char Board2[Rows][Cols], int row, int col)
{     
	int x, y;
	int count = 71;            //总共71个不是雷的地方
	while (count)
	{
		printf("请输入您要扫的坐标:> ");
		scanf("%d,%d", &x, &y);
		if (Board1[x][y] == '1')   //判断是否被炸死
		{
			printf("很遗憾你被炸死了\n");
			DisplayBoard(Board2, Row, Col);  //让你死得瞑目
			DisplayBoard(Board1, Row, Col);
			break;               
		}
		else
		{
			//没有被炸死,显示周围雷的数量
			int ret = Board1[x + 1][y - 1] + Board1[x][y - 1] + Board1[x - 1][y - 1] + Board1[x + 1][y] + Board1[x - 1][y] + Board1[x + 1][y + 1] + Board1[x][y + 1] + Board1[x - 1][y + 1] - 8 * '0' ;
			Board2[x][y] = ret + '0';
			count--;  
			DisplayBoard(Board2, Row, Col);
		}
	}
	if (count == 0)   //扫完雷胜利
	{
		printf("恭喜你赢得比赛\n");
	}
}

game.h 函数所需头文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define Rows 11
#define Cols 11
#define Row 9
#define Col 9
#include<stdio.h>
#include<stdlib.h>
void InitBoard1(char board1[Rows][Cols], int row, int col);
void InitBoard2(char board[Rows][Cols], int row, int col);
void DisplayBoard(char board[Rows][Cols], int row, int col);
void DepositRadar(char board[Row][Col], int row, int col);
void SweepRadar(char Board1[Rows][Cols], char Board2[Rows][Cols], int row, int col);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dataowu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值