扫雷游戏 未完成

 
#include <time.h>
#include <stack>

using namespace std;

struct MapInfo
{
	bool bMine;
	int nBomboNum;
	bool bShow;
	bool bFind;

	MapInfo()
	{
		bMine=bFind=bShow=false;
		nBomboNum=0;
	}
	void Reset()
	{
		bMine=bFind=bShow=false;
		nBomboNum=0;
	}
};
class MineLogic
{
public:
	MineLogic();
	~MineLogic();
private:
	MapInfo**			m_pMap;
	int					m_nMapWidth;
	int					m_nMapHeight;
	int					m_nLevel;
	int					m_nBomboNum;
	stack<int>			m_stPoint;
public:
	void				InitMap(int nWidth,int nHeight);
	void				DestroyMap();
	void				ResetMap();
	void				RandomMap();
	void				SetLevel(int nLevel);
	void				ShowMap();
	bool				Clicked(int nRow,int nCol);
	void				ShowUserMap();
	void				SetFind(int nRow,int nCol,bool b);
	bool				AllFind();
	bool				GetFind(int nRow,int nCol);
public:
	const MapInfo**		GetMap(){return (const MapInfo**)m_pMap;}
	int					GetMapHeight(){return m_nMapHeight;}
	int					GetMapWidth(){return m_nMapWidth;}
};

#include "MineLogic.h"

MineLogic::MineLogic()
{
	m_pMap=NULL;
	m_nMapHeight=m_nMapWidth=0;
	m_nLevel=0;//初级 20% 中级40% 高级60%
	m_nBomboNum=0;
}

MineLogic::~MineLogic()
{
	this->DestroyMap();
}

void				MineLogic::InitMap(int nWidth,int nHeight)
{
	this->m_nMapWidth=nWidth;
	this->m_nMapHeight=nHeight;

	if(!m_pMap)
	{
		m_pMap=new MapInfo*[nHeight];

		for(int i=0;i<nHeight;++i)
		{
			*(m_pMap+i)=new MapInfo[nWidth];
		}
	}
}

void				MineLogic::ResetMap()
{
	if(!m_pMap)		return;

	for(int i=0;i<this->m_nMapHeight;++i)
	{
		memset(*(m_pMap+i),0,sizeof(MapInfo)*m_nMapWidth);
	}
}

void				MineLogic::SetLevel(int nLevel)
{
	this->m_nLevel=nLevel;
	m_nLevel=(int)m_nMapHeight*m_nMapWidth*nLevel/10;

	// 	switch (nLevel)
	// 	{
	// 	case 0:
	// 		{
	// 			m_nLevel=(int)m_nMapHeight*m_nMapWidth*0.1f;
	// 		}
	// 		break;
	// 	case 1:
	// 		{
	// 			m_nLevel=(int)m_nMapHeight*m_nMapWidth*0.3f;
	// 		}
	// 		break;
	// 	case 2:
	// 		{
	// 			m_nLevel=(int)m_nMapHeight*m_nMapWidth*0.5f;
	// 		}
	// 		break;
	// 	}
}

void				MineLogic::DestroyMap()
{
	for(int i=0;i<this->m_nMapHeight;++i)
	{
		delete[](*(m_pMap+i));
	}
	delete[]m_pMap;
}

void				MineLogic::RandomMap()
{
	if(!m_pMap)
	{
		return;
	}

	this->ResetMap();

	//srand((unsigned int)time(NULL));
	srand(1);
	int n=0;
	int x,y;

	for(int i=0;i<m_nLevel;++i)
	{
		if(!m_pMap[(int)(m_nMapHeight*rand()/(RAND_MAX+1.0))][(int)(m_nMapHeight*rand()/(RAND_MAX+1.0))].bMine)
		{
			m_pMap[(int)(m_nMapHeight*rand()/(RAND_MAX+1.0))][(int)(m_nMapHeight*rand()/(RAND_MAX+1.0))].bMine=true;
		}
		else
		{
			--i;
		}
	}

	for(int i=0;i<m_nMapHeight;++i)
	{
		for(int j=0;j<m_nMapWidth;++j)
		{
			if(m_pMap[i][j].bMine)
			{
				m_pMap[i][j].nBomboNum=n;
				continue;
			}
			else
			{
				x=i-1;y=j-1;
				if(x>=0&&y>=0)
				{
					if(m_pMap[x][y].bMine)
					{
						++n;
					}
				}
				x=i-1;y=j;
				if(x>=0)
				{
					if(m_pMap[x][y].bMine)
					{
						++n;
					}
				}
				x=i-1;y=j+1;
				if(x>=0&&y<m_nMapWidth)
				{
					if(m_pMap[x][y].bMine)
					{
						++n;
					}
				}
				x=i;y=j-1;
				if(y>=0)
				{
					if(m_pMap[x][y].bMine)
					{
						++n;
					}
				}
				x=i;y=j+1;
				if(y<m_nMapWidth)
				{
					if(m_pMap[x][y].bMine)
					{
						++n;
					}
				}
				x=i+1;y=j-1;
				if(x<m_nMapHeight&&y>=0)
				{
					if(m_pMap[x][y].bMine)
					{
						++n;
					}
				}
				x=i+1;y=j;
				if(x<m_nMapHeight)
				{
					if(m_pMap[x][y].bMine)
					{
						++n;
					}
				}
				x=i+1;y=j+1;
				if(x<m_nMapHeight&&y<m_nMapWidth)
				{
					if(m_pMap[x][y].bMine)
					{
						++n;
					}
				}
				m_pMap[i][j].nBomboNum=n;
			}
			n=0;
		}
	}
}

bool				MineLogic::Clicked(int nRow,int nCol)
{
	m_pMap[nRow][nCol].bShow=true;

	if(m_pMap[nRow][nCol].bMine)
	{

		return false;
	}

	if(m_pMap[nRow][nCol].nBomboNum!=0)
	{
		return true;
	}

	int x,y;

	x=nRow-1;y=nCol-1;
	if(x>=0&&y>=0)
	{
		if(!m_pMap[x][y].bShow&&
			m_pMap[x][y].nBomboNum==0&&
			!m_pMap[x][y].bMine)
		{
			m_pMap[x][y].bShow=true;
			m_stPoint.push(x);
			m_stPoint.push(y);
		}
	}
	x=nRow-1;y=nCol;
	if(x>=0)
	{
		if(!m_pMap[x][y].bShow&&
			m_pMap[x][y].nBomboNum==0&&
			!m_pMap[x][y].bMine)
		{
			m_pMap[x][y].bShow=true;
			m_stPoint.push(x);
			m_stPoint.push(y);
		}
	}
	x=nRow-1;y=nCol+1;
	if(x>=0&&y<m_nMapWidth)
	{
		if(!m_pMap[x][y].bShow&&
			m_pMap[x][y].nBomboNum==0&&
			!m_pMap[x][y].bMine)
		{
			m_pMap[x][y].bShow=true;
			m_stPoint.push(x);
			m_stPoint.push(y);
		}
	}
	x=nRow;y=nCol-1;
	if(y>=0)
	{
		if(!m_pMap[x][y].bShow&&
			m_pMap[x][y].nBomboNum==0&&
			!m_pMap[x][y].bMine)
		{
			m_pMap[x][y].bShow=true;
			m_stPoint.push(x);
			m_stPoint.push(y);
		}
	}
	x=nRow;y=nCol+1;
	if(y<m_nMapWidth)
	{
		if(!m_pMap[x][y].bShow&&
			m_pMap[x][y].nBomboNum==0&&
			!m_pMap[x][y].bMine)
		{
			m_pMap[x][y].bShow=true;
			m_stPoint.push(x);
			m_stPoint.push(y);
		}
	}
	x=nRow+1;y=nCol-1;
	if(x<m_nMapHeight&&y>=0)
	{
		if(!m_pMap[x][y].bShow&&
			m_pMap[x][y].nBomboNum==0&&
			!m_pMap[x][y].bMine)
		{
			m_pMap[x][y].bShow=true;
			m_stPoint.push(x);
			m_stPoint.push(y);
		}
	}
	x=nRow+1;y=nCol;
	if(x<m_nMapHeight)
	{
		if(!m_pMap[x][y].bShow&&
			m_pMap[x][y].nBomboNum==0&&
			!m_pMap[x][y].bMine)
		{
			m_pMap[x][y].bShow=true;
			m_stPoint.push(x);
			m_stPoint.push(y);
		}
	}
	x=nRow+1;y=nCol+1;
	if(x<m_nMapHeight&&y<m_nMapWidth)
	{
		if(!m_pMap[x][y].bShow&&
			m_pMap[x][y].nBomboNum==0&&
			!m_pMap[x][y].bMine)
		{
			m_pMap[x][y].bShow=true;
			m_stPoint.push(x);
			m_stPoint.push(y);
		}
	}

	int preSize=m_stPoint.size();
	while(m_stPoint.size()!=0)
	{
		nCol=m_stPoint.top();
		m_stPoint.pop();
		nRow=m_stPoint.top();
		m_stPoint.pop();

		preSize=m_stPoint.size();
		x=nRow-1;y=nCol-1;
		if(x>=0&&y>=0)
		{
			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum==0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
				m_stPoint.push(x);
				m_stPoint.push(y);
			}

			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum!=0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
			}
		}
		x=nRow-1;y=nCol;
		if(x>=0)
		{
			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum==0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
				m_stPoint.push(x);
				m_stPoint.push(y);
			}

			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum!=0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
			}
		}
		x=nRow-1;y=nCol+1;
		if(x>=0&&y<m_nMapWidth)
		{
			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum==0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
				m_stPoint.push(x);
				m_stPoint.push(y);
			}

			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum!=0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
			}
		}
		x=nRow;y=nCol-1;
		if(y>=0)
		{
			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum==0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
				m_stPoint.push(x);
				m_stPoint.push(y);
			}

			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum!=0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
			}
		}
		x=nRow;y=nCol+1;
		if(y<m_nMapWidth)
		{
			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum==0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
				m_stPoint.push(x);
				m_stPoint.push(y);
			}

			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum!=0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
			}
		}
		x=nRow+1;y=nCol-1;
		if(x<m_nMapHeight&&y>=0)
		{
			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum==0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
				m_stPoint.push(x);
				m_stPoint.push(y);
			}

			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum!=0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
			}
		}
		x=nRow+1;y=nCol;
		if(x<m_nMapHeight)
		{
			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum==0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
				m_stPoint.push(x);
				m_stPoint.push(y);
			}

			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum!=0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
			}
		}
		x=nRow+1;y=nCol+1;
		if(x<m_nMapHeight&&y<m_nMapWidth)
		{
			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum==0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
				m_stPoint.push(x);
				m_stPoint.push(y);
			}

			if(!m_pMap[x][y].bShow&&
				m_pMap[x][y].nBomboNum!=0&&
				!m_pMap[x][y].bMine)
			{
				m_pMap[x][y].bShow=true;
			}
		}
	}
	return true;
}

void				MineLogic::ShowMap()
{
	for(int i=0;i<m_nMapWidth;++i)
	{
		for(int j=0;j<m_nMapHeight;++j)
		{
			if(m_pMap[i][j].bMine)
			{
				printf("	B");
			}
			else
			{
				printf("	%d",m_pMap[i][j].nBomboNum);
			}
		}
		printf("\n");
	}
	printf("\n\n");
}

void				MineLogic::ShowUserMap()
{
	for(int i=0;i<m_nMapWidth;++i)
	{
		for(int j=0;j<m_nMapHeight;++j)
		{
			if(m_pMap[i][j].bShow)
			{
				if(m_pMap[i][j].bMine)
				{
					printf("	B");
				}
				else
				{
					printf("	%d",m_pMap[i][j].nBomboNum);
				}
			}
			else
			{
				printf("	X");
			}

		}
		printf("\n");
	}
	printf("\n\n");
}


void			MineLogic::SetFind(int nRow,int nCol,bool b)
{
	if(m_pMap[nRow][nCol].bMine)
	{
		m_pMap[nRow][nCol].bFind=b;
	}
}

bool			MineLogic::AllFind()
{
	for(int i=0;i<m_nMapHeight;++i)
	{
		for(int j=0;j<m_nMapWidth;++j)
		{
			if(!m_pMap[i][j].bFind&&
				m_pMap[i][j].bMine)
			{
				return false;
			}
		}
	}
	return true;
}

bool			MineLogic::GetFind(int nRow,int nCol)
{
	return m_pMap[nRow][nCol].bFind;
}

class MineGame:public SGameBase
{
public:
	MineGame();
	~MineGame(){}
private:
	MineLogic m;
protected:
	virtual bool                    UserInitial();  
	virtual void                    UserUninitial();  
	virtual void                    UserFrameFunc();  
	virtual void                    UserRenderFunc(); 
	virtual bool                    UserProcCommand(); 
private:
	HTEXTURE						m_hBack;
	hgeSprite*						m_pSpBack;
	hgeSprite*						m_arrBtnMax[4];
	hgeSprite*						m_arrBtnNom[4];
};

MineGame::MineGame():SGameBase("Mine",600,450)
{
	m.InitMap(8,8);
	m.SetLevel(1);
	m.RandomMap();
}

bool			MineGame::UserInitial()
{
	m_hBack=0;
	m_hBack=m_pHGE->Texture_Load("bg1.png");

	m_pSpBack=NULL;
	m_pSpBack=new hgeSprite(m_hBack,0,0,600,450);

	HTEXTURE hMax=m_pHGE->Texture_Load("maxbutton.png");
	HTEXTURE hNom=m_pHGE->Texture_Load("close.png");
	for(int i=0;i<4;++i)
	{
		m_arrBtnMax[i]=new hgeSprite(hMax,0,30*i,31,30);
		m_arrBtnNom[i]=new hgeSprite(hNom,0,30*i,31,30);
	}

	return true;
}


void			MineGame::UserFrameFunc()
{
	static float fMouseX,fMouseY;
	static int	row,col;

	m_pHGE->Input_GetMousePos(&fMouseX,&fMouseY);

	if(m_pHGE->Input_KeyUp(HGEK_LBUTTON))
	{
		if(fMouseX>=0&&
			fMouseX<=m.GetMapWidth()*40&&
			fMouseY>=0&&
			fMouseY<=m.GetMapHeight()*40)
		{
			row=fMouseX/40;
			col=fMouseY/40;
			if(!m.Clicked(row,col))
			{
				MessageBoxA(m_hWin,"GAME OVER","DEAD",MB_OK);
				m.RandomMap();
			}
		}
	}

	if(m_pHGE->Input_KeyUp(HGEK_RBUTTON))
	{
		if(fMouseX>=0&&
			fMouseX<=m.GetMapWidth()*40&&
			fMouseY>=0&&
			fMouseY<=m.GetMapHeight()*40)
		{
			row=fMouseX/40;
			col=fMouseY/40;
			m.SetFind(row,col,!m.GetFind(row,col));
			if(m.AllFind())
			{
				MessageBoxA(m_hWin,"YOU WIN","WIN",MB_OK);
				m.RandomMap();
			}
		}
	}
}

void			MineGame::UserUninitial()
{

}

void			MineGame::UserRenderFunc()
{
	m_pSpBack->Render(0,0);

	for(int i=0;i<m.GetMapHeight();++i)
	{
		for(int j=0;j<m.GetMapWidth();++j)
		{
			if(!m.GetMap()[i][j].bShow)
			{
				if(m.GetMap()[i][j].bFind)
				{
					m_arrBtnNom[3]->Render(i*40,j*40);
				}
				else
				{
					m_arrBtnMax[0]->Render(i*40,j*40);
				}
			}
			else if(m.GetMap()[i][j].bMine)
			{
				m_arrBtnNom[0]->Render(i*40,j*40);
			}
			else
			{
				m_arrBtnMax[3]->Render(i*40,j*40);
			}
		}
	}
}

bool			MineGame::UserProcCommand()
{
	return false;
}

你可能感兴趣的:(游戏,delete,Class,include,float,IM)