📄 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 + -