?? eveluation.cpp
字號:
// Eveluation.cpp: implementation of the CEveluation class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Chess.h"
#include "Eveluation.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
const int BA0[10][9]= //紅卒的附加值矩陣
{
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{90,90,110,120,120,120,110,90,90},
{90,90,110,120,120,120,110,90,90},
{70,90,110,110,110,110,110,90,70},
{70,70,70, 70, 70, 70, 70,70,70},
{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},
};
const int BA1[10][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, 0, 0, 0, 0, 0, 0, 0, 0},
{70,70,70, 70, 70,70, 70,70, 70},
{70,90,110,110,110,110,110,90,70},
{90,90,110,120,120,120,110,90,90},
{90,90,110,120,120,120,110,90,90},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CEveluation::CEveluation()
{
//在構造函數里初始化每種棋子的基本價值數組
m_BaseValue[B_KING] = BASEVALUE_KING;
m_BaseValue[B_CAR] = BASEVALUE_CAR;
m_BaseValue[B_HORSE] = BASEVALUE_HORSE;
m_BaseValue[B_BISHOP] = BASEVALUE_BISHOP;
m_BaseValue[B_ELEPHANT] = BASEVALUE_ELEPHANT;
m_BaseValue[B_CANON] = BASEVALUE_CANON;
m_BaseValue[B_PAWN] = BASEVALUE_PAWN;
m_BaseValue[R_KING] = BASEVALUE_KING;
m_BaseValue[R_CAR] = BASEVALUE_CAR;
m_BaseValue[R_HORSE] = BASEVALUE_HORSE;
m_BaseValue[R_BISHOP] = BASEVALUE_BISHOP;
m_BaseValue[R_ELEPHANT] = BASEVALUE_ELEPHANT;
m_BaseValue[R_CANON] = BASEVALUE_CANON;
m_BaseValue[R_PAWN] = BASEVALUE_PAWN;
//初始化靈活性價值數組
m_FlexValue[B_KING] = FLEXIBILITY_KING;
m_FlexValue[B_CAR] = FLEXIBILITY_CAR;
m_FlexValue[B_HORSE] = FLEXIBILITY_HORSE;
m_FlexValue[B_BISHOP] = FLEXIBILITY_BISHOP;
m_FlexValue[B_ELEPHANT] = FLEXIBILITY_ELEPHANT;
m_FlexValue[B_CANON] = FLEXIBILITY_CANON;
m_FlexValue[B_PAWN] = FLEXIBILITY_PAWN;
m_FlexValue[R_KING] = FLEXIBILITY_KING;
m_FlexValue[R_CAR] = FLEXIBILITY_CAR;
m_FlexValue[R_HORSE] = FLEXIBILITY_HORSE;
m_FlexValue[R_BISHOP] = FLEXIBILITY_BISHOP;
m_FlexValue[R_ELEPHANT] = FLEXIBILITY_ELEPHANT;
m_FlexValue[R_CANON] = FLEXIBILITY_CANON;
m_FlexValue[R_PAWN] = FLEXIBILITY_PAWN;
}
CEveluation::~CEveluation()
{
}
//為每一個兵返回附加值
//x是橫坐標,y是縱坐標,CurSituation是棋盤
//不是兵返回零
int CEveluation::GetBingValue(int x, int y, BYTE CurSituation[][9])
{
if (CurSituation[y][x] == R_PAWN)
return BA0[y][x];
if (CurSituation[y][x] == B_PAWN)
return BA1[y][x];
return 0;
}
//這個函數將一個位置加入數組RelatePos當中
void CEveluation::AddPoint(int x, int y)
{
RelatePos[nRelatePosCount].x = x;
RelatePos[nRelatePosCount].y = y;
nRelatePosCount++;
}
//判斷棋盤position上位置From 的棋子是否能走到位置To
//如果能返回TRUE否則返回FALSE
//與IsValidMove相似
BOOL CEveluation::CanTouch(BYTE position[10][9], int nFromX, int nFromY, int nToX, int nToY)
{
int i, j;
int nMoveChessID, nTargetID;
if (nFromY == nToY && nFromX == nToX)
return FALSE;//目的與源相同
nMoveChessID = position[nFromY][nFromX];
nTargetID = position[nToY][nToX];
switch(nMoveChessID)
{
case R_KING:
if (nTargetID == B_KING)//老將見面?
{
if (nFromX != nToX)
return FALSE;//兩個將不在同一列
for (i = nFromY - 1; i > nToY; i--)
if (position[i][nFromX] != NOCHESS)
return FALSE;//中間有別的子
}
else
{
if (nToY < 7 || nToX > 5 || nToX < 3)
return FALSE;//目標點在九宮之外
if(abs(nFromY - nToY) + abs(nToX - nFromX) > 1)
return FALSE;//將帥只走一步直線:
}
break;
case B_KING:
if (nTargetID == R_KING)//老將見面?
{
if (nFromX != nToX)
return FALSE;
for (i = nFromY + 1; i < nToY; i++)
{
if (position[i][nFromX] != NOCHESS)
return FALSE;
}
}
else
{
if (nToY > 2 || nToX > 5 || nToX < 3)
return FALSE;//目標點在九宮之外
if(abs(nFromY - nToY) + abs(nToX - nFromX) > 1)
return FALSE;//將帥只走一步直線:
}
break;
case R_BISHOP:
if (nToY < 7 || nToX > 5 || nToX < 3)
return FALSE;//士出九宮
if (abs(nFromY - nToY) != 1 || abs(nToX - nFromX) != 1)
return FALSE; //士走斜線
break;
case B_BISHOP: //黑士
if (nToY > 2 || nToX > 5 || nToX < 3)
return FALSE;//士出九宮
if (abs(nFromY - nToY) != 1 || abs(nToX - nFromX) != 1)
return FALSE; //士走斜線
break;
case R_ELEPHANT://紅象
if(nToY < 5)
return FALSE;//相不能過河
if(abs(nFromX-nToX) != 2 || abs(nFromY-nToY) != 2)
return FALSE;//相走田字
if(position[(nFromY + nToY) / 2][(nFromX + nToX) / 2] != NOCHESS)
return FALSE;//相眼被塞住了
break;
case B_ELEPHANT://黑象
if(nToY > 4)
return FALSE;//相不能過河
if(abs(nFromX-nToX) != 2 || abs(nFromY-nToY) != 2)
return FALSE;//相走田字
if(position[(nFromY + nToY) / 2][(nFromX + nToX) / 2] != NOCHESS)
return FALSE;//相眼被塞住了
break;
case B_PAWN: //黑兵
if(nToY < nFromY)
return FALSE;//兵不回頭
if( nFromY < 5 && nFromY == nToY)
return FALSE;//兵過河前只能直走
if(nToY - nFromY + abs(nToX - nFromX) > 1)
return FALSE;//兵只走一步直線:
break;
case R_PAWN: //紅兵
if(nToY > nFromY)
return FALSE;//兵不回頭
if( nFromY > 4 && nFromY == nToY)
return FALSE;//兵過河前只能直走
if(nFromY - nToY + abs(nToX - nFromX) > 1)
return FALSE;//兵只走一步直線:
break;
case B_CAR:
case R_CAR:
if(nFromY != nToY && nFromX != nToX)
return FALSE; //車走直線:
if(nFromY == nToY)
{
if(nFromX < nToX)
{
for(i = nFromX + 1; i < nToX; i++)
if(position[nFromY][i] != NOCHESS)
return FALSE;
}
else
{
for(i = nToX + 1; i < nFromX; i++)
if(position[nFromY][i] != NOCHESS)
return FALSE;
}
}
else
{
if(nFromY < nToY)
{
for(j = nFromY + 1; j < nToY; j++)
if(position[j][nFromX] != NOCHESS)
return FALSE;
}
else
{
for(j= nToY + 1; j < nFromY; j++)
if(position[j][nFromX] != NOCHESS)
return FALSE;
}
}
break;
case B_HORSE:
case R_HORSE:
if(!((abs(nToX-nFromX)==1 && abs(nToY-nFromY)==2)
||(abs(nToX-nFromX)==2&&abs(nToY-nFromY)==1)))
return FALSE;//馬走日字
if (nToX-nFromX==2)
{
i=nFromX+1;
j=nFromY;
}
else if (nFromX-nToX==2)
{
i=nFromX-1;
j=nFromY;
}
else if (nToY-nFromY==2)
{
i=nFromX;
j=nFromY+1;
}
else if (nFromY-nToY==2)
{
i=nFromX;
j=nFromY-1;
}
if(position[j][i] != (BYTE)NOCHESS)
return FALSE;//絆馬腿
break;
case B_CANON:
case R_CANON:
if(nFromY!=nToY && nFromX!=nToX)
return FALSE; //炮走直線:
//炮不吃子時經過的路線中不能有棋子:------------------
if(position[nToY][nToX] == NOCHESS)
{
if(nFromY == nToY)
{
if(nFromX < nToX)
{
for(i = nFromX + 1; i < nToX; i++)
if(position[nFromY][i] != NOCHESS)
return FALSE;
}
else
{
for(i = nToX + 1; i < nFromX; i++)
if(position[nFromY][i]!=NOCHESS)
return FALSE;
}
}
else
{
if(nFromY < nToY)
{
for(j = nFromY + 1; j < nToY; j++)
if(position[j][nFromX] != NOCHESS)
return FALSE;
}
else
{
for(j = nToY + 1; j < nFromY; j++)
if(position[j][nFromX] != NOCHESS)
return FALSE;
}
}
}
//以上是炮不吃子-------------------------------------
//吃子時:=======================================
else
{
int nCount=0;
if(nFromY == nToY)
{
if(nFromX < nToX)
{
for(i=nFromX+1;i<nToX;i++)
if(position[nFromY][i]!=NOCHESS)
nCount++;
if(nCount != 1)
return FALSE;
}
else
{
for(i=nToX+1;i<nFromX;i++)
if(position[nFromY][i] != NOCHESS)
nCount++;
if(nCount!=1)
return FALSE;
}
}
else
{
if(nFromY<nToY)
{
for(j=nFromY+1;j<nToY;j++)
if(position[j][nFromX]!=NOCHESS)
nCount++;
if(nCount!=1)
return FALSE;
}
else
{
for(j=nToY+1;j<nFromY;j++)
if(position[j][nFromX] != NOCHESS)
nCount++;
if(nCount!=1)
return FALSE;
}
}
}
//以上是炮吃子時================================
break;
default:
return FALSE;
}
return TRUE;
}
//這個函數枚舉了給定位是上棋子的所有相關位置
//包括可走到的位置和可保護的位置
//position是當前棋盤
//x是棋子的橫坐標,y是棋子的縱坐標
//與CMoveGenerator中一些部分類似
int CEveluation::GetRelatePiece(BYTE position[10][9], int j, int i)
{
nRelatePosCount = 0;
BYTE nChessID;
BYTE flag;
int x,y;
nChessID = position[i][j];
switch(nChessID)
{
case R_KING:
case B_KING:
for (y = 0; y < 3; y++)
for (x = 3; x < 6; x++)
if (CanTouch(position, j, i, x, y))
AddPoint(x, y);
for (y = 7; y < 10; y++)
for (x = 3; x < 6; x++)
if (CanTouch(position, j, i, x, y))
AddPoint(x, y);
break;
case R_BISHOP://紅仕
for (y = 7; y < 10; y++)
for (x = 3; x < 6; x++)
if (CanTouch(position, j, i, x, y))
AddPoint(x, y);
break;
case B_BISHOP://黑士
for (y = 0; y < 3; y++)
for (x = 3; x < 6; x++)
if (CanTouch(position, j, i, x, y))
AddPoint(x, y);
break;
case R_ELEPHANT://象
case B_ELEPHANT:
x=j+2;
y=i+2;
if(x < 9 && y < 10 && CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j+2;
y=i-2;
if(x < 9 && y>=0 && CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j-2;
y=i+2;
if(x>=0 && y < 10 && CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j-2;
y=i-2;
if(x>=0 && y>=0 && CanTouch(position, j, i, x, y))
AddPoint(x, y);
break;
case R_HORSE: //馬
case B_HORSE:
x=j+2;
y=i+1;
if((x < 9 && y < 10) &&CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j+2;
y=i-1;
if((x < 9 && y >= 0) &&CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j-2;
y=i+1;
if((x >= 0 && y < 10) &&CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j-2;
y=i-1;
if((x >= 0 && y >= 0) &&CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j+1;
y=i+2;
if((x < 9 && y < 10) &&CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j-1;
y=i+2;
if((x >= 0 && y < 10) &&CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j+1;
y=i-2;
if((x < 9 && y >= 0) &&CanTouch(position, j, i, x, y))
AddPoint(x, y);
x=j-1;
y=i-2;
if((x >= 0 && y >= 0) &&CanTouch(position, j, i, x, y))
AddPoint(x, y);
break;
case R_CAR:
case B_CAR:
x=j+1;
y=i;
while(x < 9)
{
if( NOCHESS == position[y][x] )
AddPoint(x, y);
else
{
AddPoint(x, y);
break;
}
x++;
}
x = j-1;
y = i;
while(x >= 0)
{
if( NOCHESS == position[y][x] )
AddPoint(x, y);
else
{
AddPoint(x, y);
break;
}
x--;
}
x=j;
y=i+1;//
while(y < 10)
{
if( NOCHESS == position[y][x])
AddPoint(x, y);
else
{
AddPoint(x, y);
break;
}
y++;
}
x = j;
y = i-1;//
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -