在winforms中模拟来自另一个类的按钮单击



我想创建一个小的"AI";对于我用Windows窗体创建的Tic Tac Toe,但我无法模拟人工智能类的人工智能点击按钮。我要么需要这样做,要么只是更改按钮的背景。只有当我在按钮窗体的类中使用非静态方法时,这才有可能。有其他方法可以让它发挥作用吗?

在下面的代码中,您可以看到最重要的代码(我省略了PlayArea类的一些方法(-它不起作用,因为非静态方法PlayArea需要对象引用。SimulateButtonClick-我已经尝试了很多方法来解决这个问题,但像创建一个PlayArea窗体的对象并引用它这样的东西对没有帮助

public partial class PlayArea : Form
{
public static WinningScreen winningScreen;
public int counter = 0;
public int seconds = 0;
public PlayArea()
{
Program.oldCurrentPlayer = Program.currentPlayer;
Program.oldEnemyPlayer = Program.enemyPlayer;
InitializeComponent();
textBox1.BackColor = Program.currentPlayer.color;
textBox2.BackColor = Program.currentPlayer.color;
scoreBox.Text = Program.player1.score.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
ButtonPressed(button1, 0, 0);
}
private void button2_Click(object sender, EventArgs e)
{
ButtonPressed(button2, 0, 1);
}
private void button3_Click(object sender, EventArgs e)
{
ButtonPressed(button3, 0, 2);
}
private void button4_Click(object sender, EventArgs e)
{
ButtonPressed(button4, 1, 0);
}
private void button5_Click(object sender, EventArgs e)
{
ButtonPressed(button5, 1, 1);
}
private void button6_Click(object sender, EventArgs e)
{
ButtonPressed(button6, 1, 2);
}
private void button7_Click(object sender, EventArgs e)
{
ButtonPressed(button7, 2, 0);
}
private void button8_Click(object sender, EventArgs e)
{
ButtonPressed(button8, 2, 1);
}
private void button9_Click(object sender, EventArgs e)
{
ButtonPressed(button9, 2, 2);
}
public void ButtonPressed(Button button, int x, int y)
{
if (Program.currentPlayer.storage[x, y] != true && Program.enemyPlayer.storage[x, y] != true)
{
Program.PlaySound("D:/eigenes/Programmieren/Sounds/mixkit-positive-interface-click-1112.wav");
colorChoice(button);
playerSwitch();
Program.currentPlayer.storage[x, y] = true;
winControl();
if (Program.AIOn == true && Program.unbeatableAIOn != true && AI.isPlaying == false) AI.MakeMove();
if (Program.unbeatableAIOn == true && AI.isPlaying == false) AI.MakeUnbeatableMove();
}
else
{
Program.PlaySound("D:/eigenes/Programmieren/Sounds/mixkit-cool-interface-click-tone-2568.wav");
if (AI.isPlaying == false) AI.MakeMove();
if (AI.isPlaying == false) AI.MakeUnbeatableMove();
}
AI.isPlaying = false;
}
private void colorChoice(Button button)
{
if (Program.currentPlayer.color == System.Drawing.Color.Red) button.BackColor = Color.Red;
if (Program.currentPlayer.color == System.Drawing.Color.Blue) button.BackColor = Color.Blue;
}
public void SimulateButtonClick(int buttonNumber)
{
switch (buttonNumber)
{
case 1: button1.PerformClick(); break;
case 2: button2.PerformClick(); break;
case 3: button3.PerformClick(); break;
case 4: button4.PerformClick(); break;
case 5: button5.PerformClick(); break;
case 6: button6.PerformClick(); break;
case 7: button7.PerformClick(); break;
case 8: button8.PerformClick(); break;
case 9: button9.PerformClick(); break;
}
}
}
class AI
{
public static bool isPlaying = false;
public void MakeMove()
{
Random rnd = new Random();
isPlaying = true;
PlayArea.SimulateButtonClick(rnd.Next(1, 9));
}
public static void MakeUnbeatableMove()
{
}
}

您可以减少一些代码:

private void colorChoice(Button button)
{
button.BackColor = (Program.currentPlayer.color == System.Drawing.Color.Red) ? System.Drawing.Color.Red : System.Drawing.Color.Blue;
}
public void SimulateButtonClick(int buttonNumber)
{
Button btn = this.Controls.Find("button" + buttonNumber, true).FirstOrDefault() as Button;
if (btn != null) 
{
btn.PerformClick();
}
}

最新更新