java九宫格棋代码 java九宫格程序( 二 )


类结构如下:
class CNineGird
{
public:
struct PlaceList
{
DWORD Place;
PlaceList* Left;
PlaceList* Right;
};
struct Scanbuf
{
DWORD Place;
int ScanID;
};
struct PathList
{
unsigned char Path[9];
};
private:
PlaceList *m_pPlaceList;
Scanbuf *m_pScanbuf;
RECT m_rResetButton;
RECT m_rAutoButton;
public:
int m_iPathsize;
clock_t m_iTime;
UINT m_iStepCount;
unsigned char m_iTargetChess[9];
unsigned char m_iChess[9];
HWND m_hClientWin;
PathList *m_pPathList;
bool m_bAutoRun;
private:
inline bool AddTree(DWORD place , PlaceList* parent);
void FreeTree(PlaceList* parent);
inline void ArrayToDword(unsigned char *array , DWORDdata);
inline void DwordToArray(DWORD data , unsigned char *array);
inline bool MoveChess(unsigned char *array , int way);
bool EstimateUncoil(unsigned char *array);
void GetPath(UINT depth);
public:
void MoveChess(int way);
bool ComputeFeel();
void ActiveShaw(HWND hView);
void DrawGird(HDC hDC , RECT clientrect);
void DrawChess(HDC hDC , RECT clientrect);
void Reset();
void OnButton(POINT pnt , HWND hView);
public:
CNineGird();
~CNineGird();
};
计算随机随机数组使用了vector模板用random_shuffle(,)函数来打乱数组数据,并计算目标结果是什么 。代码:
void CNineGird::Reset()
{
if(m_bAutoRun) return;
vector vs;
int i;
for (i = 1 ; i9 ; i ++)
vs.push_back(i);
vs.push_back(0);
random_shuffle(vs.begin(), vs.end());
random_shuffle(vs.begin(), vs.end());
for ( i = 0 ; i9 ; i ++)
{
m_iChess[i] = vs[i];
}
if (!EstimateUncoil(m_iChess))
{
unsigned char array[9] = {1,2,3,8,0,4,7,6,5};
memcpy(m_iTargetChess , array , 9);
}
else
{
unsigned char array[9] = {1,2,3,4,5,6,7,8,0};
memcpy(m_iTargetChess , array , 9);
}
m_iStepCount = 0;
}
数据压缩函数实现:
inline void CNineGird::ArrayToDword(unsigned char *array , DWORD data)
{
unsigned char night = 0;
for ( int i = 0 ; i9 ; i ++)
{
if (array[i] == 8)
{
night = (unsigned char)i;
break;
}
}
array[night] = 0;
data = https://www.04ip.com/post/0;
data = https://www.04ip.com/post/(DWORD)((DWORD)array[0]29 | (DWORD)array[1]26 |
(DWORD)array[2]23 | (DWORD)array[3]20 |
(DWORD)array[4]17 | (DWORD)array[5]14 |
(DWORD)array[6]11 | (DWORD)array[7]8 |
(DWORD)array[8]5 | night);
array[night] = 8;
}
解压缩时跟压缩正好相反,解压代码:
inline void CNineGird::DwordToArray(DWORD data , unsigned char *array)
{
unsigned char chtem;
for ( int i = 0 ; i9 ; i ++)
【java九宫格棋代码 java九宫格程序】{
chtem = (unsigned char)(data(32 - (i + 1) * 3)0x00000007);
array[i] = chtem;
}
chtem = (unsigned char)(data0x0000001F);
array[chtem] = 8;
}
由于可扩展的数据量非常的大,加上在保存的时候使用的是DWORD类型,将每一步数据都记录在一个排序二叉树中,按从小到大从左到有的排列,搜索的时候跟每次搜索将近万次的形式比较快几乎是N次方倍,把几个在循环中用到的函数声明为内联函数 , 并在插入的时候同时搜索插入的数据会不会在树中有重复来加快总体速度 。二叉树插入代码:
inline bool CNineGird::AddTree(DWORD place , PlaceList* parent)
{
if (parent == NULL)
{
parent = new PlaceList();
parent-Left = parent-Right = NULL;
parent-Place = place;
return true;

推荐阅读