📄 weiqibrd.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#if !defined( globalH )
#include "global.h"
#endif
#include "weiqibrd.h"
#include "weiqiply.h"
//---------------------------------------------------------------------------
/*class TWeiqiBoard
{
enum TWhichSide { wqSideNone = 0, wqSideWhite, wqSideBlack };
struct TBlock
{
int X;
int Y;
TBlock *Next;
};
struct TSituationTree
{
TWhichSide WhichSide;
TBlock *Block;
TSituationTree *Next;
};
friend class TMainFormWeiqi;
private:
TWhichSide Phase[21][21];
TSituationTree *SituationTree;
public:
bool InsertStone( TWhichSide WhichSide );
bool RemoveStone( TWhichSide WhichSide );
bool UpdateSituation();
public:
TWeiqiBoard();
~TWeiqiBoard();
};*/
//---------------------------------------------------------------------------
__fastcall TWeiqiBoard::TWeiqiBoard()
{
int i, j;
for( i=0; i<=20; i++ )
for( j=0; j<=20; j++ )
{
Phase[i][j] = wqSideNone;
}
Order = 0;
BlackDeadStones = 0;
WhiteDeadStones = 0;
GetLocalTime( &StartTime ); // Windows function
BlackUsedTime.wYear = 0; // Windows function
BlackUsedTime.wMonth = 0; // Windows function
BlackUsedTime.wDay = 0; // Windows function
BlackUsedTime.wHour = 0; // Windows function
BlackUsedTime.wMinute = 0; // Windows function
BlackUsedTime.wSecond = 0; // Windows function
WhiteUsedTime.wYear = 0; // Windows function
WhiteUsedTime.wMonth = 0; // Windows function
WhiteUsedTime.wDay = 0; // Windows function
WhiteUsedTime.wHour = 0; // Windows function
WhiteUsedTime.wMinute = 0; // Windows function
WhiteUsedTime.wSecond = 0; // Windows function
pSituationTree = new( TSituationTree );
// pSituationTree = NULL;
pSituationTree->WhichSide = wqSideNone;
pSituationTree->nBreath = 0;
// pSituationTree->pBlock = new( TBlock );
// pSituationTree->pBlock->nX = 0;
// pSituationTree->pBlock->nY = 0;
// pSituationTree->pBlock->pNext = NULL;
pSituationTree->pBlock = NULL;
pSituationTree->pNext = NULL;
}
//---------------------------------------------------------------------------
__fastcall TWeiqiBoard::~TWeiqiBoard()
{
TSituationTree *p1 = new( TSituationTree );
TSituationTree *p2 = new( TSituationTree );
TBlock *q1 = new( TBlock );
TBlock *q2 = new( TBlock );
p1 = pSituationTree;
while( p1->pNext != NULL )
{
q1 = p1->pBlock;
while( q1->pNext != NULL )
{
q2 = q1->pNext;
if( q1 != NULL )
{
delete q1;
q1 = NULL;
}
q1 = q2;
}
if( q1 != NULL )
{
delete q1;
q1 = NULL;
}
p2 = p1->pNext;
if( p1 != NULL )
{
delete p1;
p1 = NULL;
}
p1 = p2;
}
while( q1->pNext != NULL )
{
q2 = q1->pNext;
if( q1 != NULL )
{
delete q1;
q1 = NULL;
}
q1 = q2;
}
if( q1 != NULL )
{
delete q1;
q1 = NULL;
}
if( p1 != NULL )
{
delete p1;
p1 = NULL;
}
delete this;
}
//---------------------------------------------------------------------------
bool TWeiqiBoard::CanKillSomeStones( TWhichSide WhichSide, int X, int Y )
{
// bForbidden = true;
// return( bForbidden );
return( true );
}
//---------------------------------------------------------------------------
bool TWeiqiBoard::IsRaiding( TWhichSide WhichSide, int X, int Y )
{
// bool bForbidden = false;
// bForbidden = true;
// return( bForbidden );
return( true );
}
//---------------------------------------------------------------------------
bool TWeiqiBoard::IsForbiddenMove( TWhichSide WhichSide, int X, int Y )
{
// bool bForbidden = false;
if( !CanKillSomeStones( WhichSide, X, Y ) )
return( false );
if( IsRaiding( WhichSide, X, Y ) )
return( false );
// bForbidden = true;
if( CanKillSomeStones( WhichSide, X, Y ) || !IsRaiding( WhichSide, X, Y ) )
return( false );
// bForbidden = true;
// return( bForbidden );
return( true );
}
//---------------------------------------------------------------------------
bool TWeiqiBoard::InsertStone( TWhichSide WhichSide, int X, int Y )
{
if( Phase[X][Y] == wqSideNone )
{
Order += 1;
Phase[X][Y] = WhichSide;
if( X == 1 )
{
if( WhichSide == wqSideBlack )
Phase[0][Y] = wqSideWhite;
else if( WhichSide == wqSideWhite )
Phase[0][Y] = wqSideBlack;
}
else if( X == 19 )
{
if( WhichSide == wqSideBlack )
Phase[20][Y] = wqSideWhite;
else if( WhichSide == wqSideWhite )
Phase[20][Y] = wqSideBlack;
}
if( Y == 1 )
{
if( WhichSide == wqSideBlack )
Phase[X][0] = wqSideWhite;
else if( WhichSide == wqSideWhite )
Phase[X][0] = wqSideBlack;
}
else if( Y == 19 )
{
if( WhichSide == wqSideBlack )
Phase[X][20] = wqSideWhite;
else if( WhichSide == wqSideWhite )
Phase[Y][20] = wqSideBlack;
}
UpdateSituationTree( X, Y );
return( true );
}
else
return( false );
}
//---------------------------------------------------------------------------
TAdjacentInformation TWeiqiBoard::GetAdjacentInformation( int X, int Y ) const
{
TAdjacentInformation ai;
ai.nX = X;
ai.nY = Y;
ai.nTotal = 0;
if( Phase[X][Y] == Phase[X][Y-1] )
{
ai.nTotal += 1;
ai.bTop = true;
}
else
ai.bTop = false;
if( Phase[X][Y] == Phase[X][Y+1] )
{
ai.nTotal += 1;
ai.bBottom = true;
}
else
ai.bBottom = false;
if( Phase[X][Y] == Phase[X-1][Y] )
{
ai.nTotal += 1;
ai.bLeft = true;
}
else
ai.bLeft = false;
if( Phase[X][Y] == Phase[X+1][Y] )
{
ai.nTotal += 1;
ai.bRight = true;
}
else
ai.bRight = false;
#if defined( __DEBUG__ )
AnsiString as;
if( ai.bTop )
as += "Top ";
if( ai.bBottom )
as += "Bottom ";
if( ai.bLeft )
as += "Left ";
if( ai.bRight )
as += "Right ";
// ShowMessage(IntToStr(ai.nX)+", "+IntToStr(ai.nY)+as);
#endif
return( ai );
}
//---------------------------------------------------------------------------
TBlock * TWeiqiBoard::SearchAdjacentBlock( int X, int Y, TAdjacentDirection ad ) const
{
// bool bSearched = false;
int theX = X, theY = Y;
if( ad == adLeft )
theX -= 1;
if( ad == adRight )
theX += 1;
if( ad == adTop )
theY -= 1;
if( ad == adBottom )
theY += 1;
TSituationTree *pSearchingTree = new( TSituationTree );
TBlock *pSearchingBlock = new( TBlock );
TBlock *pSearchedBlock = new( TBlock );
pSearchingTree = pSituationTree->pNext;
while( pSearchingTree != NULL )
{
pSearchingBlock = pSearchingTree->pBlock;
pSearchedBlock = pSearchingTree->pBlock;
while( pSearchingBlock != NULL )
{
if( ( pSearchingBlock->nX == theX ) && ( pSearchingBlock->nY == theY ) )
{
// ShowMessage( IntToStr(pSearchedBlock->nX) + ", " + IntToStr(pSearchedBlock->nY) );
return( pSearchedBlock );
}
// bSearched = true;
else
pSearchingBlock = pSearchingBlock->pNext;
}
pSearchingTree = pSearchingTree->pNext;
}
// delete q1;
// delete p1;
// if( bSearched )
// return( pSearchingTree->pSearchedBlock );
// else
return( NULL );
}
//---------------------------------------------------------------------------
TBlock * TWeiqiBoard::AddToNewBlock( TAdjacentInformation ai )
{
// Declare two pointers:
// One(pLastTree) is for the last branch(block) of the TSituationTree,
// The other(pNewTree) is for the new TSituationTree.
// A new pointer pNewBlock is neccessary that points to the new block.
// TBlock *pNewBlock = new( TBlock );
// pNewBlock = NULL;
// pNewBlock->nX = ai.nX;
// pNewBlock->nY = ai.nY;
// pNewBlock->pNext = new( TBlock );
// pNewBlock->pNext = NULL;
TSituationTree *pNewTree = new( TSituationTree );
// pNewTree = NULL;
pNewTree->WhichSide = Phase[ ai.nX ][ ai.nY ];
pNewTree->nBreath = 4;
// pNewTree->pBlock = pNewBlock;
pNewTree->pBlock = new( TBlock );
pNewTree->pBlock->nNo = Order;
pNewTree->pBlock->nX = ai.nX;
pNewTree->pBlock->nY = ai.nY;
pNewTree->pBlock->pNext = NULL;
// pNewTree->pNext = new( TSituationTree );
pNewTree->pNext = NULL;
// Reserve the orginal pointer.
TSituationTree *pTailOfTree = new( TSituationTree );
pTailOfTree = pSituationTree;
// TBlock *pNewBlock = new( TBlock );
// pNewBlock = pSituationTree->pBlock;
// Add a new block to pSiuationTree.
while( pTailOfTree->pNext != NULL )
pTailOfTree = pTailOfTree->pNext;
// pLastTree->pNext = pNewTree;
// pTailOfTree = new( TSituationTree );
pTailOfTree->pNext = pNewTree;
// pTailOfTree->WhichSide = Phase[ ai.nX ][ ai.nY ];
// pTailOfTree->nBreath = 4;
// pTailOfTree->pBlock = new( TBlock );
// pTailOfTree->pBlock->nX = ai.nX;
// pTailOfTree->pBlock->nY = ai.nY;
// pTailOfTree->pBlock->pNext = NULL;
// pTailOfTree->pNext = NULL;
// pSituationTree = pOriginalTree;
/* TSituationTree *pOriginalTree = new( TSituationTree );
pOriginalTree = pSituationTree;
// TBlock *pNewBlock = new( TBlock );
// pNewBlock = pSituationTree->pBlock;
// Add a new block to pSiuationTree.
while( pSituationTree != NULL )
pSituationTree = pSituationTree->pNext;
// pLastTree->pNext = pNewTree;
pSituationTree = new( TSituationTree );
pSituationTree->WhichSide = Phase[ ai.nX ][ ai.nY ];
pSituationTree->nBreath = 4;
pSituationTree->pBlock = new( TBlock );
pSituationTree->pBlock->nX = ai.nX;
pSituationTree->pBlock->nY = ai.nY;
pSituationTree->pBlock->pNext = NULL;
pSituationTree->pNext = NULL;
pSituationTree = pOriginalTree;
*/
// pLastTree = pNewTree;
// delete pNewBlock;
// delete pOriginalTree;
// delete pNewTree;
// UpdateMemoTip( ai.nX, ai.nY );
return( pNewTree->pBlock ); // pSituationTree
}
//---------------------------------------------------------------------------
TBlock * TWeiqiBoard::AddToExistedBlock( TAdjacentInformation ai, TBlock *pAdjBlock )
{
// Declare two pointers:
// One(pLastTree) is for the last branch(block) of the TSituationTree,
// The other(pNewTree) is for the new TSituationTree.
// A new pointer pNewBlock is neccessary that points to the new block.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -