⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shaolei.cpp

📁 一个挖雷的小程序
💻 CPP
字号:
#include "stdafx.h"
#include "shaolei.h"
#include "MFC挖雷.h"
extern int XDIVISIONS;
extern int YDIVISIONS;
extern int LEISHU;
int ShaoLei::count=0;
void ShaoLei::SetMap(int i,int j,int e)
{
	int k=CountCost[i][j];
	MapCost[i][j]=e;
	if (e==2)
	{
		if (LeiCost[i][j]==2) return;
		else if (k==0)
		{
			if (i>0) 
				if (LeiCost[i-1][j]==0 && MapCost[i-1][j]!=2 && MapCost[i-1][j]!=3) 
				{
					MapCost[i-1][j]=2;
				}
			if (i<XDIVISIONS-1)
				if (LeiCost[i+1][j]==0 && MapCost[i+1][j]!=2 && MapCost[i+1][j]!=3) 
				{
					MapCost[i+1][j]=2;
				}
			if (j>0) 
				if (LeiCost[i][j-1]==0 && MapCost[i][j-1]!=2 && MapCost[i][j-1]!=3)
				{
					MapCost[i][j-1]=2;
				}
			if (j<YDIVISIONS)
				if (LeiCost[i][j+1]==0 && MapCost[i][j+1]!=2 && MapCost[i][j+1]!=3) 
				{					
					MapCost[i][j+1]=2;
				}	
		}
	}
}
ShaoLei::~ShaoLei()
{
	if (MapCost!=0) 
		delete[] MapCost;
	if (!LeiCost!=0) 
		delete[] LeiCost;
	if (!CountCost!=0)
		delete[] CountCost;
}
	
void ShaoLei::InitMap()
{
	int i,j,k;
	count=0;
	srand((unsigned)time(NULL));
	End=0;
	if (!MapCost)
		delete[] MapCost;
	if (!LeiCost) 
		delete[] LeiCost;
	if (!CountCost) 
		delete[] CountCost;
	MapCost=new int*[XDIVISIONS];
	LeiCost=new int*[XDIVISIONS];
	CountCost=new int*[XDIVISIONS];
	for (i=0;i<XDIVISIONS;i++)
	{
		MapCost[i]=new int[YDIVISIONS];
		LeiCost[i]=new int[YDIVISIONS];
		CountCost[i]=new int[YDIVISIONS];
	}
	for (i=0;i<XDIVISIONS;i++)
		for (j=0;j<YDIVISIONS;j++)
			MapCost[i][j]=LeiCost[i][j]=0;
	for (k=0;k<LEISHU;k++)
	{
		i=rand()%XDIVISIONS;
		j=rand()%YDIVISIONS;
		j=j%XDIVISIONS;
		i=i%YDIVISIONS;
		while (LeiCost[i][j]!=0)
		{
			j=rand()%XDIVISIONS;
			i=rand()%YDIVISIONS;
		}
		LeiCost[i][j]=1;

	}
	for (i=0;i<XDIVISIONS;i++)
		for (j=0;j<YDIVISIONS;j++)
		{
			CountCost[i][j]=0;
			if (i>0 && j>0) 
				if (LeiCost[i-1][j-1]==1) 
					CountCost[i][j]++;
			if (i>0) 
				if (LeiCost[i-1][j]==1) 
					CountCost[i][j]++;
			if (i>0 && j<XDIVISIONS-1) 
				if (LeiCost[i-1][j+1]==1) 
					CountCost[i][j]++;
			if (i<XDIVISIONS-1)
				if (LeiCost[i+1][j]==1) 
					CountCost[i][j]++;
			if (j>0) 
				if (LeiCost[i][j-1]==1) 
					CountCost[i][j]++;
			if (j<YDIVISIONS-1)
				if (LeiCost[i][j+1]==1) 
					CountCost[i][j]++;
			if (j>0 && i<XDIVISIONS-1) 
				if (LeiCost[i+1][j-1]==1) 
					CountCost[i][j]++;
			if (i<XDIVISIONS-1 && j<YDIVISIONS-1) 
				if (LeiCost[i+1][j+1]==1) 
					CountCost[i][j]++;
		}
	
		
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -