如何在 C# 标签数组中确定一行相同颜色的 5 个标签

How to determine 5 labels in a row of the same color in an array of C# labels

我创建了一个二维标签数组 [19,19]。单击时,用户根据迭代将标签的颜色更改为黑色或白色。当有五个相同颜色的标签水平排列时,我希望弹出一个消息框。这是在没有递归的情况下完成的。任何帮助将不胜感激。

    public partial class Form1 : Form
{
int labelCount = 0;
        int iteration = 0;
        public Label[,] board = new Label[19,19];
        const int WinLength = 5;
        const int BoardWidth = 19;
        const int BoardHeight = 19;
        gamePlay obj = new gamePlay();
    public Form1()
    {
        InitializeComponent();
}

private void labelClick (object sender, EventArgs e)
{
Label x = (Label)sender;

            if (x.BackColor == Color.Transparent)
            {
                if (iteration % 2 == 0)
                {
                    x.BackColor = Color.Black;
                }
                else
                {
                    x.BackColor = Color.White;
                }
                iteration++;
            }
            else
            {

            }
for (int r = 0; r < BoardHeight; r++)
        {
            for (int c = 0; c < BoardWidth; c++)
            {
                if (board[r, c] == x)
                {
                    Color? winner = obj.CheckForWinner(board, r, c);
                    if (winner == Color.Black)
                    {
                        MessageBox.Show("Black is the winner!");
                    }
                    else if (winner == Color.White)
                    {
                        MessageBox.Show("White is the winner!");
                }
                // else winner is null, meaning no winner yet. 
            }
        }
    }
    private int[] FindClickedLabelCoordinates(Label[,] board, Label label)
    {
        for (int r = 0; r < BoardHeight; r++)
        {
            for (int c = 0; c < BoardWidth; c++)
            {
                if (board[r, c] == label)
                    return new int[] { r, c };
            }
        }
        return null;
        }
}

    class gamePlay
{
        const int WinLength = 5;
const int BoardWidth = 19;
const int BoardHeight = 19;
private Color? CheckForWinner(Label[,] board, int r, int c)
{
    Color startColor = board[r, c].BackColor;
    for (int c1 = c - WinLength + 1; c1 <= c; c1++)
    {
        if (c1 >= 0 && c1 < BoardWidth && board[r, c1].BackColor == startColor)
        {
            MessageBox.Show("you win!");
            bool win = true;
            for (int c2 = c1 + 1; c2 < c1 + WinLength; c2++)
            {
                if (c2 < 0 || c2 >= BoardWidth || board[r, c2].BackColor != startColor)
                {
                    win = false;
                    break;
                }
            }
            if (win)
            {

                return startColor;
            }

        }
    }
    return null;
}

您只需在方法调用中传递它:

new abc().checkWin(board);

您还需要修复您的 checkWin() 方法签名:

public void checkWin(Label[,] board)
{
    ...
}

顺便说一句,checkWin() 似乎是一种方法,应该 return 一个 bool (如果赢了则为真,否则为假)而不是 void.

        int iteration = 0;
        public Label[,] board = new Label[19,19];
        const int WinLength = 5;
        const int BoardWidth = 19;
        const int BoardHeight = 19;
        gamePlay obj = new gamePlay();
    public Form1()
    {
        InitializeComponent();
}

private void labelClick (object sender, EventArgs e)
{
Label x = (Label)sender;

            if (x.BackColor == Color.Transparent)
            {
                if (iteration % 2 == 0)
                {
                    x.BackColor = Color.Black;
                }
                else
                {
                    x.BackColor = Color.White;
                }
                iteration++;
            }
            else
            {

            }
for (int r = 0; r < BoardHeight; r++)
        {
            for (int c = 0; c < BoardWidth; c++)
            {
                if (board[r, c] == x)
                {
                    Color? winner = obj.CheckForWinner(board, r, c);
                    if (winner == Color.Black)
                    {
                        MessageBox.Show("Black is the winner!");
                    }
                    else if (winner == Color.White)
                    {
                        MessageBox.Show("White is the winner!");
                }
                // else winner is null, meaning no winner yet. 
            }
        }
    }
    private int[] FindClickedLabelCoordinates(Label[,] board, Label label)
    {
        for (int r = 0; r < BoardHeight; r++)
        {
            for (int c = 0; c < BoardWidth; c++)
            {
                if (board[r, c] == label)
                    return new int[] { r, c };
            }
        }
        return null;
        }
}

    class gamePlay
{
        const int WinLength = 5;
const int BoardWidth = 19;
const int BoardHeight = 19;
private Color? CheckForWinner(Label[,] board, int r, int c)
{
    Color startColor = board[r, c].BackColor;
    for (int c1 = c - WinLength + 1; c1 <= c; c1++)
    {
        if (c1 >= 0 && c1 < BoardWidth && board[r, c1].BackColor == startColor)
        {
            MessageBox.Show("you win!");
            bool win = true;
            for (int c2 = c1 + 1; c2 < c1 + WinLength; c2++)
            {
                if (c2 < 0 || c2 >= BoardWidth || board[r, c2].BackColor != startColor)
                {
                    win = false;
                    break;
                }
            }
            if (win)
            {

                return startColor;
            }

        }
    }
    return null;
}