对角连接4检查

时间:2015-03-16 14:09:17

标签: c# .net

你好我以前可以看到这个问题。但是我不明白或者可以看到我自己可以如何实施。如果它可能你可以解释你在做什么,也许可以添加一些伪代码,所以我可以看到脚本的流程。

我已经完成了这样的垂直和水平线,是PlayingBoard是一个二维数组:

        private void HasWon(int xPlaced, int yPlaced) {
        //MessageBox.Show(xPlaced.ToString()+","+yPlaced.ToString());
        int[] Coords = new int[2];

        /// <summary>
        /// This part checks if we have a win on East or West
        /// </summary>
        Coords[0] = xPlaced;
        Coords[1] = yPlaced;
        while(Coords[0] != 0)
        {
            Coords[0] -= 1;
            if (PlayingBoard[Coords[0], Coords[1]] == playerValue)
            {
                foundInRow += 1;
            }
            else { break; }
        }

        Coords[0] = xPlaced;
        Coords[1] = yPlaced;
        while (Coords[0] < 6)
        {
            Coords[0] += 1;
            if (PlayingBoard[Coords[0], Coords[1]] == playerValue)
            {
                foundInRow += 1;
            }
            else { break; }
        }
        if (foundInRow > 2) { MessageBox.Show("You won."); Won = true; }
        else { foundInRow = 0; Won = false; }

        /// <summary>
        /// This part checks if we have a win on North or South
        /// </summary>
        Coords[0] = xPlaced;
        Coords[1] = yPlaced;
        while(Coords[1] != 0)
        {
            Coords[1] -= 1;
            if (PlayingBoard[Coords[0], Coords[1]] == playerValue)
            {
                foundInRow += 1;
            }
            else { break; }
        }

        Coords[0] = xPlaced;
        Coords[1] = yPlaced;
        while (Coords[1] < 6)
        {
            Coords[1] += 1;
            if (PlayingBoard[Coords[0], Coords[1]] == playerValue)
            {
                foundInRow += 1;
            }
            else { break; }
        }
        if (foundInRow > 2) { MessageBox.Show("You won."); Won = true; }
        else { foundInRow = 0; Won = false; }
    }

1 个答案:

答案 0 :(得分:1)

这是一个使用递归工作的连接四解算器。

它接受代表电路板的二维数组b。数组中的每个元素都可以用int填充,其中0表示空白区域1个玩家1,2个玩家2等

Checkboard方法返回一个int,其中-1表示没有获胜者,正整数代表获胜的玩家数

该方法通过从0,0遍历数组中的每个方块并检查三个可能的方向(左,对角和向下)来进一步检查另外3个包含相同数字的相邻元素。如果找到一行4,则该方法返回当时正在检查的方格中的数字。

    public class Connect4Solver
    {
        public int Checkboard(int[,] b)
        {
            for (int x = 0; x < b.GetLength(0); x++)
            {
                for (int y = 0; y < b.GetLength(1); y++)
                {
                    for (int d = 0; d < 3; d++)
                    {
                        int p = b[x, y];
                        if (p != 0)
                        {
                            if (countDir(0, b, x, y, d, p) >= 3)
                            {
                                //win for p
                                return p;
                            }
                        }
                    }
                }
            }

            return -1;
        }

        protected int countDir(int depth, int[,] b, int x, int y, int dir, int p)
        {
            int x2;
            int y2;
            if (getposdir(b, x, y, dir, out x2, out y2) == p)
            {
                //good
                depth++;
                return countDir(depth, b, x2, y2, dir, p);
            }
            else
            {
                return depth;
            }
        }

        protected int getposdir(int[,] b, int x, int y, int dir, out int x2, out int y2)
        {

            if (dir == 0)
            {
                x2 = x + 1;
                y2 = y;
            }
            else if (dir == 1)
            {
                x2 = x + 1;
                y2 = y + 1;
            }
            else if (dir == 2)
            {
                x2 = x;
                y2 = y + 1;
            }
            else
            {
                throw new Exception("unknown");
            }
            return getpos(b, x2, y2);
        }

        protected int getpos(int[,] b, int x, int y)
        {
            if (b.GetLength(0) <= x)
            {
                return -1;
            }
            if (b.GetLength(1) <= y)
            {
                return -1;
            }
            return b[x, y];
        }

    }
  

注意:我忘了检查'向下和向右',假设它想'向下'   并且不需要'左'。我将此作为练习留给读者   添加