[英]C# WPF How do i change the content to a random button?
我正在嘗試在Silverlight中制作一個Tic Tac Toe游戲,該游戲可以在1人模式下播放。 因此,在單擊任何按鈕並將其內容更改為“ X”或“ O”后,我需要更改隨機按鈕的內容。
我嘗試制作所有按鈕的列表並獲取隨機值:
public List<string> avail = new List<string>() { "button1", "button2", "button3", "button4", "button5", "button6", "button7", "button8", "button9" };
public string Ran()
{
Random b1 = new Random();
int index = b1.Next(avail.Count);
if (index > 0)
return avail[index];
else
return null;
}
但是我不知道如何使我的隨機字符串成為一個按鈕,所以我可以調用以下方法:
public void buttonchange(Button b)
{
if (b.Content.ToString() == "")
if (x == true)
{
x = false;
b.Content = "X";
}
else
{
x = true;
b.Content = "O";
}
if(b.Name!=null)
avail.Remove(b.Name);
}
有任何想法嗎? 謝謝!
而是列出按鈕引用:
public List<Button> avail = new List<Button>() { button1, button2, button3, button4, button5, button6, button7, button8, button9 };
public Button Ran()
{
Random b1 = new Random();
int index = b1.Next(avail.Count);
if (index > 0) {
return avail[index];
} else {
return null;
}
}
public void buttonchange(Button b)
{
if(b != null) {
if (b.Content.ToString() == "") {
b.Content = x ? "X" : "O";
x = !x;
}
avail.Remove(b);
}
}
不確定為什么不使用索引為零的按鈕,但是...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.