#pragma once
#include
#include
using namespace std;
class Chess_MoveTree
{
//二维数组存的多叉树,标号从0开始
public:
Chess_MoveTree(void);
~Chess_MoveTree(void);
//生成多叉树,默认是生成12层的4叉树,约6百万个节点
void Create(const int in_iTreePath = 4, const int in_iLayerNum = 12);
int Path() { return m_iTreePath; }
int Size() { return MoveValue.size(); }
bool GetItemData(const int in_ID, int& out_dValue);
//获得子结点ID,in_ID从0, in_iChildPos从0开始
bool GetChildID(const int in_ID, const int in_iChildPos, int& out_iChildID);
//获得子结点ID,in_ID从0, in_iChildPos从0开始
bool GetChildData(const int in_ID, const int in_iChildPos, int& out_dChildValue);
void Show();
private:
void Show(const int in_ID, const int in_Layer);
int m_iTreePath;
vector
};
#include "Chess_MoveTree.h"
#include
#include
Chess_MoveTree::Chess_MoveTree(void)
{
MoveValue.clear();
m_iTreePath = 0;
srand(time(0));
}
Chess_MoveTree::~Chess_MoveTree(void)
{
MoveValue.clear();
}
void Chess_MoveTree::Create(const int in_iTreePath, const int in_iLayerNum)
{
m_iTreePath = in_iTreePath;
MoveValue.clear();
int iTreeSize = (pow((double)m_iTreePath,in_iLayerNum)-1.0)/(m_iTreePath-1.0);
for(int i=0; i
int dValue = rand()%10000-5000;
MoveValue.push_back(dValue);
}
}
bool Chess_MoveTree::GetItemData(const int in_ID, int& out_dValue)
{
if( in_ID <0 || in_ID>= Size())
return false;
out_dValue = MoveValue[in_ID];
return true;
}
bool Chess_MoveTree::GetChildID(const int in_ID, const int in_iChildPos, int& out_iChildID)
{
int iTemp = in_ID*m_iTreePath+(in_iChildPos+1);
if( iTemp <0 || iTemp>= Size())
return false;
out_iChildID = iTemp;
return true;
}
bool Chess_MoveTree::GetChildData(const int in_ID, const int in_iChildPos, int& out_dChildValue)
{
int iTemp = in_ID*m_iTreePath+(in_iChildPos+1);
if( iTemp <0 || iTemp>= Size())
return false;
out_dChildValue = MoveValue[iTemp];
return true;
}
void Chess_MoveTree::Show()
{
Show(0,1);
}
void Chess_MoveTree::Show(const int in_ID, const int in_Layer)
{
int dValue;
if( GetItemData(in_ID, dValue))
{
for(int i=0; i
cout<<"("<
int iChildID = 0;
if( GetChildID(in_ID,i, iChildID))
Show(iChildID, in_Layer+1);
}
}
}