使用MINMAX有四个我想实现连续最小最大算法四个(或connect4或连接四)游戏。实施和行(connect4)游戏
我想我找到了它的想法,它应该建立尽可能板的树达到一定的深度,对其进行评估并返回自己的分数,那么我们只取那些得分最高。
所以,aiChooseCol()
检查通过调用MinMax()
比分每一个可能的列,并返回具有最高分的列。
现在我不知道,这是调用MinMax()
正确的方式?
是否正确,检查temp = Math.Max(temp, 1000);
?
我还没有做出启发式功能,但至少应该承认一个成功的列,然后选择它,但它目前只是选择从左边第一个无柱...我想不出我是什么做错了。
private int AiChooseCol()
{
int best = -1000;
int col=0;
for (int i = 0; i < m_Board.Cols; i++)
{
if (m_Board.CheckIfColHasRoom(i))
{
m_Board.FillSignInBoardAccordingToCol(i, m_Sign);
int t = MinMax(5, m_Board, board.GetOtherPlayerSign(m_Sign));
if (t > best)
{
best = t;
col = i;
}
m_Board.RemoveTopCoinFromCol(i);
}
}
return col;
}
private int MinMax(int Depth, board Board, char PlayerSign)
{
int temp=0;
if (Depth <= 0)
{
// return from heurisitic function
return temp;
}
char otherPlayerSign = board.GetOtherPlayerSign(PlayerSign);
char checkBoard = Board.CheckBoardForWin();
if (checkBoard == PlayerSign)
{
return 1000;
}
else if (checkBoard == otherPlayerSign)
{
return -1000;
}
else if (!Board.CheckIfBoardIsNotFull())
{
return 0; // tie
}
if (PlayerSign == m_Sign) // maximizing Player is myself
{
temp = -1000;
for (int i = 0; i < Board.Cols; i++)
{
if (Board.FillSignInBoardAccordingToCol(i, PlayerSign)) // so we don't open another branch in a full column
{
var v = MinMax(Depth - 1, Board, otherPlayerSign);
temp = Math.Max(temp, v);
Board.RemoveTopCoinFromCol(i);
}
}
}
else
{
temp = 1000;
for (int i = 0; i < Board.Cols; i++)
{
if (Board.FillSignInBoardAccordingToCol(i, PlayerSign)) // so we don't open another branch in a full column
{
var v = MinMax(Depth - 1, Board, otherPlayerSign);
temp = Math.Min(temp, v);
Board.RemoveTopCoinFromCol(i);
}
}
}
return temp;
}
一些注意事项:
FillSignInBoardAccordingToCol()
返回一个布尔值,如果它是成功的。
的board
类型具有char[,]
阵列与实际板和玩家迹象。
此代码是在AI Player类。
在'AiChooseCol'你不及格列'i'成'MinMax',所以它是如何知道你问它来评估哪些列? – juharr
哦,也许我应该在调用'MinMax()'之前在'i'列中放置一个硬币? @juharr – shinzou
是的,我知道它可以被重构。它仍然无法修复'AiChooseCol'。 @juharr – shinzou