首页 > 解决方案 > 如何更改 button.text 而不点击它?

问题描述

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

标签: c#visual-studiowinforms

解决方案


要更改 button.text,您必须做的所有事情是:

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 块以避免索引错误。


推荐阅读