I need help

Discussion in 'Code Snippets and Tutorials' started by test123, Mar 23, 2007.

  1. test123

    test123 Level IV

    Joined:
    Nov 3, 2006
    Messages:
    935
    Likes Received:
    0
    Location:
    Australia
    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):
    1. Select Case 1 - 9
    2.             Case 1
    3.                 If Panel.Controls.Item(0).Text = "" Then
    4.                     Panel.Controls.Item(0).Text = "O"
    5.                 End If
    6.             Case 2
    7.                 If Panel.Controls.Item(1).Text = "" Then
    8.                     Panel.Controls.Item(1).Text = "O"
    9.                 End If
    10.             Case 3
    11.                 If Panel.Controls.Item(2).Text = "" Then
    12.                     Panel.Controls.Item(2).Text = "O"
    13.                 End If
    14.             Case 4
    15.                 If Panel.Controls.Item(3).Text = "" Then
    16.                     Panel.Controls.Item(3).Text = "O"
    17.                 End If
    18.             Case 5
    19.                 If Panel.Controls.Item(4).Text = "" Then
    20.                     Panel.Controls.Item(4).Text = "O"
    21.                 End If
    22.             Case 6
    23.                 If Panel.Controls.Item(5).Text = "" Then
    24.                     Panel.Controls.Item(5).Text = "O"
    25.                 End If
    26.             Case 7
    27.                 If Panel.Controls.Item(6).Text = "" Then
    28.                     Panel.Controls.Item(6).Text = "O"
    29.                 End If
    30.             Case 8
    31.                 If Panel.Controls.Item(7).Text = "" Then
    32.                     Panel.Controls.Item(7).Text = "O"
    33.                 End If
    34.             Case 9
    35.                 If Panel.Controls.Item(8).Text = "" Then
    36.                     Panel.Controls.Item(8).Text = "O"
    37.                 End If
    38.  
    39.         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
     
  2. expon

    expon Administrator
    Staff Member

    Joined:
    Oct 30, 2006
    Messages:
    1,393
    Likes Received:
    56
    Location:
    UK
    this should do you just fine I think

    Code (Text):
    1.  
    2. Private Function RandNum(LowNum As Long, HighNum As Long) As Long
    3.        RandNum = Int((HighNum - LowNum + 1) * Rnd + LowNum)
    4. End Function
    5.  
     
  3. test123

    test123 Level IV

    Joined:
    Nov 3, 2006
    Messages:
    935
    Likes Received:
    0
    Location:
    Australia
    hey

    how do i call that function?
     
  4. the_skip

    the_skip Level IV

    Joined:
    Dec 25, 2006
    Messages:
    2,354
    Likes Received:
    1
    Location:
    Indiana
    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
     
  5. test123

    test123 Level IV

    Joined:
    Nov 3, 2006
    Messages:
    935
    Likes Received:
    0
    Location:
    Australia
    hey

    thanks for the help you 2 it can now put an "O" in all squares...i used this code:

    Code (Text):
    1. Dim c As Integer
    2.         c = RandNum(0, 8)
    3.  
    4.         If Panel.Controls.Item(c).Text = "" Then
    5.             Panel.Controls.Item(c).Text = "0"
    6.         End If
    7.     End Sub
    8.     Private Function RandNum(ByVal LowNum As Long, ByVal HighNum As Long) As Long
    9.         RandNum = Int((HighNum - LowNum + 1) * Rnd + LowNum)
    10.     End Function
    ill post here if i need more help later on
     
  6. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Just a suggestion: add a "Randomize" to the randNum Function, so that it looks like this:
    Code (Text):
    1.     Private Function RandNum(ByVal LowNum As Long, ByVal HighNum As Long) As Long
    2.         Randomize
    3.         RandNum = Int((HighNum - LowNum + 1) * Rnd + LowNum)
    4.     End Function
    I dunno why, but I read something about that...