C/关于扫雷小游戏的创建

本文是用C语言写的N子棋小游戏

头文件:

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

#define Line 9		//行
#define Column 9	//列

//实际数组
#define Line1 Line+2		//行
#define Column1 Column+2	//列

#define Easy 10 //简易模式

//初始化棋盘
void blank_board(char board[Line1][Column1],int line1,int column1, char set);

//打印棋盘
void print_board(char board[Line1][Column1], int line, int column);

//生成雷
void build_board(char board[Line1][Column1], int line, int column);

//判断玩家输赢
void is_win_board(char board[Line1][Column1], char board1[Line1][Column1], int line, int column);

函数功能的实现:

#include"game.h"

//累计雷的个数
//game.c 源文件的专属函数
int cumulative(char board[Line1][Column1], int x, int y)
{
	//字符转数字 - '0' |  0--'0'(48)
	return (board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1]
		+ board[x][y - 1] + board[x][y + 1] + board[x + 1][y - 1]
		+ board[x + 1][y] + board[x + 1][y + 1] - 8 * '0');
}


//判断玩家输赢
void is_win_board(char board[Line1][Column1], char board1[Line1][Column1], int line, int column)
{
	int x = 0;
	int y = 0;
	//雷的个数
	int count = Easy;
	//没有雷的地方的个数
	int count1 = line * column - count;
	int number = 0;

	
	while (count1)
	{
		printf("请输入您选择的下标:");
		scanf("%d %d", &x, &y);

		if (x >= 1 && x <= line
			&& y >= 1 && y <= column)
		{
			if (board1[x][y] == '*')
			{
				if (board[x][y] == '0')
				{
					number = cumulative(board, x, y);
					board1[x][y] = number + '0';
					system("cls");
					print_board(board1, Line, Column);
				}
				else if (board[x][y] == '1')
				{
					printf("您被炸死了!!\n\n");
					print_board(board, Line, Column);

					break;
				}
			}
			else
			{
				printf("该坐标,您已排查过,请重新输入!\n");
				continue;
			}
		}
		else
		{
			printf("您的输入有误,请重新输入:");
			continue;
		}
		count1--;
	}

	if (count1 == 0)
	{
		printf("恭喜您,扫雷成功!!\n");
		print_board(board, Line, Column);
	}

}

//生成雷
void build_board(char board[Line1][Column1], int line, int column)
{
	//生成行 1-line  列 1-column中的雷
	int x = 0;
	int y = 0;
	int count = 0;
	while (1)
	{
		x = rand() % line + 1;
		y = rand() % column + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count++;
		}
		if (count == Easy)
		{
			break;
		}
	}
}


//打印棋盘
void print_board(char board[Line1][Column1], int line, int column)
{
	int i = 0;
	int j = 0;

	printf("------------扫雷---------------\n");
	for (i = 0;i <= line;i++)
	{
		printf("%d ", i);
	}
	printf("\n");

	for (i = 1;i <= line;i++)
	{
		printf("%d ", i);
		for (j = 1;j <= column;j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}

}

//初始化棋盘
void blank_board(char board[Line1][Column1], int line1, int column1, char set)
{
	int i = 0;
	int j = 0;

	for (i = 0;i < line1;i++)
	{
		for (j = 0;j < column1;j++)
		{
			board[i][j] = set;
		}
	}

}

主函数代码:

#include"game.h"

void menu1();//主界面
void Choice();//选择函数
void Game();//游戏函数
void menu2();//选择是否再玩一次游戏

void Game()
{
	srand((unsigned int)time(NULL));
	system("cls");
	//存储雷
	char board[Line1][Column1];
	//打印,存储雷个数  玩家实际操作的棋盘
	char board1[Line1][Column1];

	printf("\t欢迎来到扫雷游戏!!!\n");

	//初始化棋盘
	blank_board( board, Line1, Column1,'0');
	blank_board( board1, Line1, Column1,'*');
	//打印棋盘
	print_board( board1, Line, Column);
	//生成雷
	build_board(board, Line, Column);
	//判断玩家输赢
	is_win_board(board, board1, Line, Column);



}

void Choice()//选择函数
{
	int choice = 0;
	printf("请选择:");

	do
	{
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			Game();
			break;
		case 2:
			printf("已退出游戏....");
			break;
		default:
			printf("输入错误,请重新输入:");
			break;
		}

		menu2();//选择是否再玩一次游戏

	} while (choice!=2);

}

void menu2()//选择是否再玩一次游戏
{
	printf("\t您是否再玩一次扫雷游戏?\n");
	printf("\t 1.play 2.exit \n");
	printf("请选择:");
}

void menu1()//主界面
{
	printf("\t\t*******************************\n");
	printf("\t\t************扫雷游戏***********\n");
	printf("\t\t******** 1.play 2.exit ********\n");
	printf("\t\t*******************************\n");
	printf("\t\t*******************************\n");

}

int main()
{
	menu1();//主界面
	Choice();//选择函数


	return 0;
}

  • 15
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Y君的进化史

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

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

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

打赏作者

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

抵扣说明:

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

余额充值