今天还是只做了项目,稍微调整了一下人机大战,但是还是有点小问题,就是要多次点击人机大战才能让ai白棋,很恶心,很痛苦,但是相比昨天而言,ai可以运行了也没啥好总结的,就放下代码有部分注释的。
void ai_calculateScore( )
{
// 统计玩家或者电脑连成的子
int personNum = 0; // 玩家连成子的个数
int botNum = 0; // AI连成子的个数
int emptyNum = 0; // 各方向空白位的个数
// 清空评分数组
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
scoreMap[i][j] = 0;
}
}
int size = 15;
for (int Row = 0; Row < size; Row++)
for (int Col = 0; Col < size; Col++)
{
// 空白点就算
if (board[Row][Col] == 0) {
// 遍历周围八个方向
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++)
{
// 重置
personNum = 0;
botNum = 0;
emptyNum = 0;
// 原坐标不算
if (!(y == 0 && x == 0))
{
// 每个方向延伸4个子
// 对黑棋评分(正反两个方向)
for (int i = 1; i <= 4; i++)
{
int curRow = Row + i * y;
int curCol = Col + i * x;
if (curRow >= 0 && curRow < size &&
curCol >= 0 && curCol < size &&
board[curRow][curCol] == 1) // 真人玩家的子
{
personNum++;
}
else if (curRow >= 0 && curRow < size &&
curCol >= 0 && curCol < size &&
board[curRow][curCol] == 0) // 空白位
{
emptyNum++;
break;
}
else // 出边界
break;
}
for (int i = 1; i <= 4; i++)
{
int curRow = Row - i * y;
int curCol = Col - i * x;
if (curRow >= 0 && curRow < size &&
curCol >= 0 && curCol < size &&
board[curRow][curCol] == 1) // 真人玩家的子
{
personNum++;
}
else if (curRow >= 0 && curRow < size &&
curCol >= 0 && curCol < size &&
board[curRow][curCol] == 0) // 空白位
{
emptyNum++;
break;
}
else // 出边界
break;
}
if (personNum == 1) // 杀二
scoreMap[Row][Col] += 10;
else if (personNum == 2) // 杀三
{
if (emptyNum == 1)
scoreMap[Row][Col] += 30;
else if (emptyNum == 2)
scoreMap[Row][Col] += 40;
}
else if (personNum == 3) // 杀四
{
// 量变空位不一样,优先级不一样
if (emptyNum == 1)
scoreMap[Row][Col] += 60;
else if (emptyNum == 2)
scoreMap[Row][Col] += 200;
}
else if (personNum == 4) // 杀五
scoreMap[Row][Col] += 20000;
// 进行一次清空
emptyNum = 0;
// 对白棋评分
for (int i = 1; i <= 4; i++)
{
int curRow = Row + i * y;
int curCol = Col + i * x;
if (curRow > 0 && curRow < size &&
curCol > 0 && curCol < size &&
board[curRow][curCol] == -1) // 玩家的子
{
botNum++;
}
else if (curRow > 0 && curRow < size &&
curCol > 0 && curCol < size &&
board[curRow][curCol] == 0) // 空白位
{
emptyNum++;
break;
}
else // 出边界
break;
}
for (int i = 1; i <= 4; i++)
{
int curRow = Row - i * y;
int curCol = Col - i * x;
if (curRow > 0 && curRow < size &&
curCol > 0 && curCol < size &&
board[curRow][curCol] == -1) // 玩家的子
{
botNum++;
}
else if (curRow > 0 && curRow < size &&
curCol > 0 && curCol < size &&
board[curRow][curCol]==0) // 空白位
{
emptyNum++;
break;
}
else // 出边
break;
}
if (botNum == 0) // 普通下子
scoreMap[Row][Col] += 5;
else if (botNum == 1) // 活二
scoreMap[Row][Col] += 10;
else if (botNum == 2)
{
if (emptyNum == 1) // 死三
scoreMap[Row][Col] += 25;
else if (emptyNum == 2)
scoreMap[Row][Col] += 50; // 活三
}
else if (botNum == 3)
{
if (emptyNum == 1) // 死四
scoreMap[Row][Col] += 55;
else if (emptyNum == 2)
scoreMap[Row][Col] += 10000; // 活四
}
else if (botNum >= 4)
scoreMap[Row][Col] += 30000; // 活五,应该具有最高优先级
}
}
}
}
}
}