我的Connect R程序出现问题。我可以让它玩playervsplayer就好了,但是我的MinMax有些不对劲我不明白。我将我的2D数组传递给算法,以便遍历它并在我的MinMax中使用PlaceT算法,以便在我递归调用MinMax时添加到临时2D数组中。出于某种原因,它实际上将它添加到我的主2Darray中,所以当函数结束填充时我只需要添加一个项目。我包括了PlaceT函数和MinMax。我也做了一张图片供参考
这个想法是从底部遍历2D数组,如果有一个空白空间而不是添加到临时2D数组,而不是递归地调用Max,它会直到遍历结束。然后它会采取所有添加的移动,并查看哪一个得分最多,而不是返回到主MinMax,该MinMax应该用于将磁贴放置到应该去的位置。如果您想查看这些算法之外的任何其他代码,请告诉我。
static void PlaceT(int ogC, int ogR, int c, int r, string[,] board, string id)
{
if (id == "R")
{
if(r - 1 >= 0)
{
if (board[r - 1, c] == "R" || board[r - 1, c] == "B")
{
PlaceT(ogC, ogR, c, r - 1, board, id);
}
else
{
board[r - 1, c] = "R";
}
}
}
else if (id == "B")
{
if(r - 1 >= 0)
{
if (board[r - 1, c] == "B" || board[r - 1, c] == "R")
{
PlaceT(ogC, ogR, c, r - 1, board, id);
}
else
{
board[r - 1, c] = "B";
}
}
}
}
static void MinMax(string [,] board, int row, int col, string ID, int count, string otherID, bool isComp)
{
string[,] tempB = board;
AIMove bestmove = new AIMove();
bestmove.x = MaxMove(tempB, row, col, ID, count, otherID, isComp).x;
PlaceT(col, row, bestmove.x, row, board, ID);
return;
}
static AIMove MaxMove(string[,] board, int row, int col, string ID, int count, string otherID, bool isComp)
{
AIMove move = new AIMove();
string[,] grid = new string[row,col];
grid = board;
if (isFull(grid))
{
move.score = 0;
return move;
}
else if(rInRow(grid, count, row, col, ID) == 10)
{
move.score = 10;
return move;
}
else if(rInRow(grid, count, row, col, otherID) == 10)
{
move.score = -10;
return move;
}
List<AIMove> moves = new List<AIMove>();
for (int y = row - 1; y >= 0; y--)
{
for (int x = col - 1; x >= 0; x--)
{
if (grid[y, x] != "R" && grid[y, x] != "B")
{
move.x = x;
move.y = y;
if(isComp)
{
PlaceT(col, row, move.x, row, grid, ID);
isComp = !isComp;
move.score = MaxMove(grid, row, col, ID, count, otherID, isComp).score;
}
else
{
PlaceT(col, row, move.x, row, grid, otherID);
isComp = !isComp;
move.score = MaxMove(grid, row, col, ID, count, otherID, isComp).score;
}
moves.Add(move);
}
}
}
grid = board;
int _best = 0;
if(isComp)
{
int _bestScore = -100;
for (int i = 0; i < moves.Count; i++)
{
if (moves[i].score > _best)
{
_best = i;
_bestScore = moves[i].score;
}
}
}
else
{
int _bestScore = 100;
for (int i = 0; i < moves.Count; i++)
{
if (moves[i].score < _best)
{
_best = i;
_bestScore = moves[i].score;
}
}
}
return moves[_best];
}