Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about Control Typeof

Status
Not open for further replies.

bigz60

Technical User
Apr 18, 2007
40
US
Hello.
I am trying to create a simple tic-tac-toe game as a learning experience. I am stuck on getting the computer to play "his turn."

What I am trying to do is create a random number, and then loop through all of the buttons on the form that are empty, counting each empty button until the count has reached the random number. Here is an example:

Code:
dim rdm as new random()
dim targetnumber as integer
dim count as integer = 0
dim buttons as Control
targetnumber = rdm.next(10,100)

While count < targetnumber
    For each buttons in Me.controls
         If type(buttons) is Button then 
              If buttons.text = "" then
               count +=1
                    If count = targetnumber thne
                    buttons.text="O"
                 Exit for
           End if
          End If
          End If
     Next
End While

I have done some debug.writeline and it appears that the "count" variable is not increasing. I'm not sure if i have the for structured incorrectly, or what.

Thank you.
 
You may find this helpful:
Also, I knocked up something in VB6 (you should be able to translate pretty easily), that does nothing but put a O at random in an empty square for the computer's turn. (an array of 9 text boxes is assumed.)

Code:
Private Sub txtSquare_Click(Index As Integer)
Dim i As Integer
Dim count As Integer
Dim OpenSquares() As Integer
Dim intRnd As Integer
If txtSquare(Index).Text = "" Then
    txtSquare(Index) = "X"
End If
For i = 0 To 8
    If txtSquare(i) = "" Then
        count = count + 1
        ReDim Preserve OpenSquares(count - 1)
        OpenSquares(count - 1) = i
    End If
Next
intRnd = Int(Rnd(Now) * count)
txtSquare(OpenSquares(intRnd)) = "O"
End Sub

HTH

Bob
 
Example of 2.0 Random, in use:


Dim a As Integer = New Random().Next / 9

-The answer to your problem may not be the answer to your question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top