i need help randomizing where an O will be placed in my naughts and crosses game...here is my select case of possible options Code (Text): Select Case 1 - 9 Case 1 If Panel.Controls.Item(0).Text = "" Then Panel.Controls.Item(0).Text = "O" End If Case 2 If Panel.Controls.Item(1).Text = "" Then Panel.Controls.Item(1).Text = "O" End If Case 3 If Panel.Controls.Item(2).Text = "" Then Panel.Controls.Item(2).Text = "O" End If Case 4 If Panel.Controls.Item(3).Text = "" Then Panel.Controls.Item(3).Text = "O" End If Case 5 If Panel.Controls.Item(4).Text = "" Then Panel.Controls.Item(4).Text = "O" End If Case 6 If Panel.Controls.Item(5).Text = "" Then Panel.Controls.Item(5).Text = "O" End If Case 7 If Panel.Controls.Item(6).Text = "" Then Panel.Controls.Item(6).Text = "O" End If Case 8 If Panel.Controls.Item(7).Text = "" Then Panel.Controls.Item(7).Text = "O" End If Case 9 If Panel.Controls.Item(8).Text = "" Then Panel.Controls.Item(8).Text = "O" End If End Select what i need to happen is for the computer to pick one of those randomly then do the case...for example, it randomly generates case 6 and places an O in the control
this should do you just fine I think Code (Text): Private Function RandNum(LowNum As Long, HighNum As Long) As Long RandNum = Int((HighNum - LowNum + 1) * Rnd + LowNum) End Function
Either call Randnum(2,3) OR Randnum(2,3) Most lickly intrand = randnum(2,3) Just a few things I thought of. These MAY not work I tried my best
hey thanks for the help you 2 it can now put an "O" in all squares...i used this code: Code (Text): Dim c As Integer c = RandNum(0, 8) If Panel.Controls.Item(c).Text = "" Then Panel.Controls.Item(c).Text = "0" End If End Sub Private Function RandNum(ByVal LowNum As Long, ByVal HighNum As Long) As Long RandNum = Int((HighNum - LowNum + 1) * Rnd + LowNum) End Function ill post here if i need more help later on
Just a suggestion: add a "Randomize" to the randNum Function, so that it looks like this: Code (Text): Private Function RandNum(ByVal LowNum As Long, ByVal HighNum As Long) As Long Randomize RandNum = Int((HighNum - LowNum + 1) * Rnd + LowNum) End Function I dunno why, but I read something about that...