C语言的扫雷简化版

C语言的扫雷

扫雷的概述

在我们许多计算机里面都有扫雷游戏,开始游戏时如果遇到地雷就会游戏结束,相反则会点开这个区域,并提示玩家周围有雷的个数,如果周围没有雷就会展开,但在我这个代码是扫雷的简化版,(其实是自己的技术还不够)不能展开也不能标记只能写一些简单的代码,在以后我会再写一篇有关扫雷的完整版。

扫雷游戏的头文件

定义一些常量便于后面的代码的编译

#pragma once
#include <stdlib.h>
#include <time.h>
#include<stdio.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//初始化雷盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//显示雷盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char board[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//排查二维数组mine[x][y]周围雷的数目返回雷的个数
int  GetMineCount(char mine[ROWS][COLS], int x, int y);

扫雷的游戏界面

首先玩家来到游戏界面输入相关的数字:1为玩游戏,2为退出游戏,若输入其他的数字则会判断为输入错误

void menu()
{
    
    
	printf("*******************************\n");
	printf("*******     1.play     ********\n");
	printf("*******     0.exit     ********\n");
	printf("*******************************\n");
}

初始化雷盘

先将数组初始化,以便后面雷的随机排放

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

显示雷盘

打印雷盘,让雷盘显示在玩家眼中,同时将列数和行数打印出来以方便玩家选择坐标

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

电脑随机布置雷

电脑随机布置雷,如果在坐标上有雷就会重新选择坐标

void SetMine(char board[ROWS][COLS], int row, int col)
{
    
    
	int count = EASY_COUNT;
	while (count)
	{
    
    
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] != '1')
		{
    
    
			board[x][y] = '1';
			count--;
		}
	}
}

排查雷

玩家输入一个坐标,看一个mine数组在这个坐标上是否是雷如果是就游戏结束
如果不是就检查周围雷的个数,再将该坐标周围雷的个数放置在数组show里面

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 win = 0;
	int sum = row * col - EASY_COUNT;
	int x=0, y=0;
	while (win < sum)
	{
    
    
		printf("请选择坐标>");
		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);
				break;
			}
			else
			{
    
    
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
    
    
			printf("输入的坐标错误\n");
		}
	}
	if (win == sum)
	{
    
    
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

总代码如下:

#pragma once
#include <stdlib.h>
#include <time.h>
#include<stdio.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//初始化雷盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//显示雷盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char board[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//排查二维数组mine[x][y]周围雷的数目返回雷的个数
int  GetMineCount(char mine[ROWS][COLS], int x, int y);
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
    
    
	int i, j;
	for (i = 0; i < rows; i++)
	{
    
    
		for (j = 0; j < cols; j++)
		{
    
    
			board[i][j] = set;
		}
	}
}
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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    
    
	int i,j;
	printf("--------------------\n");
	for (int i = 0; i <= col; i++)
	{
    
    
		printf("%d  ",i);
	}
	printf("\n");
	for (i=1;i<=row;i++)
	{
    
    
		printf("%d  ",i);
		for (j = 1; j <= col; j++)
		{
    
    
			printf("%c  ",board[i][j]);
		}
		printf("\n");
	}
	printf("--------------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
    
    
	int count = EASY_COUNT;
	while (count)
	{
    
    
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] != '1')
		{
    
    
			board[x][y] = '1';
			count--;
		}
	}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    
    
	int win = 0;
	int sum = row * col - EASY_COUNT;
	int x=0, y=0;
	while (win < sum)
	{
    
    
		printf("请选择坐标>");
		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);
				break;
			}
			else
			{
    
    
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
    
    
			printf("输入的坐标错误\n");
		}
	}
	if (win == sum)
	{
    
    
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}
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, '*');
	SetMine(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);
	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:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input!=0);
	return 0;
}

执行图如下:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_49830664/article/details/119354591