初学者指南:使用C语言实现简易版扫雷游戏

 

目录

1.介绍

描述扫雷游戏及它作为编程的价值

我们要解决的问题

*布置的雷随机

*switch...case语句

*do..while( );

*while语句

*if..else语句

*函数

2.开始使用C语言

简单介绍C语言和环境配置的相关知识

3.扫雷游戏规则

(1)

(2)​编辑

(3)

(4)

(5)

4.项目结构

使用头文件

使用源文件

5.代码

game.h     --头函数的注明和游戏测试

game.c      --游戏模块 

扫雷游戏.c --游戏模块 

游戏胜利✌结束

总结


1.介绍

描述扫雷游戏及它作为编程的价值

*实现简易版C语言扫雷

我们要解决的问题

*布置的雷随机

srand((unsigned int)time(NULL));

*switch...case语句

*do..while( );

*while语句

*if..else语句

*函数

2.开始使用C语言

简单介绍C语言和环境配置的相关知识

使用vs2022

3.扫雷游戏规则

(1)

进入控制台

进入menu函数

请选择1,然后按回车-->进入游戏

或者

请选择0,然后按回车-->结束游戏

(2)

(3)

该简易版扫雷游戏只有10个雷

在9*9棋盘中排查出9*9-10=71个无雷,

则为扫雷成功

(4)

排查的形式以坐标为主

正确分清行和列

形如:

请输入你要排查的坐标(6行7列):6 7

(5)

  若在排查雷中排查中了雷,

  则很遗憾,没能成功扫雷 

4.项目结构

使用头文件

game.h     --头函数的注明和游戏测试

使用源文件

game.c      --游戏模块

扫雷游戏.c --游戏模块

5.代码

game.h     --头函数的注明和游戏测试

#pragma once

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

#define EASY_COUNT 10
//设置为10个雷
#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

//初始化棋盘
void Key_board(char arr[ROWS][COLS],int rows,int cols,char set);

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

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

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

game.c      --游戏模块 

#include<stdio.h>
#include"game.h"

void Key_board(char arr[ROWS][COLS], int rows, int cols,char set)
{
	int i = 0;
	//控制行
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		//控制列
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = set;
		}
	}

}

void Display_board(char arr[ROWS][COLS], int row, int col)
{
	int i = 1;
	//先打印列号
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");

	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");
	}
}

void SetMine(char arr[ROWS][COLS], int row, int col)
{
	//布置10个雷
	int count = EASY_COUNT;
	while (count)
	{
		int x = rand() % row + 1;//1~9
		int y = rand() % col + 1;//1~9

		//布置好雷,count--
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
}

static int GetMinecount(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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x=0, y=0;
	int win = 0;
	while (win<row*col-EASY_COUNT)
	{
		printf("请输入要排查的坐标:");
		scanf("%d %d", &x, &y);

		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了\n");
				Display_board(mine, ROW, COL);
				break;
			}
			else
			{
				//不是雷,就统计周围有几个雷
				int n = GetMinecount(mine, x, y);
				show[x][y] = n + '0';
				Display_board(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
			break;
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功!!!\n");
	}
}

扫雷游戏.c --游戏模块 

#include<stdio.h>
#include "game.h"

void menu()
{
	printf("*************************\n");
	printf("********1. 开始 **********\n");
	printf("********0. 结束 **********\n");
	printf("*************************\n");
}

void game()
{
	char mine[ROWS][COLS];
	char show[ROWS][COLS];

	//初始化棋盘
	Key_board(mine,ROWS,COLS,'0');//'0'
	Key_board(show, ROWS, COLS,'*');//'*'

	//打印棋盘
	Display_board(show, ROW, COL);
	//Display_board(mine, ROW, COL);
	//1.布置雷
	SetMine(mine, ROW, COL);
	//Display_board(mine, ROW, COL);

	//2.排查雷
	FindMine(mine,show, ROW, COL);
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));

	do
	{
		menu();
		printf("请选择-->");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("********扫雷********\n");
			game();
			printf("********扫雷********\n");
			printf("\n");
			break;
		case 0:
			printf("结束游戏\n");
			break;
		default:
			printf("选择错误,请重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

游戏胜利✌结束

控制台显示 

总结

1.该扫雷游戏使用了数组和函数等主要内容的知识来实现

2.实现该游戏布置的雷使用了随机值

3.在数组中使用了以中心坐标标记周围雷的数量

网页版在线扫雷:http://www.minesweeper.cn/

感谢你的浏览和阅读,创作不易,你的点赞和关注是我持续写作的动力!!!

谢谢!!!

  • 31
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值