📄 evaluation.c
字号:
//evaluatation.c
#include <assert.h>
#include "data.h"
int GameOver(int wtm, int *si, int ply)
{
if((si[RED] == 8) || (si[RED] == 9))
//红胜
{
return ((wtm==RED) ? (WIN - ply) : -(WIN - ply));
}
else if ((si[RED] == 0) && //红子被逼死在牛角尖上
((si[BLUESTONE1] + si[BLUESTONE2]) == 3))
//红负
{
return ((wtm==RED) ? (LOSE + ply) : -(LOSE + ply));
}
return ((wtm==RED) ? UNKNOWN : -UNKNOWN);
}
/*
************************************************************************
Evaluation
******************************
功能描述:为局面打分。
参数: wtm:轮到谁走棋;si:待估局面;ply:层数。
返回值: 局面的估值。
*************************************************************************
*/
int Evaluation(int wtm, int *si, int ply)
{
int i = 0;
int value = ((wtm==RED) ? UNKNOWN : -UNKNOWN);
#ifdef _DEBUG //局面合法性检查
int good = 1;
if(stoneIntersection[REDSTONE] ==
stoneIntersection[BLUESTONE1])
good = 0;
if(stoneIntersection[REDSTONE] ==
stoneIntersection[BLUESTONE2])
good = 0;
if(stoneIntersection[BLUESTONE1] ==
stoneIntersection[BLUESTONE2])
good = 0;
assert(good);
#endif
if ((si[RED] > si[BLUESTONE1]) || //红子成功突围
(si[RED] > si[BLUESTONE2]))
//红胜
{
value = ((wtm==RED) ?
(WIN - ply) : -(WIN - ply));
}
else if (
(wtm == BLUE) && //该蓝子走棋,且红子和两个蓝子构成三角
((stoneIntersection[BLUESTONE1] == si[RED] + 1 ) &&
(stoneIntersection[BLUESTONE2] == si[RED] + 2 ) ||
(stoneIntersection[BLUESTONE1] == si[RED] + 2 ) &&
(stoneIntersection[BLUESTONE2] == si[RED] + 1 ))
)
//红胜
{
value = WIN - ply;
}
else if (
(wtm == RED) && //该红子走棋,且红子和蓝子构成三角
((1 == si[REDSTONE] || 2 == si[REDSTONE] || 3 == si[REDSTONE]) &&
((stoneIntersection[BLUESTONE1] == si[RED] + 1 ) &&
(stoneIntersection[BLUESTONE2] == si[RED] + 2 ) ||
(stoneIntersection[BLUESTONE1] == si[RED] + 2 ) &&
(stoneIntersection[BLUESTONE2] == si[RED] + 1 )))
)
{
value = -WIN + ply;
}
return value;
}
/*
************************************************************************
HasRepetition
******************************
功能描述:检测循环局面。
参数: posStk:保存局面的栈结构;ply:层数。
返回值: 0,无循环;1,有循环。
*************************************************************************
*/
int HasRepetition(int* posStk, int ply)
{
//blue stone1 与 blue stone2棋子交换,还是同一个局面
//若采用hash table来识别不同的局面,则可以避免这样的处理
if(stoneIntersection[BLUESTONE1] >
stoneIntersection[BLUESTONE2])
{
posStk[ply] =
(stoneIntersection[RED] << 8) +
(stoneIntersection[BLUESTONE1] << 4) +
(stoneIntersection[BLUESTONE2]);
}
else
{
posStk[ply] =
(stoneIntersection[RED] << 8) +
(stoneIntersection[BLUESTONE2] << 4) +
(stoneIntersection[BLUESTONE1]);
}
//5步之内,不会循环
if (ply < 5)
return 0;
//探测6步一循环的着法序列:a1 a2 b1 b2 a1 a2
if ((posStk[ply] == posStk[ply - 4]) &&
(posStk[ply - 1] == posStk[ply - 5])
)
{
return 1;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -