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

2023-10-16 08:27:19 浏览数 (2)

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

头文件:

代码语言:javascript复制
#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);

函数功能的实现:

代码语言:javascript复制
#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("您被炸死了!!nn");
					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;
		}
	}

}

主函数代码:

代码语言:javascript复制
#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("tt*******************************n");
	printf("tt************扫雷游戏***********n");
	printf("tt******** 1.play 2.exit ********n");
	printf("tt*******************************n");
	printf("tt*******************************n");

}

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


	return 0;
}

0 人点赞