?? cchessevaluate.h
字號:
// CChessEvaluate.h
////////////////////// Type Define /////////////////////////////////////////////
typedef struct _relationtable{
BYTE nCChessID ;
int nUAttackCount ;
int nUGuardCount ;
BYTE UnderAttack[5];
BYTE UnderGurad[5];
} RelationTable;
/////////////////// Data Define ////////////////////////////////////////////////
POINT PointList[20]; // 目標點隊列
int nPointCount; // 目標點數目
RelationTable RelationOfMan[9][10]; // 關系表
const int MaxValue = 10000; // 最大極值(最小極值為最大極值的負值)
//各子的基本價值(子力價值)
// 將, 士, 象, 馬, 車, 炮, 卒
const int BasicValues[15] = { 0,0, 250, 250, 300, 500, 300, 80,
0, 250, 250, 300, 500, 300, 80 };
//各子的機動性價值(每多一步走法所加的分)
// 將, 士, 象, 馬, 車, 炮, 卒
const int MobilityValues[8] = { 0, 0, 1, 1, 12, 6, 6, 15 };
//各子的控制區域價值(所在的位置的價值)
const int PositionValues[8][90] = {
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
},
{ // 帥
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, -8, -9, 0, 0, 0, 0, 0, 0, 0,
5, -8, -9, 0, 0, 0, 0, 0, 0, 0,
1, -8, -9, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
},
{ // 士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 4, 12, 11, 13, 7, 9, 7,
6, 8, 8, 9, 12, 11, 13, 8, 12, 8,
-6, 5, -2, 4, 8, 8, 6, 6, 6, 6
},
{ // 炮
0, 0, 1, 0, -1, 0, 0, 1, 2, 4,
0, 1, 0, 0, 0, 0, 3, 1, 2, 4,
1, 2, 4, 0, 3, 0, 3, 0, 0, 0,
3, 2, 3, 0, 0, 0, 2, -5, -4, -5,
3, 2, 5, 0, 4, 4, 4, -4, -7, -6,
3, 2, 3, 0, 0, 0, 2, -5, -4, -5,
1, 2, 4, 0, 3, 0, 3, 0, 0, 0,
0, 1, 0, 0, 0, 0, 3, 1, 2, 4,
0, 0, 1, 0, -1, 0, 0, 1, 2, 4
},
{ // 兵
0, 0, 0, -2, 3, 10, 20, 20, 20, 0,
0, 0, 0, 0, 0, 18, 27, 30, 30, 0,
0, 0, 0, -2, 4, 22, 30, 45, 50, 0,
0, 0, 0, 0, 0, 35, 40, 55, 65, 2,
0, 0, 0, 6, 7, 40, 42, 55, 70, 4,
0, 0, 0, 0, 0, 35, 40, 55, 65, 2,
0, 0, 0, -2, 4, 22, 30, 45, 50, 0,
0, 0, 0, 0, 0, 18, 27, 30, 30, 0,
0, 0, 0, -2, 3, 10, 20, 20, 20, 0
}
};
/////////////////// Function Prototype /////////////////////////////////////////
// 估值函數,返回對當前局面的估值。fWhoseTurn標志當前輪到哪一方走棋
int Eveluate( int fWhoseTurn );
// 將目標點加入PointList隊列
inline void AddPointToQueue( BYTE x, BYTE y );
////////////////// Programmer-Defined Function //////////////////////////////////
int Eveluate( int fWhoseTurn )
{
int RedValues = 0; // 紅方總的分值
int BlackValues = 0; // 黑方總的分值
int nBasicVal[2] = { 0 , 0 }; // 雙方的子力值
int nMobilityVal[2] = { 0 , 0 }; // 雙方的機動性值
int nPositionVal[2] = { 0 , 0 }; // 雙方的控制區域值
int nRelationVal[2] = { 0 , 0 }; // 雙方的關系值(攻擊或保護)
BYTE nCChessID;
BYTE nTargetType;
int fSide;
int nPosition;
int i;
bool bHaveHalf;
//**** Reset RelationTable ****
memset( RelationOfMan, 0, sizeof(RelationTable)*90 );
int x, y;
for( x = 0; x <= 8; x ++ )
for( y = 0; y <= 9; y ++ )
{
if( CChessBoard[x][y] != 0 )
{
nCChessID = CChessBoard[x][y];
fSide = SideOfMan[ nCChessID ];
//++++++++++++++求得棋子的基本價值以及它所占據的位置的價值+++++++++++++++++++++++++++++++++++++++++++++++
nPosition = x * 10 + y * 9 ;
if( fSide == RED )
{
nBasicVal[fSide] += BasicValues[nCChessID];
nPositionVal[fSide] += PositionValues[nCChessID][nPosition];
}
else
{
nBasicVal[fSide] += BasicValues[nCChessID];
nPositionVal[fSide] += PositionValues[nCChessID - 7][89 - nPosition];
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//求得棋子的所有目標位置(移動到的、攻擊的、保護的)
nPointCount = 0;
switch( nCChessID )
{
case RED_K:
// 將帥碰面
i = IsKingFaceToFace( x, y, fSide );
if( i != -1 )
AddPointToQueue( x, i );
//向前
if( y < 2 )
AddPointToQueue( x, y + 1 );
//向后
if( y > 0 )
AddPointToQueue( x, y - 1 );
//向左
if( x > 3 )
AddPointToQueue( x - 1, y );
//向右
if( x < 5 )
AddPointToQueue( x + 1, y );
break;
case BLACK_K:
// 將帥碰面
i = IsKingFaceToFace( x, y, fSide );
if( i != -1 )
AddPointToQueue( x, i );
//向前
if( y > 7 )
AddPointToQueue( x, y - 1 );
//向后
if( y < 9 )
AddPointToQueue( x, y + 1 );
//向左
if( x < 5 )
AddPointToQueue( x + 1, y );
//向右
if( x > 3 )
AddPointToQueue( x - 1, y );
break;
case RED_J: // fall through
case BLACK_J:
//縱向
for( i = y + 1; i <= 9; i ++ )
{
AddPointToQueue( x, i );
if( HaveMan( x, i ) )
break;
}
for( i = y - 1; i >= 0; i -- )
{
AddPointToQueue( x, i );
if( HaveMan( x, i ) )
break;
}
//橫向
for( i = x - 1; i >= 0; i -- )
{
AddPointToQueue( i, y );
if( HaveMan( i, y ) )
break;
}
for( i = x + 1; i <= 8; i ++ )
{
AddPointToQueue( i, y );
if( HaveMan( i, y ) )
break;
}
break;
case RED_M: // fall through
case BLACK_M:
if( y <= 7 && ! HaveMan( x, y + 1 ) )
{
//11點方向(相對紅馬)
if( x > 0 )
AddPointToQueue( x - 1, y + 2 );
//1點方向
if( x < 8 )
AddPointToQueue( x + 1, y + 2 );
}
if( x <= 6 && ! HaveMan( x + 1, y ) )
{
//2點方向
if( y < 9 )
AddPointToQueue( x + 2, y + 1 );
//4點方向
if( y > 0 )
AddPointToQueue( x + 2, y - 1 );
}
if( y >= 2 && ! HaveMan( x, y - 1 ) )
{
//5點方向
if( x < 8 )
AddPointToQueue( x + 1, y - 2 );
//7點方向
if( x > 0 )
AddPointToQueue( x - 1, y - 2 );
}
if( x >= 2 && ! HaveMan( x - 1, y ) )
{
//8點方向
if( y > 0 )
AddPointToQueue( x - 2, y - 1 );
//10點方向
if( y < 9 )
AddPointToQueue( x - 2, y + 1 );
}
break;
case RED_P: // fall through
case BLACK_P:
//縱向
bHaveHalf = false; //標志尚未發現中間子
for( i = y + 1; i <= 9; i ++ )
{
if( ! bHaveHalf ) // 無中間子
{
if( ! HaveMan( x, i ) )
{
AddPointToQueue( x, i );
}
else // if( HaveMan( x, i ) )
{
bHaveHalf = true;
}
}
else // 已有中間子
{
if( HaveMan( x, i ) )
{
AddPointToQueue( x, i );
break;
}
}
}
bHaveHalf = false; //標志尚未發現中間子
for( i = y - 1; i >= 0; i -- )
{
if( ! bHaveHalf ) // 無中間子
{
if( ! HaveMan( x, i ) )
{
AddPointToQueue( x, i );
}
else // if( HaveMan( x, i ) )
{
bHaveHalf = true;
}
}
else // 已有中間子
{
if( HaveMan( x, i ) )
{
AddPointToQueue( x, i );
break;
}
}
}
//橫向
bHaveHalf = false; //標志尚未發現中間子
for( i = x - 1; i >= 0; i -- )
{
if( ! bHaveHalf ) // 無中間子
{
if( ! HaveMan( i, y ) )
{
AddPointToQueue( i, y );
}
else // if( HaveMan( i, y ) )
{
bHaveHalf = true;
}
}
else // 已有中間子
{
if( HaveMan( i, y ) )
{
AddPointToQueue( i, y );
break;
}
}
}
bHaveHalf = false; //標志尚未發現中間子
for( i = x + 1; i <= 8; i ++ )
{
if( ! bHaveHalf ) // 無中間子
{
if( ! HaveMan( i, y ) )
{
AddPointToQueue( i, y );
}
else // if( HaveMan( i, y ) )
{
bHaveHalf = true;
}
}
else // 已有中間子
{
if( HaveMan( i, y ) )
{
AddPointToQueue( i, y );
break;
}
}
}
break;
case RED_X:
if( x == 0 )
{
if( ! HaveMan(1, 3) )
AddPointToQueue( 2, 4 );
if( ! HaveMan(1, 1) )
AddPointToQueue( 2, 0 );
}
else if( x == 2 )
{
if( y == 4 )
{
if( ! HaveMan(1, 3) )
AddPointToQueue( 0, 2 );
if( ! HaveMan(3, 3) )
AddPointToQueue( 4, 2 );
}
else // y == 0
{
if( ! HaveMan(1, 1) )
AddPointToQueue( 0, 2 );
if( ! HaveMan(3, 1) )
AddPointToQueue( 4, 2 );
}
}
else if( x == 4 )
{
if( ! HaveMan(3, 3) )
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -