扫雷小游戏

头文件代码,存储函数的声明,与一些库函数的头文件

#define _CRT_SECURE_NO_WARNINGS 1

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

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2
#define DEFAULT_COUNT 30//默认雷数

void init_board(char board[ROWS][COLS], int rows, int cols,char set);//初始化

void display_board(char board[ROWS][COLS], int row, int col);//打印棋盘

void set_mine(char mine[ROWS][COLS], int row, int col);//布置雷

void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//排雷

扫雷代码:调用菜单函数,运用循环完成游戏的进行与退出

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void menu()
{
	printf(" 0.退出! \n");
	printf(" 1.play!\n");
	printf(" 2.秘诀!\n");
}

void game()
{
	char mine[ROWS][COLS]={0};
	char show[ROWS][COLS]={0};
	//初始化数组 mine全为0;show全为*;
	init_board(mine, ROWS, COLS, '0');
	init_board(show, ROWS, COLS, '*');

	//打印棋盘
	//display_board(mine, ROW, COL);
	//display_board(show, ROW, COL);

	//布置雷
	set_mine(mine, ROW, COL);

	//排雷
	printf("\n");
	display_board(show, ROW, COL);
	find_mine(mine, show, ROW, COL);
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));//设置时间戳使rand随机数生效
	do
	{
		menu();
		printf("请输入(1/0): ");
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			printf(" 离开!\n ");
			break;
		case 1:
			game();
			break;
		case 2:
			printf(" V我50,我告诉你扫雷秘诀\n\n\n");
			break;
		default :
			printf(" 输错了 \n");
			break;


		}
	} while (input);
	return 0;
}

游戏运行时需要调用的函数代码

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void init_board(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

void display_board(char board[ROWS][COLS], int row, int col)
{
	int i = 0, j = 0;
	//列号
	for (j = 0; j <= col; j++)
	{
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);//行号
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void set_mine(char mine[ROWS][COLS], int row, int col)
{
	//布置雷数:DEFAULT_COUNT
	int count = DEFAULT_COUNT;
	while (count)
	{
		int x = rand() % row + 1;//%row==%9 得到0~8
		int y = rand() % row + 1;

		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

int get_mine_count(char mine[ROWS][COLS], int x, int y)//统计坐标周围有多少雷
{
	return mine[x - 1][y] +
		mine[x - 1][y - 1] +
		mine[x][y - 1] +
		mine[x + 1][y - 1] +
		mine[x + 1][y] +
		mine[x + 1][y + 1] +
		mine[x][y + 1] +
		mine[x - 1][y + 1]-8*'0';
}

void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0, y = 0,win = 0;
	while (win < row * col - DEFAULT_COUNT)
	{
		printf("输入要查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y]=='*')//坐标是否被排查
			{
				if (mine[x][y] == '1')//坐标是否有雷
				{
					printf("很遗憾,你没了\n");
					printf("\n\n");
					display_board(mine, ROW, COL);
					break;
				}
				else
				{
					int count = get_mine_count(mine, x, y);
					show[x][y] = count + '0';//数字+字符‘0’,找到相应的字符  ‘5’-‘0’= 5
					printf("\n\n");
					display_board(show, ROW, COL);
					win++;
				}
			}
			else
			{
				printf("该位置已被排查,请重新输入\n\n\n");
			}
		}
		else
		{
			printf("输错了,重新输!\n");
		}
	}
	if (win == row * col - DEFAULT_COUNT)
	{
		printf("YOU WIN!!!\n\n\n");
		display_board(mine, ROW, COL);
	}
}

一个还算可以的小游戏,也可以自己加窗口、界面、按钮使之更完善。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值