33.【C语言】实践扫雷游戏

预备知识:

第13篇 一维数组 第13.5篇 二维数组

第28篇 库函数 第29篇 自定义函数 第30篇 函数补充

0x1游戏的运行:

1.随机布置雷

2.排雷

基本规则:

点开一个格子后,显示1,对于9*9,代表以1为中心的去心九宫格内有一个雷,找到后标记

,直到标完所有的雷,游戏结束;如果中途点中雷,游戏结束

0x2.编写过程

现创建9*9的雷区,编写代码

但这样会程出现问题,边缘的雷需要另外写代码(否则九宫格越界)-->为了方便,扩大一圈(11*11)

定义二维数组:char board[11][11]

两个二维数组:一个布雷,一个显示玩家排雷的状态

game.h

cpp 复制代码
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 10
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
void menu();
void game();
void InitBoard(char board[ROWS][COLS], int row, int col, char set);
void SetMine(char board[ROWS][COLS], int row, int col);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int GetMineCount(char mine[ROWS][COLS], int x, int y);

DisplayBoard.c

cpp 复制代码
#include "game.h"
void DisplayBoard(char board[ROWS][COLS],int row,int col)
{
	printf("-------99扫雷-------\n");
	printf("0 ");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ", i);//打印行
	}
	printf("y");
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ", i);//打印列
		for (int j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("x");
	printf("\n--------------------\n");
}

FineMine.c

cpp 复制代码
#include "game.h"
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-COUNT)
	{
		
		//DisplayBoard(mine, ROW, COL);//作弊模式:)
		printf("输入排查雷的坐标 x y :>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("这是雷,很遗憾游戏结束\n");
				DisplayBoard(mine, ROW, COL);
				Sleep(3000);
				system("cls");
				break;
			}
			else
			{
				win++;
				system("cls");
				int sum = GetMineCount(mine, x, y);//t统计去心九宫格雷的个数
				show[x][y] = sum + '0';
				
				DisplayBoard(show, ROW, COL);
			}
		}

		else
		{
			printf("输入有误,重新输入\n");
		}

	}
	if (win== row * col - COUNT)
		printf("赢了\n");
}

game.c

cpp 复制代码
#include "game.h"
void game()
{
	char mine[ROWS][COLS] = { 0 };//定义布雷数组
	char show[ROWS][COLS] = { 0 };//定义显示玩家排雷的状态的数组
	InitBoard(mine, ROWS, COLS, '0');//初始化布雷数组
	InitBoard(show, ROWS, COLS, '?');//初始化显示玩家排雷的状态的数组
	SetMine(mine, ROW,COL);//布雷
	DisplayBoard(show, ROW, COL);//打印显示玩家排雷的状态的数组
	FindMine(mine,show,ROW,COL);//玩家找雷
}

GetMineCount.c

cpp 复制代码
#include "game.h"
int GetMineCount(char mine[ROWS][COLS],int x,int y)
{
	return mine[x][y + 1] + mine[x][y - 1] + mine[x - 1][y] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x + 1][y - 1] - 8 * '0';
}

InitBoard.c

cpp 复制代码
#include "game.h"
//初始化雷区
void InitBoard(char board[ROWS][COLS], int row, int col, char set)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = set;
		}
	}
}

main.c

cpp 复制代码
#include "game.h"
int main()
{
	int tmp = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();//调用菜单函数
		scanf("%d", &tmp);
		switch (tmp)
		{
		case 2:
			break;
		default:
		{
			printf("输入错误,重新选择!\n");
			Sleep(1000);
			system("cls");
			break;
		}
		  case 1:
		  {
			  system("cls");
			  game();
			  break;
		  }

		}

	} 
	while (tmp != 2);
	return 0;
}

menu.c

cpp 复制代码
#include "game.h"
void menu()//主菜单函数
{
	printf("**********************************************\n");
	printf("*******************1.PLAY********************\n");
	printf("*******************2.EXIT*********************\n");
	printf("**********************************************\n");
	printf("请选择:\n");
}

SetMine.c

cpp 复制代码
#include "game.h"
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = 10;
	while (count)
	{
		int x = rand() % row + 1;//x:1~9
		int y = rand() % col + 1;//y:1~9
		if (board[x][y] != '1')
		{
			board[x][y] = '1';
			count--;
		}
	}

}

有兴趣可以看看 CE实现扫雷作弊

相关推荐
摸鱼的春哥1 天前
10年3次大失败,他从“罪人”输成了中年人的“白月光”
游戏
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
echoarts1 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix1 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题1 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说1 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔1 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔1 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing102329291 天前
Day03_刷题niuke20250915
c语言
我是菜鸟0713号1 天前
Qt 中 OPC UA 通讯实战
开发语言·qt