如何在不单击的情况下更改button.text?

时间:2019-03-02 09:46:07

标签: c# visual-studio winforms

我正在打井字游戏,现在我想做个“ AI”作为对手。对手的“ X”当然会产生自己。我是一个初学者,如果不单击按钮,我不知道如何执行此操作。

3 个答案:

答案 0 :(得分:0)

您可以随机设置;)

例如,如果随机数为4,而button4为空,则button4.text =“ x”。

Random rand = new Random();
int i = rand.Next(0,6);
if (i == 4 && button4.text != "x")
{
    button4.text = "x";
}

我认为很好;)

答案 1 :(得分:0)

您可以在用户按下按钮后简单地更改按钮文本,假设AI的对手是人。

答案 2 :(得分:0)

更改按钮的所有操作。文本为:

button.text = "something";

但是对于第二个问题,您的Ai必须是这样的:

try{
for(var k =-1;k<2;k++){ //k is the parameter responsible for previous and 
//next also up and down cells and even also the upper right , bottom right,bottom left and bottom right cells.
for(var i =0;k<3;i++)
{ 
for(var j =0;k<3;j++)
{
if(x[i,j]==x[i+k,j+k]  && ((i+k<3)&&(j+k<3)) //checks if there is two cells having the same "X" in a row.
{
button.text = "O"
}
}
}
}
}
catch(Exception e){
//nothing

}

我放置了try catch块以避免索引错误。