我目前正在尝试制作一个抽搐-战术-脚趾游戏,其中pc随机选择一个数字从一个数组,并根据数字,它选择一个按钮,并改变它的文本属性。如何使它不选择已经具有文本属性的按钮?这是我的代码:
Dim AIChoice As Integer
AIChoice = AIChoic.Next(0, AI.Length - 1)
If AI.GetValue(AIChoice) = 1 And Button1.Text = "" Then
Button1.Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)
ElseIf AI.GetValue(AIChoice) = 2 And Button2.Text = "" Then
Button2.Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)
ElseIf AI.GetValue(AIChoice) = 3 And Button3.Text = "" Then
Button3.Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)
ElseIf AI.GetValue(AIChoice) = 4 And Button4.Text = "" Then
Button4.Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)
ElseIf AI.GetValue(AIChoice) = 5 And Button5.Text = "" Then
Button5.Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)
ElseIf AI.GetValue(AIChoice) = 6 And Button6.Text = "" Then
Button6.Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)
ElseIf AI.GetValue(AIChoice) = 7 And Button7.Text = "" Then
Button7.Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)
ElseIf AI.GetValue(AIChoice) = 8 And Button8.Text = "" Then
Button8.Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)
ElseIf AI.GetValue(AIChoice) = 9 And Button9.Text = "" Then
Button9.Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)我前面还有一个数组,上面有数字1-9。
发布于 2015-09-17 12:16:34
这应该与您发布的代码相同,只是它将继续选择一个随机数,直到找到一个带有空字符串的按钮作为其Text。请注意,最好将按钮创建为数组,而不是创建一个额外的数组来引用它们,但这是最简单的修复方法,我认为您不会遇到其他方法会避免的任何问题。
Dim AllButtons() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9}
Dim AIChoice As Integer
Do
AIChoice = AIChoic.Next(0, AI.Length - 1)
Loop Until AllButtons(AI.GetValue(AIChoice) - 1).Text = ""
AllButtons(AI.GetValue(AIChoice) - 1).Text = "O"
AIChoice = AIChoic.Next(0, AI.Length)https://stackoverflow.com/questions/32621202
复制相似问题