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

📄 colorlinezuitwomodel.cpp

📁 使用CarbideC++编程工具
💻 CPP
字号:
/*
 ============================================================================
 Name		: ColorLinezUITwoModel.cpp
 Author	  : 
 Version	 : 1.0
 Copyright   : 
 Description : CColorLinezUITwoModel implementation
 ============================================================================
 */

#include "ColorLinezUITwoModel.h"

CColorLinezUITwoModel::CColorLinezUITwoModel()
	{
	// No implementation required
	}

CColorLinezUITwoModel::~CColorLinezUITwoModel()
	{
	iCurrentInfos.Close();
	}

CColorLinezUITwoModel* CColorLinezUITwoModel::NewLC()
	{
	CColorLinezUITwoModel* self = new (ELeave)CColorLinezUITwoModel();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CColorLinezUITwoModel* CColorLinezUITwoModel::NewL()
	{
	CColorLinezUITwoModel* self=CColorLinezUITwoModel::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CColorLinezUITwoModel::ConstructL()
	{
	iDX[0] = -1;iDX[1] = 0;iDX[2] = 1;iDX[3] = 0;
	iDY[0] = 0;iDY[1] = -1;iDY[2] = 0;iDY[3] = 1;
	
	for(short x=0;x!=7;++x)
		{
		for(short y=0;y!=7;++y)
			{
			iTable[x][y] = EFalse;
			iBlock[x][y] = EFalse;
			}
		}
	}
void CColorLinezUITwoModel::SetCurrentBitmap(TPoint aCurrentPos,CFbsBitmap* aCurrentBitmap)
	{
	TCurrentInfo CurrentPosAndBm;
	CurrentPosAndBm.iCurrentPos = aCurrentPos;
	TInt index ;
	index = iCurrentInfos.Find(CurrentPosAndBm,CColorLinezUITwoModel::Match);
	short x = (aCurrentPos.iX - 68)/29;
	short y = (aCurrentPos.iY - 49)/29;

	if(index == KErrNotFound)
		{
		CurrentPosAndBm.iCurrentBitmap = aCurrentBitmap;
		iCurrentInfos.AppendL(CurrentPosAndBm);	
		}
	else
		{
		iCurrentInfos[index].iCurrentBitmap = aCurrentBitmap;
		}
	}
CFbsBitmap* CColorLinezUITwoModel::GetCurrentBitmap(TPoint aCurrentPos)
	{
	TInt index;
    TCurrentInfo currentInfo;
	currentInfo.iCurrentPos = aCurrentPos;
	index = iCurrentInfos.Find(currentInfo,CColorLinezUITwoModel::Match);
	if(index == KErrNotFound) { return NULL;}
	currentInfo = iCurrentInfos[index];	
	return currentInfo.iCurrentBitmap;
	}
TPoint CColorLinezUITwoModel::GetCurrentBitmapByBmp(CFbsBitmap* aCurrentBitmap)
	{
	TInt index;
	TCurrentInfo currentInfo;
	currentInfo.iCurrentBitmap = aCurrentBitmap;
	index = iCurrentInfos.Find(currentInfo,CColorLinezUITwoModel::Match);
	currentInfo = iCurrentInfos[index];
	return currentInfo.iCurrentPos;
	}
TBool CColorLinezUITwoModel::Match(const TCurrentInfo& aFirstInfo,const TCurrentInfo& aSecondInfo)
	{
	if(aFirstInfo.iCurrentPos == aSecondInfo.iCurrentPos)
		return ETrue;
	return EFalse;
	}
void CColorLinezUITwoModel::SetScore(TInt aScore)
	{
	iScore = aScore;
	}
TInt CColorLinezUITwoModel::GetScore()
	{
	return iScore;
	}

//寻路方法
void CColorLinezUITwoModel::SetBlock(short aLevelX,short aLevelY)
	{
	iBlock[aLevelX][aLevelY] = ETrue;
	}

void CColorLinezUITwoModel::RemoveBlock(short aLevelX,short aLevelY)
	{
	iBlock[aLevelX][aLevelY] = EFalse;
	}
TBool CColorLinezUITwoModel::Test()
	{
	if((iLevelX == iTargetX)&&(iLevelY == iTargetY))
		{
		return ETrue;
		}
	return EFalse;
	}
TBool CColorLinezUITwoModel::ActOK()
	{	
	if(Act[iLevel] > iMaxAct)
		return EFalse;
	
	short tx = iLevelX + iDX[Act[iLevel]-1];
	short ty = iLevelY + iDY[Act[iLevel]-1];
	
	if((tx>6)||(tx<0))
		return EFalse;
	if((ty>6)||(ty<0))
		return EFalse;
	if(iBlock[tx][ty])
		return EFalse;
	if(iTable[tx][ty])
		return EFalse;
	iLevelX = tx;
	iLevelY = ty;
	iTable[iLevelX][iLevelY] = ETrue;
	return ETrue;
	}
void CColorLinezUITwoModel::Back()
	{

	iLevelX -= iDX[Act[iLevel-1]-1];
	iLevelY -= iDY[Act[iLevel-1]-1];
//	iTable[iLevelX][iLevelY] = EFalse;
	Act[iLevel] = 0;
	--iLevel;
	}
short CColorLinezUITwoModel::GetNextAct()
	{	

	t = 100; tt = 2;
	for(short i=0;i!=4;++i)
		{
		disX = iTargetX-iLevelX-iDX[i];
		disY = iTargetY-iLevelY-iDY[i];
		
		if(disX<0) { disX = -disX;}
		if(disY<0) { disY = -disY;}
		
		dis[i] =disX + disY;		
		}
	for(short i=0;i!=4;++i)
		{
		if(dis[i]<t)
			{
			order[0] = i+1;
			t = dis[i];
			}
		}
		
	if(Act[iLevel] == 0)
		return order[0];
	
	order[1] = -1;
	
	for(short i=0;i!=4;++i)
		{
		if((dis[i]==t)&&(i!=(order[0]-1)))
			{
			order[1] = i+1;
			break;
			}
		}
	if(order[1]!=-1)
		{
		for(short i=0;i!=4;++i)
			{
			if(dis[i]!=t)
				{
				order[tt]=i+1;
				++tt;
				}
			}
		}
	else
		{
		for(short i=0;i!=4;++i)
			{
			if(dis[i]!=t)
				{
				order[tt-1]=i+1;
				++tt;
				}
			}
		}	


	if(Act[iLevel]==order[0])
		return order[1];
	if(Act[iLevel]==order[1])
		return order[2];
	if(Act[iLevel]==order[2])
		return order[3];
	if(Act[iLevel]==order[3])
		return 5;
	return 5;
	}
//搜查路径
TBool CColorLinezUITwoModel::FindPath(short aLevelX,short aLevelY,short aTargetX,short aTargetY)
	{
	iLevelX = aLevelX;
	iLevelY = aLevelY;
	iTargetX = aTargetX;
	iTargetY = aTargetY;	
	iAllComplete = EFalse;
	iMaxAct = 4;
	isComplete = EFalse;

	for(short x=0;x!=7;++x)
		{
		for(short y=0;y!=7;++y)
			{
			iTable[x][y] = EFalse;	
			}
		}
	for(short i=0;i!=50;++i)
		{
		Act[i] = 0;
		}
		
	iTable[iLevelX][iLevelY] = ETrue;
	iLevel = -1;
	iAllComplete = EFalse;
	while(!iAllComplete)
		{
		++iLevel;
		iLevelComplete =EFalse;
		while(!iLevelComplete)
			{
			Act[iLevel] = GetNextAct();
			if(ActOK())
				{
				if(Test())
					{
					return ETrue;
					}
				iLevelComplete = ETrue;
				}
			else
				{
				if(iLevel<0)
					{
					iAllComplete = ETrue;
					iLevelComplete = ETrue;
					}
				if(Act[iLevel]>iMaxAct)
					Back();				
				}
			}
		}
	return EFalse;
	}

⌨️ 快捷键说明

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