繁体   English   中英

C# 返回列表,包括井字游戏中可能的动作

[英]C# Return list with possible moves from a TicTacToe game

我正在使用 Minimax 算法(我是编程新手)研究井字游戏 AI,但无法弄清楚如何制作 function,它返回一个包含 AI 可以做出的所有可能动作的列表。 GetChildren Function 应该返回一个包含所有可能动作的列表(以便 minimax 算法可以查看它们),而是返回相同的棋盘,但零变为负数

这是我的代码:

private List<int[,]> GetChildren(int[,] board, int value)
    {
        List<int[,]> ret = new List<int[,]>();
        if (board[0, 0] == 0)
        {
            int[,] aux = board;
            aux[0, 0] = value;
            ret.Add(aux);
        }
        if (board[0, 1] == 0)
        {
            int[,] aux = board;
            aux[0, 1] = value;
            ret.Add(aux);
        }
        if (board[0, 2] == 0)
        {
            int[,] aux = board;
            aux[0, 2] = value;
            ret.Add(aux);
        }

        // more code (up until [2, 2])
        return ret;
    }

我没有包含更多来自 [0, 2] 的 if 语句,因为它们是相同的。 此外,还有很多我认为可以简化的代码。 请帮我。

提前致谢。

为了简化您的代码,我将使用单个数组。 使用二维数组没有任何好处,所以你可以做这样的事情

int[] board = new int[9] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
List<int> possible_moves = new List<int>();
for (int i = 0; i < 9; i++)
{
   if(board[i] == 0)
   {
      possible_moves.Add(i);
   }                                                                                 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM