扫雷游戏1.0

目录

扫雷游戏分为多个文件:

1. game.h —— 游戏需要的数据类型和函数声明

2. test.c —— 游戏的逻辑

3. game.c —— 游戏的实现

1. game.h —— 游戏需要的数据类型和函数声明
#pragma once
//头文件
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//定义数值
#define ROW 9
#define COL 9

//考虑边缘,扩大数组
#define ROWS ROW+2 
#define COLS COL+2

#define MineCount 10

//函数声明
//初始化
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set);

//显示
void DisplayBoard(char arr[ROWS][COLS], int rows, int cols);

//埋雷
void SetMine(char mine[ROWS][COLS], int row, int col);

//扫雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
2. test.c —— 游戏的逻辑

我们在这里定义两个数组,一个是埋雷的数组,一个是扫雷的数组。

由于数组边缘的点需要统计周围雷的个数,所以我们需要对数组进行扩张,由9*9扩张至11*11,埋雷和扫雷的区域都在9*9的数组之中。

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//打印菜单
void menu()
{
	printf("**********************\n");
	printf("******* 1.play *******\n");
	printf("******* 0.exit *******\n");
	printf("**********************\n");
}
//游戏步骤
void game()
{
	//定义两个数组:一个是埋雷的,一个是扫雷的
	char mine[ROWS][COLS];
	char show[ROWS][COLS];

	//初始化数组
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');

	//打印数组
	//DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);

	//埋雷
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);

	//扫雷
	FindMine(mine, show, ROW, COL);

}
//主程序
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL)); //rand所需要的
	do
	{
		//打印菜单
		menu();
		printf("请输入选项(1/0): ");
		scanf("%d", &input); //input输入在里面,所以不能用while语句
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("已退出游戏!\n");
			break;
		default:
			printf("输入错误,请重新输入!\n");
			break;
		}

	} while (input);

}
3. game.c —— 游戏的实现

为了可以通过一个函数对两个数组都完成初始化,在函数中设置一个char set参数,注意是字符型

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//初始化数组:11 * 11
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set) //set是字符类型
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = set;
		}
	}
}

//打印数组:9 * 9
void DisplayBoard(char arr[ROWS][COLS], int row, int col) //用row和col,不是ROW和COL
{
	int i = 0;
	//打印第一行0 1 2 3 4 5 6 7 8 9
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	//打印第二行 1 * * * * * * * * *
	//...
	for (i = 1; i <= row; i++)
	{
		int j = 0;
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]); //打印字符
		}
		printf("\n");
	}
}

//埋雷:9 * 9
void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int number = MineCount;
	while (number)
	{
		int x = rand() % ROW + 1; //x取1~9
		int y = rand() % COL + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
		}
		number--;
	}
	
}
//数雷
int CountMine(char mine[ROWS][COLS], int x, int y)
{
	return(mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0'); //将字符转换成数字
}

//扫雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	int step = 0;
	while(step <row * col - MineCount) //步数
	{
		printf("请输入:");
		scanf("%d %d", &i, &j);
		if (i >= 1 && i <= row && j >= 1 && j <= col) //如果在9 * 9的范围内
		{
			if (mine[i][j] == '1')
			{
				printf("你输了!游戏结束。\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int sum = CountMine(mine, i, j);
				show[i][j] = sum + '0'; 
				DisplayBoard(show, ROW, COL);
				step++;
			}
		}
		else
		{
			printf("输入错误!请重新输入。\n");
		}
	}
	if (step == row * col - MineCount)
	{
		printf("恭喜你,成功通过游戏啦!\n");
		DisplayBoard(mine, ROW, COL);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值