【问题标题】:How can I change the button.text without it being clicked on?如何更改 button.text 而不点击它?
【发布时间】:2019-03-02 20:29:49
【问题描述】:

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

【问题讨论】:

  • 让计算机自动设置按钮的文本很容易。困难的部分是让它学习游戏的策略。

标签: c# visual-studio winforms


【解决方案1】:

你可以随意做一个;)

例如随机数为 4 且 button4 为空 button4.text = "x"。

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

我觉得不错;)

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      要更改 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块来避免索引错误。

      【讨论】:

      • 我有点困惑。你在检查什么?你能解释一下代码吗?
      • 哪一部分不明白?
      • 什么是 i 和 j?
      • i 和 j 是“for”循环的索引我认为您必须在尝试在这里学习之前提高您的编程技能,因为这个网站不是帮助台。
      猜你喜欢
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2017-03-10
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多