【发布时间】:2019-03-02 20:29:49
【问题描述】:
我正在做一个井字游戏,现在我想制作一种“AI”作为对手。对手的“X”当然是自己生成的。我是初学者,如果不单击按钮,我不知道该怎么做。
【问题讨论】:
-
让计算机自动设置按钮的文本很容易。困难的部分是让它学习游戏的策略。
标签: c# visual-studio winforms
我正在做一个井字游戏,现在我想制作一种“AI”作为对手。对手的“X”当然是自己生成的。我是初学者,如果不单击按钮,我不知道该怎么做。
【问题讨论】:
标签: c# visual-studio winforms
你可以随意做一个;)
例如随机数为 4 且 button4 为空 button4.text = "x"。
Random rand = new Random();
int i = rand.Next(0,6);
if (i == 4 && button4.text != "x")
{
button4.text = "x";
}
我觉得不错;)
【讨论】:
假设 AI 的对手是人类,您可以在用户按下按钮后简单地更改按钮文本
【讨论】:
要更改 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块来避免索引错误。
【讨论】: