gpt4 book ai didi

c# - 为什么我的 disableButtons 方法不起作用?

转载 作者:行者123 更新时间:2023-11-30 20:22:42 26 4
gpt4 key购买 nike

我目前正在为 C# 做一个学校项目,我正在制作井字游戏。不过我有个小问题..

在我构建记分板之前,我的代码过去一直运行良好。

我认为记分牌否决了 disableButtons.. 在我添加记分牌之前它工作得很好..

谁能看看我的代码并告诉我哪里出了问题?

它应该像这样工作:

播放,如果有赢家,禁用按钮。请帮帮我!

编辑:我不知道为什么我的命名空间在这个文本字段中..

namespace Tic_Tac_Toe
{
public partial class Form1 : Form
{
bool turn = true; //true = x en false = o
int turn_count = 0;
static String player1, player2;

public Form1()
{
InitializeComponent();
}

public static void setPlayerName(String n1, String n2)
{
player1 = n1;
player2 = n2;
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Gemaakt door Luca Fraser", "About");
}

private void button_click(object sender, EventArgs e)
{
Button b = (Button)sender;
if (turn)
b.Text = "X";
else
b.Text = "O";

turn = !turn;
b.Enabled = false;
turn_count++;
checkForWinner();

if (turn_count % 2 == 0)
{
lblTurn.Text = player1 + " is aan de beurt";
}
else
{
lblTurn.Text = player2 + " is aan de beurt";
}
}

private void checkForWinner()
{
bool thereIsWinner = false;

//Horizontaal checken
if((btnA1.Text == btnA2.Text) && (btnA2.Text == btnA3.Text) && (!btnA1.Enabled))
thereIsWinner = true;
else if((btnB1.Text == btnB2.Text) && (btnB2.Text == btnB3.Text) && (!btnB1.Enabled))
thereIsWinner = true;
else if((btnC1.Text == btnC2.Text) && (btnC2.Text == btnC3.Text) && (!btnC1.Enabled))
thereIsWinner = true;

//Verticaal checken
if ((btnA1.Text == btnB1.Text) && (btnB1.Text == btnC1.Text) && (!btnA1.Enabled))
thereIsWinner = true;
else if ((btnA2.Text == btnB2.Text) && (btnB2.Text == btnC2.Text) && (!btnA2.Enabled))
thereIsWinner = true;
else if ((btnA3.Text == btnB3.Text) && (btnB3.Text == btnC3.Text) && (!btnA3.Enabled))
thereIsWinner = true;

//Diagonaal checken
if ((btnA1.Text == btnB2.Text) && (btnB2.Text == btnC3.Text) && (!btnA1.Enabled))
thereIsWinner = true;
else if ((btnA3.Text == btnB2.Text) && (btnB2.Text == btnC1.Text) && (!btnC1.Enabled))
thereIsWinner = true;


if (thereIsWinner)
{
disableButtons();

String winner = "";
if (turn)
{
winner = player2;
lblO.Text = (Int32.Parse(lblO.Text) + 1).ToString();
}
else
{
winner = player1;
lblX.Text = (Int32.Parse(lblX.Text) + 1).ToString();
}
MessageBox.Show(winner + " wins!");
}
else
{
if (turn_count == 9)
{
lblDraw.Text = (Int32.Parse(lblDraw.Text) + 1).ToString();
MessageBox.Show("It's a draw!");
}
}

}

private void disableButtons()
{
try
{
foreach (Control c in Controls)
{
Button b = (Button)c;
b.Enabled = false;
}
}
catch { }
}

private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
turn = true;
turn_count = 0;

foreach (Control c in Controls)
{
try
{
Button b = (Button)c;
b.Enabled = true;
b.Text = "";
}
catch { }
}

}

private void Form1_Load(object sender, EventArgs e)
{
Form2 formtwo = new Form2();
formtwo.ShowDialog();
lblXcount.Text = player1;
lblOcount.Text = player2;
lblTurn.Text = player1 + " is aan de beurt";
}

private void mouse_enter(object sender, EventArgs e)
{
Button b = (Button)sender;
if (b.Enabled)
{
if (turn)
{
b.Text = "X";
}
else
b.Text = "O";
}
}

private void mouse_leave(object sender, EventArgs e)
{
Button b = (Button)sender;
if (b.Enabled)
{
b.Text = "";
}

}

private void resetWinCountToolStripMenuItem_Click(object sender, EventArgs e)
{
lblO.Text = "0";
lblX.Text = "0";
lblDraw.Text = "0";
}

private void lblOcount_Click(object sender, EventArgs e)
{

}

private void lblOcount_TextChanged(object sender, EventArgs e)
{

}
}
}

最佳答案

这里:

private void disableButtons()
{
try
{

foreach (Control c in Controls)
{
Button b = (Button)c;
b.Enabled = false;
}
}
catch { }
}

当您迭代一个不是 Button 的控件时,您会遇到异常并且循环结束。

你可以试试这个:

    foreach (Control c in Controls)
{
Button b = c as Button;
if(b != null)
b.Enabled = false;
}

关于c# - 为什么我的 disableButtons 方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30841168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com