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

📄 state.cpp

📁 八数码问题求解
💻 CPP
字号:
// State.cpp: implementation of the CState class.
//
//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "Bashuma.h"
#include "State.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////



CState::CState()
{	
	m_BSelect=FALSE;    //标志次状态是否是最佳路径上的一个状态
	m_Depth=-1;
	//NextState=NULL;
	PreState=NULL;
	m_moveDirection=0;

/*
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		m_contentFlag[i][j]=FALSE;
	}*/

	
}

CState::~CState()
{

}
void  CState::DrawState(CDC *pDC,CPoint StartPt,COLORREF color)
{
	CPen Mypen(PS_SOLID,2,color);
	CPen *oldPen=pDC->SelectObject(&Mypen);
	//划横线
	for(int i=0;i<4;i++)
	{
		pDC->MoveTo(StartPt.x,   StartPt.y+i*20);
		pDC->LineTo(StartPt.x+60,StartPt.y+i*20);
	}
	//画纵线
	for(i=0;i<4;i++)
	{
		pDC->MoveTo(StartPt.x+i*20,	StartPt.y);
		pDC->LineTo(StartPt.x+i*20,StartPt.y+60);
	}

	pDC->SelectObject(oldPen);	
	Mypen.DeleteObject();
	//写数字
	CPoint pt;
	pt.x=StartPt.x;
    pt.y=StartPt.y;
	for(i=0;i<3;i++)
	{
		pt.y+=1;
		for(int j=0;j<3;j++)
		{
			pt.x+=2;
			CString strTemp("");
			if (m_content[i][j]!=0) 
			{
				strTemp.Format("%d",m_content[i][j]);
			}			
			pDC->SetBkColor(14215660);
			pDC->TextOut(pt.x,pt.y,strTemp);
			pt.x+=21;				
		}
		pt.x=StartPt.x;
		pt.y=pt.y+20;
	}	
}

void  CState::FindIndex(int Num,int &x,int &y)
{
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			if (m_content[i][j]==Num) 
			{
				x=i;
				y=j;
				return;
			}			
		}
	}	
}

BOOL CState::CompareState(CState CurState,CState GoState)
{
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			if(CurState.m_content[i][j]!=GoState.m_content[i][j])
				return FALSE;
		}
	}
	return TRUE;
}

BOOL CState::MoveLeft()
{
	if (m_moveDirection==Right)
	{
		return FALSE;
	}
	int x=0,y=0;
	FindIndex(0,x,y);
	if (y-1>=0) 
	{
		m_content[x][y]=m_content[x][y-1];
		m_content[x][y-1]=0;
		return TRUE;
	}
	return FALSE;
}


BOOL CState::MoveRight()
{
	if (m_moveDirection==Left)
	{
		return FALSE;
	}
	int x=0,y=0;
	FindIndex(0,x,y);
	if (y+1<3) 
	{
		m_content[x][y]=m_content[x][y+1];
		m_content[x][y+1]=0;
		return TRUE;
	}
	return FALSE;
}

BOOL CState::MoveUp()
{
	if (m_moveDirection==Down)
	{
		return FALSE;
	}
	int x=0,y=0;
	FindIndex(0,x,y);
	if (x-1>=0) 
	{
		m_content[x][y]=m_content[x-1][y];
		m_content[x-1][y]=0;
		return TRUE;
	}
	return FALSE;
}

BOOL CState::MoveDown()
{
	if (m_moveDirection==Up)
	{
		return FALSE;
	}
	int x=0,y=0;
	FindIndex(0,x,y);
	if (x+1<3) 
	{
		m_content[x][y]=m_content[x+1][y];
		m_content[x+1][y]=0;
		return TRUE;
	}
	return FALSE;
}

⌨️ 快捷键说明

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