如何在C#标签数组中确定同一颜色行中的5个标签
我创建了一个二维标签数组[19,19]。单击后,用户根据迭代将标签颜色更改为黑色或白色。当有五个相同颜色的标签水平排列时,我希望弹出一个消息框。这是在没有递归的情况下完成的。任何帮助都将不胜感激如何在C#标签数组中确定同一颜色行中的5个标签,c#,arrays,label,C#,Arrays,Label,我创建了一个二维标签数组[19,19]。单击后,用户根据迭代将标签颜色更改为黑色或白色。当有五个相同颜色的标签水平排列时,我希望弹出一个消息框。这是在没有递归的情况下完成的。任何帮助都将不胜感激 public partial class Form1 : Form { int labelCount = 0; int iteration = 0; public Label[,] board = new Label[19,19]; const i
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;
}
公共部分类表单1:表单
{
int-labelCount=0;
int迭代=0;
公共标签[,]板=新标签[19,19];
const int WinLength=5;
const int BoardWidth=19;
const int BoardHeight=19;
gamePlay obj=新游戏性();
公共表格1()
{
初始化组件();
}
私有无效标签单击(对象发送方,事件参数e)
{
标签x=(标签)发送方;
if(x.BackColor==Color.Transparent)
{
如果(迭代%2==0)
{
x、 背景色=颜色。黑色;
}
其他的
{
x、 背景色=颜色。白色;
}
迭代++;
}
其他的
{
}
对于(int r=0;r=BoardWidth | | board[r,c2]。背景色!=startColor)
{
赢=假;
打破
}
}
如果(赢)
{
返回起始颜色;
}
}
}
返回null;
}
您只需在方法调用中传递它:
new abc().checkWin(board);
您还需要修复checkWin()
方法签名:
public void checkWin(Label[,] board)
{
...
}
顺便说一句,checkWin()
似乎是一个应该返回bool
(如果赢了,则为true,否则为false)而不是void
int iteration=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;
}
公共标签[,]板=新标签[19,19];
const int WinLength=5;
const int BoardWidth=19;
const int BoardHeight=19;
gamePlay obj=新游戏性();
公共表格1()
{
初始化组件();
}
私有无效标签单击(对象发送方,事件参数e)
{
标签x=(标签)发送方;
if(x.BackColor==Color.Transparent)
{
如果(迭代%2==0)
{
x、 背景色=颜色。黑色;
}
其他的
{
x、 背景色=颜色。白色;
}
迭代++;
}
其他的
{
}
对于(int r=0;r=BoardWidth | | board[r,c2]。背景色!=startColor)
{
赢=假;
打破
}
}
如果(赢)
{
返回起始颜色;
}
}
}
返回null;
}
不要*重复发布同一个问题。它被归类为噪音。@Matt我不久前将这个问题标记为删除,因为它是重复的。