int CEveluation::Eveluate(BYTE position[10][9], BOOL bIsRedTurn) { int i, j, k; int nChessType, nTargetType; count++; memset(m_chessValue,0, 360); memset(m_AttackPos,0, 180); memset(m_GuardPos,0, 90); memset(m_FlexibilityPos, 0, 90); for(i = 0; i < 10; i++) for(j = 0; j < 9; j++) { if(position[i][j] != NOCHESS) { nChessType = position[i][j]; GetRelatePiece(position, j, i); for (k = 0; k < nPosCount; k++) { nTargetType = position[RelatePos[k].y][RelatePos[k].x]; if (nTargetType == NOCHESS) { m_FlexibilityPos[i][j]++; } else { if (IsSameSide(nChessType, nTargetType)) { m_GuardPos[RelatePos[k].y][RelatePos[k].x]++; }else { m_AttackPos[RelatePos[k].y][RelatePos[k].x]++; m_FlexibilityPos[i][j]++; switch (nTargetType) { case R_KING: if (!bIsRedTurn) return 18888; break; case B_KING: if (bIsRedTurn) return 18888; break; default: m_AttackPos[RelatePos[k].y][RelatePos[k].x] += (30 + (m_BaseValue[nTargetType] - m_BaseValue[nChessType])/10)/10; break; } } } } } } for(i = 0; i < 10; i++) for(j = 0; j < 9; j++) { if(position[i][j] != NOCHESS) { nChessType = position[i][j]; m_chessValue[i][j]++; m_chessValue[i][j] += m_FlexValue[nChessType] * m_FlexibilityPos[i][j]; m_chessValue[i][j] += GetBingValue(j, i, position); } } int nHalfvalue; for(i = 0; i < 10; i++) for(j = 0; j < 9; j++) { if(position[i][j] != NOCHESS) { nChessType = position[i][j]; nHalfvalue = m_BaseValue[nChessType]/16; m_chessValue[i][j] += m_BaseValue[nChessType]; if (IsRed(nChessType)) { if (m_AttackPos[i][j]) { if (bIsRedTurn) { if (nChessType == R_KING) { m_chessValue[i][j]-= 20; } else { m_chessValue[i][j] -= nHalfvalue * 2; if (m_GuardPos[i][j]) m_chessValue[i][j] += nHalfvalue; } } else { if (nChessType == R_KING) return 18888; m_chessValue[i][j] -= nHalfvalue*10; if (m_GuardPos[i][j]) m_chessValue[i][j] += nHalfvalue*9; } m_chessValue[i][j] -= m_AttackPos[i][j]; } else { if (m_GuardPos[i][j]) m_chessValue[i][j] += 5; } } else { if (m_AttackPos[i][j]) { if (!bIsRedTurn) { if (nChessType == B_KING) { m_chessValue[i][j]-= 20; } else { m_chessValue[i][j] -= nHalfvalue * 2; if (m_GuardPos[i][j]) m_chessValue[i][j] += nHalfvalue; } } else { if (nChessType == B_KING) return 18888; m_chessValue[i][j] -= nHalfvalue*10; if (m_GuardPos[i][j]) m_chessValue[i][j] += nHalfvalue*9; } m_chessValue[i][j] -= m_AttackPos[i][j]; } else { if (m_GuardPos[i][j]) m_chessValue[i][j] += 5; } } } } int nRedValue = 0; int nBlackValue = 0; for(i = 0; i < 10; i++) for(j = 0; j < 9; j++) { nChessType = position[i][j]; // if (nChessType == R_KING || nChessType == B_KING) // m_chessValue[i][j] = 10000; if (nChessType != NOCHESS) { if (IsRed(nChessType)) { nRedValue += m_chessValue[i][j]; } else { nBlackValue += m_chessValue[i][j]; } } } if (bIsRedTurn) { return nRedValue - nBlackValue; } else { return nBlackValue-nRedValue ; } }
int CEveluation::Eveluate(BYTE position[8][8], BOOL bIsRedTurn) { int i, j, k; int nChessType, nTargetType; count++; //初始化临时变量 memset(m_chessValue,0, 256); memset(m_AttackPos,0, 128); memset(m_GuardPos,0, 64); memset(m_FlexibilityPos, 0, 64); for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) { if(position[i][j] != NOCHESS) { nChessType = position[i][j]; GetRelatePiece(position, i, j);//y x for (k = 0; k < nPosCount; k++) { nTargetType = position[RelatePos[k].y][RelatePos[k].x]; if (nTargetType == NOCHESS) { m_FlexibilityPos[i][j]++; //灵活性增加 } else { if (IsSameSide(nChessType, nTargetType)) { m_GuardPos[RelatePos[k].y][RelatePos[k].x]++; }else { m_AttackPos[RelatePos[k].y][RelatePos[k].x]++; m_FlexibilityPos[i][j]++; switch (nTargetType) { case R_KING: if (!bIsRedTurn)//若是红王受攻击,黑方走下一步 则返回失败极值 return 18888; break; case B_KING: if (bIsRedTurn)//若是黑王受攻击,红方走下一步 则返回失败极值 return 18888; break; default://其它棋子,加上威胁分 m_AttackPos[RelatePos[k].y][RelatePos[k].x] += (30 + (m_BaseValue[nTargetType] - m_BaseValue[nChessType])/10)/10; break; } } } } } } //下面统计扫描到的数据 for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) { if(position[i][j] != NOCHESS) { nChessType = position[i][j]; m_chessValue[i][j]++; //加上灵活性分 值 m_chessValue[i][j] += m_FlexValue[nChessType] * m_FlexibilityPos[i][j]; //加上兵的附加值 m_chessValue[i][j] += GetBingValue(j, i, position); } } //下面统计扫描到的数据 int nHalfvalue; for(i = 0; i < 8; i++) for(j = 0; j <8; j++) { if(position[i][j] != NOCHESS) { nChessType = position[i][j]; //棋子的基本价值的1/16作为威胁/保护的增量 nHalfvalue = m_BaseValue[nChessType]/16; //每个棋子的基本价值入总价值 m_chessValue[i][j] += m_BaseValue[nChessType]; if (IsRed(nChessType)) { if (m_AttackPos[i][j])//当前红棋被威胁 { if (bIsRedTurn)//如果轮到红棋走 { if (nChessType == R_KING) { m_chessValue[i][j]-= 20;//如果是红王价值减20 } else//其它棋子 { m_chessValue[i][j] -= nHalfvalue * 2; if (m_GuardPos[i][j])//被保护 m_chessValue[i][j] += nHalfvalue; } } else//当前红棋被威胁 轮到黑棋走 { if (nChessType == R_KING) return 18888;//失败 被将死 m_chessValue[i][j] -= nHalfvalue*10; if (m_GuardPos[i][j]) m_chessValue[i][j] += nHalfvalue*9; } //加上被威胁差 ///防止一个兵威胁一个车,而估值函数没有反应 m_chessValue[i][j] -= m_AttackPos[i][j]; } else//没有受到威胁 { if (m_GuardPos[i][j])//受到了保护,加上一点分吧 m_chessValue[i][j] += 5; } } else//黑棋 同理 { if (m_AttackPos[i][j]) { if (!bIsRedTurn) { if (nChessType == B_KING) { m_chessValue[i][j]-= 20; } else { m_chessValue[i][j] -= nHalfvalue * 2; if (m_GuardPos[i][j]) m_chessValue[i][j] += nHalfvalue; } } else { if (nChessType == B_KING) return 18888; m_chessValue[i][j] -= nHalfvalue*10; if (m_GuardPos[i][j]) m_chessValue[i][j] += nHalfvalue*9; } m_chessValue[i][j] -= m_AttackPos[i][j]; } else { if (m_GuardPos[i][j]) m_chessValue[i][j] += 5; } } } } //以上是统计了每一个棋子的总价值 //下面统计红黑双方的总分 int nRedValue = 0; int nBlackValue = 0; for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) { nChessType = position[i][j]; // if (nChessType == R_KING || nChessType == B_KING) // m_chessValue[i][j] = 10000; if (nChessType != NOCHESS) { if (IsRed(nChessType))//把红方的值加总 { nRedValue += m_chessValue[i][j]; } else//把黑方的值加总 { nBlackValue += m_chessValue[i][j]; } } } //返回估值 if (bIsRedTurn) { return nRedValue - nBlackValue; } else { return nBlackValue-nRedValue ; } }