(C#,连接四)- 给下面的按钮上色
(C#, Connect four) - Coloring the button below
我正在参加编程考试,我几乎完成了游戏。我决定使用 connect 4,因为它似乎是处理一些 algrothicy 特性的好机会,因为我不太习惯那种编码。
问题很简单:我需要给按钮上色以创建砖块掉落的效果。我想做一些类似 big ass if 语句的事情来检查坐标是否 == this 然后它的 this 按钮是否被着色。否则如果...
但这会很痛苦,而且不是很漂亮。我在想是否有可能或多或少地获取表单中的所有按钮,然后从函数中获取 x 和 y 生成一个字符串,然后找到一个具有该名称的按钮
我的按钮标签为 btxy
但是我搞砸了数组第一个按钮是:0,0
第一个按钮的名称是 bt11 x 上的下一个是 bt12
我来自丹麦,所以一些变量是丹麦语的,这个函数也是:
private void farv(int x, int y)
{
x += 1;
y += 1;
MessageBox.Show("bt" + y.ToString() + x.ToString());
foreach (Control c in this.Controls)
{
if (c is Button)
{
if (c.Name == "bt" + x.ToString() + y.ToString())
{
if (playerValue == 1)
{
c.BackColor = Color.Blue;
}
else if (playerValue == 10)
{
c.BackColor = Color.Red;
}
}
}
}
}
这就是着色法。
我这样称呼它:
temp = 0;
while (temp < 6)
{
MessageBox.Show("While");
farv(rowNr, temp);
temp += 1;
}
我真的无法让它正常工作。有什么建议吗?结果比我想象的难多了,哈哈。
不确定是哪一部分 "isn't working",但这对我有用:
private void farv(int x, int y)
{
var buttonName = string.Format("bt{0}{1}", x + 1, y + 1);
var buttonControl = Controls.Find(buttonName, true).FirstOrDefault();
if (buttonControl != null)
{
buttonControl.BackColor = GetColorForPlayer(playerValue);
}
}
private Color GetColorForPlayer(int playerValue)
{
Color defaultColor = SystemColors.Control;
switch (playerValue)
{
case 1:
return Color.Blue;
case 10:
return Color.Red;
default:
return defaultColor;
}
}
假设你有一块 6 x 6 的板,你可以这样使用:
for (int x = 0; x < 6; x++)
{
for (int y = 0; y < 6; y++)
{
farv(x, y);
}
}
我正在参加编程考试,我几乎完成了游戏。我决定使用 connect 4,因为它似乎是处理一些 algrothicy 特性的好机会,因为我不太习惯那种编码。
问题很简单:我需要给按钮上色以创建砖块掉落的效果。我想做一些类似 big ass if 语句的事情来检查坐标是否 == this 然后它的 this 按钮是否被着色。否则如果...
但这会很痛苦,而且不是很漂亮。我在想是否有可能或多或少地获取表单中的所有按钮,然后从函数中获取 x 和 y 生成一个字符串,然后找到一个具有该名称的按钮
我的按钮标签为 btxy 但是我搞砸了数组第一个按钮是:0,0 第一个按钮的名称是 bt11 x 上的下一个是 bt12
我来自丹麦,所以一些变量是丹麦语的,这个函数也是:
private void farv(int x, int y)
{
x += 1;
y += 1;
MessageBox.Show("bt" + y.ToString() + x.ToString());
foreach (Control c in this.Controls)
{
if (c is Button)
{
if (c.Name == "bt" + x.ToString() + y.ToString())
{
if (playerValue == 1)
{
c.BackColor = Color.Blue;
}
else if (playerValue == 10)
{
c.BackColor = Color.Red;
}
}
}
}
}
这就是着色法。 我这样称呼它:
temp = 0;
while (temp < 6)
{
MessageBox.Show("While");
farv(rowNr, temp);
temp += 1;
}
我真的无法让它正常工作。有什么建议吗?结果比我想象的难多了,哈哈。
不确定是哪一部分 "isn't working",但这对我有用:
private void farv(int x, int y)
{
var buttonName = string.Format("bt{0}{1}", x + 1, y + 1);
var buttonControl = Controls.Find(buttonName, true).FirstOrDefault();
if (buttonControl != null)
{
buttonControl.BackColor = GetColorForPlayer(playerValue);
}
}
private Color GetColorForPlayer(int playerValue)
{
Color defaultColor = SystemColors.Control;
switch (playerValue)
{
case 1:
return Color.Blue;
case 10:
return Color.Red;
default:
return defaultColor;
}
}
假设你有一块 6 x 6 的板,你可以这样使用:
for (int x = 0; x < 6; x++)
{
for (int y = 0; y < 6; y++)
{
farv(x, y);
}
}