C# 连接4中的对角线匹配

C# 连接4中的对角线匹配,c#,forms,C#,Forms,嘿,伙计们,我正在用C语言开发一个Windows窗体应用程序中的Connect 4游戏,一切都很好,我只是被困在对角线部分。这是我为左下对角线检查开发的,但我不确定它是否完美工作。它是从第一块瓷砖开始工作的,它是1,1,但是我不能告诉你其他的瓷砖。我还想知道如何为另一边的对角线比赛创建另一种方法。gameButtons是我的2D数组,我的表单是6行7列这是我的方法: private void checkForDiaMatch() { int countBlue = 0;

嘿,伙计们,我正在用C语言开发一个Windows窗体应用程序中的Connect 4游戏,一切都很好,我只是被困在对角线部分。这是我为左下对角线检查开发的,但我不确定它是否完美工作。它是从第一块瓷砖开始工作的,它是1,1,但是我不能告诉你其他的瓷砖。我还想知道如何为另一边的对角线比赛创建另一种方法。gameButtons是我的2D数组,我的表单是6行7列这是我的方法:

private void checkForDiaMatch()
    {
        int countBlue = 0;
        int countRed = 0;

        for (int i = 0; i < 6; i++)
        {
            if (gameButtons[i, i].BackColor == Color.Blue)
            {
                countBlue++;
            }
            else
            {
                countBlue = 0;
            }
            if (gameButtons[i, i].BackColor == Color.Red)
            {
                countRed++;
            }
            else
            {
                countRed = 0;
            }

            if (countBlue >= 4)
            {
                MessageBox.Show("There is a blue diagonal match");
                MessageBox.Show("Blue wins!");
            }
            else if (countRed >= 4)
            {
                MessageBox.Show("There is a red diagonal match");
                MessageBox.Show("Red wins!");
            }

        }
    }

也许最有用的方法是在纸上画出你的图板和索引,然后写下任意对角线连接的索引4:

-------------------------------            -------------------------------
5 |   |   |   |   |   |   |5,6|            5 |   |   |   |   |   |   |   |
--+---+---+---+---+---+---+---+            --+---+---+---+---+---+---+---+
4 |   |   |   |   |   |4,5|   |            4 |   |4,1|   |   |   |   |   |
--+---+---+---+---+---+---+---+            --+---+---+---+---+---+---+---+
3 |   |   |   |   |3,4|   |   |            3 |   |   |3,2|   |   |   |   |
--+---+---+---+---+---+---+---+            --+---+---+---+---+---+---+---+
2 |   |   |   |2,3|   |   |   |            2 |   |   |   |2,3|   |   |   |
--+---+---+---+---+---+---+---+            --+---+---+---+---+---+---+---+
1 |   |   |   |   |   |   |   |            1 |   |   |   |   |1,4|   |   | 
--+---+---+---+---+---+---+---+            --+---+---+---+---+---+---+---+
0 |   |   |   |   |   |   |   |            0 |   |   |   |   |   |   |   |
--+---+---+---+---+---+---+---+            --+---+---+---+---+---+---+---+
  | 0 | 1 | 2 | 3 | 4 | 5 | 6 |              | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
我们可以从任何一点上看到,我们需要在两个方向上进行检查:上/右+下/左像a/和上/左+下/右像a\。我们想在这两个方向上计算相同颜色的碎片的数量,作为我们开始的那个。一旦总数为4,我们就可以返回true。如果两个方向都没有4个匹配的片段,我们将返回false

我对此进行了一些研究,并提出了以下适合我的方法。您的类可能略有不同,我刚刚开发了一个控制台应用程序,但希望逻辑能有所帮助:

private bool CompletesDiagonal(int pieceRow, int pieceCol)
{
    var colorToMatch = Board[pieceRow, pieceCol]; // Board is a ConsoleColor[7,6] array

    var matchingPieces = 1; // We will count the original piece as a match

    // Check forward slash direction '/'

    // First check down/left (decrement both row and column up to 3 times)
    for (int counter = 1; counter < 4; counter++)
    {
        var row = pieceRow - counter;
        var col = pieceCol - counter;

        // Make sure we stay within our board
        if (row < Board.GetLowerBound(0) || col < Board.GetLowerBound(1)) { break; }

        if (Board[row, col] == colorToMatch)
        {
            matchingPieces++;
            if (matchingPieces == 4) return true;
        }
        else { break; }
    }

    // Next check up/right (increment both row and column up to 3 times)
    for (int counter = 1; counter < 4; counter++)
    {
        var row = pieceRow + counter;
        var col = pieceCol + counter;

        // Make sure we stay within our board
        if (row > Board.GetUpperBound(0) || col > Board.GetUpperBound(1)) { break; }

        // Check for a match
        if (Board[row, col] == colorToMatch)
        {
            matchingPieces++;
            if (matchingPieces == 4) return true;
        }
        else { break; }
    }

    // If we got this far, no match was found in forward slash direction,
    // so reset our counter and check the back slash direction '\'
    matchingPieces = 1;

    // First check down/right (decrement row and increment column)
    for (int counter = 1; counter < 4; counter++)
    {
        var row = pieceRow - counter;
        var col = pieceCol + counter;

        // Make sure we stay within our board
        if (row < Board.GetLowerBound(0) || col > Board.GetUpperBound(1)) { break; }

        // Check for a match
        if (Board[row, col] == colorToMatch)
        {
            matchingPieces++;
            if (matchingPieces == 4) return true;
        }
        else { break; }
    }

    // Next check up/left (increment row and decrement column)
    for (int counter = 1; counter < 4; counter++)
    {
        var row = pieceRow + counter;
        var col = pieceCol - counter;

        // Make sure we stay within our board
        if (row > Board.GetUpperBound(0) || col < Board.GetLowerBound(1)) { break; }

        // Check for a match
        if (Board[row, col] == colorToMatch)
        {
            matchingPieces++;
            if (matchingPieces == 4) return true;
        }
        else { break; }
    }

    // If we've gotten this far, then we haven't found a match
    return false;
}
以下是对角线胜利的结果:


我会考虑两件事:1。如何手动检测对角线?2.你什么时候调用这个方法,在那个时候,你有什么信息?你不应该每次都要分析整个电路板。我的猜测是,每次有人放置一个片段时,你都会调用它,并且你知道该片段的位置。从那个位置,你会如何检测对角线?但我不能告诉其他瓷砖-为什么不?你用其他瓷砖测试过这个方法吗?您应该有,并且让这样的测试看到单元测试可用将帮助您编写和调试代码。对于有关堆栈溢出的问题,您需要提供一个可靠地再现您遇到的任何问题的好方法,详细解释该代码的作用和您想要的内容,并详细解释您迄今为止尝试解决该问题的方法,还有一个详细的解释,说明什么是你无法理解的。@RufusL所以,当用户在某处放置一个平铺时,就会调用这个方法。我得到的唯一信息是用户点击了某个地方,这就是为什么我创建了检查整个电路板的方法。我还有两个方法检查垂直和水平,我计划稍后在创建所有需要的方法时将它们放在一个方法中。该块已存储在变量中,但仅存储用户单击的块,而不是块的结束位置。@PeterDuniho我测试了其他块,但它似乎不起作用。它只从左上角的瓷砖开始,沿对角线向下,直到没有更多的瓷砖为止。@jk485921这样你就知道他们点击的位置,大概你也知道他们刚刚放在黑板上的那块瓷砖落在哪里了,对吧?如果你只是从放置的棋子上检查对角线,你应该是好的。为你制作实际游戏的道具,只是为了能够写出答案!:D@RufusL嘿,谢谢你详尽的回答和帮助。我已经尝试了你的方法,在改变了一些之后,比如我从0到3的计数器和类。不过,我仍然得到一个错误,在某些情况下我超出了数组的界限。@RufusL说实话,我真的不知道为什么我得到的索引超出了范围,因为编译器返回给我的每个值对于我的信息都是正确的。例如,我得到的索引超出了数组的范围,因为列变量是4。@jk485921抱歉,您仍然有问题。如果您使用winform,我建议您添加一些日志记录,您可以只记录到多行文本框或其他内容,或者在调试器中单步执行代码。如果您在测试时注意到只有某些模式触发了异常,那么在您知道要触发它之前设置一个断点,这样您就不必单步执行太多的循环。祝你好运@哈哈!谢谢有时候,别人的问题变成了一个有趣的挑战。当我完成游戏时,我真的玩得很开心!!:
private bool CompletesDiagonal(int pieceRow, int pieceCol)
{
    var colorToMatch = Board[pieceRow, pieceCol]; // Board is a ConsoleColor[7,6] array

    var matchingPieces = 1; // We will count the original piece as a match

    // Check forward slash direction '/'

    // First check down/left (decrement both row and column up to 3 times)
    for (int counter = 1; counter < 4; counter++)
    {
        var row = pieceRow - counter;
        var col = pieceCol - counter;

        // Make sure we stay within our board
        if (row < Board.GetLowerBound(0) || col < Board.GetLowerBound(1)) { break; }

        if (Board[row, col] == colorToMatch)
        {
            matchingPieces++;
            if (matchingPieces == 4) return true;
        }
        else { break; }
    }

    // Next check up/right (increment both row and column up to 3 times)
    for (int counter = 1; counter < 4; counter++)
    {
        var row = pieceRow + counter;
        var col = pieceCol + counter;

        // Make sure we stay within our board
        if (row > Board.GetUpperBound(0) || col > Board.GetUpperBound(1)) { break; }

        // Check for a match
        if (Board[row, col] == colorToMatch)
        {
            matchingPieces++;
            if (matchingPieces == 4) return true;
        }
        else { break; }
    }

    // If we got this far, no match was found in forward slash direction,
    // so reset our counter and check the back slash direction '\'
    matchingPieces = 1;

    // First check down/right (decrement row and increment column)
    for (int counter = 1; counter < 4; counter++)
    {
        var row = pieceRow - counter;
        var col = pieceCol + counter;

        // Make sure we stay within our board
        if (row < Board.GetLowerBound(0) || col > Board.GetUpperBound(1)) { break; }

        // Check for a match
        if (Board[row, col] == colorToMatch)
        {
            matchingPieces++;
            if (matchingPieces == 4) return true;
        }
        else { break; }
    }

    // Next check up/left (increment row and decrement column)
    for (int counter = 1; counter < 4; counter++)
    {
        var row = pieceRow + counter;
        var col = pieceCol - counter;

        // Make sure we stay within our board
        if (row > Board.GetUpperBound(0) || col < Board.GetLowerBound(1)) { break; }

        // Check for a match
        if (Board[row, col] == colorToMatch)
        {
            matchingPieces++;
            if (matchingPieces == 4) return true;
        }
        else { break; }
    }

    // If we've gotten this far, then we haven't found a match
    return false;
}