对于学校的最终项目,我正在制作Tic Tac Toe游戏。基本前提是按钮单击按钮文本转到“X”并且按钮被禁用,然后将随机选择它周围的按钮以转到“O”。 (我正试图制作假人工智能)
好的,问题是;即使按钮在单击时被禁用,当您单击其附近的另一个按钮时,也可以禁用另一个按钮上的文本。例如,如果您按下按钮A1并且机器人在B2上放置“O”,然后您按下A2,即使A1被禁用并且上面有“X”,机器人也会将A1更改为“O”
那你怎么不允许代码能够改变文本呢? 这是代码:Pastebin Of code
答案 0 :(得分:0)
在您的代码中有大量类似的shutdown()
语句块,您可以在其中测试变量awaitTermination()
(机器人做出的选择),然后根据该值设置按钮的文本:< / p>
if
如果您添加rnd
条件来测试该按钮是否为If rnd = 1 Then
Button3.Text = "O"
Button3.Enabled = False
End If
If rnd = 2 Then
Button6.Text = "O"
Button6.Enabled = False
End If
If rnd = 3 Then
Button5.Text = "O"
Button5.Enabled = False
End If
,那么您应该对问题中的内容进行修复。
请尝试这样:
And
等等,对于您的代码中的各类Enabled
语句,例如这些语句。
答案 1 :(得分:0)
//assign bool for 9 buttons
Dim btnOne as bool
Dim btnTwo as bool
Dim aiIsDone as bool;
aiIsDone = false;
//and so on..
Do While (aiIsDone == false) //Loop until it finds an unoccupied value
//assign random numbers
Dim value As Integer = CInt(Int((9 * Rnd()) + 1))
if (value == 1) then
if(btnOne == false) then //Check the random result if it is not occupied yet
// Some code
btnOne = true //turn the button to occupied so it will not pick the same button again
aiIsDone = True //this will exit the loop
// continue statement for 9 buttons
end if
Loop
//Do something if all 9 are occupied (value is true)