Making it look like the computer is thinking in TicTacToe game

Posted by rage on Stack Overflow See other posts from Stack Overflow or by rage
Published on 2012-10-20T22:49:56Z Indexed on 2012/10/20 23:00 UTC
Read the original article Hit count: 269

Filed under:

here is a slice of code that i've written in VB.net

Private Sub L00_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles L00.Click, L01.Click, L02.Click, L03.Click, L10.Click, L11.Click, L12.Click, L13.Click, L20.Click, L21.Click, L22.Click, L23.Click, L30.Click, L31.Click, L32.Click, L33.Click
    Dim ticTac As Label = CType(sender, Label)
    Dim strRow As String
    Dim strCol As String

    'Once a move is made, do not allow user to change whether player/computer goes first, because it doesn't make sense to do so since the game has already started.
    ComputerFirstStripMenuItem.Enabled = False
    PlayerFirstToolStripMenuItem.Enabled = False

    'Check to make sure clicked tile is a valid tile i.e and empty tile.
    If (ticTac.Text = String.Empty) Then
        ticTac.Text = "X"
        ticTac.ForeColor = ColorDialog1.Color
        ticTac.Tag = 1


        'After the player has made his move it becomes the computers turn.
        computerTurn(sender, e)

    Else
        MessageBox.Show("Please pick an empty tile to make next move", "Invalid Move")
    End If
End Sub

Private Sub computerTurn(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Call Randomize()
    row = Int(4 * Rnd())
    col = Int(4 * Rnd())

    'Check to make sure clicked tile is a valid tile i.e and empty tile.
    If Not ticTacArray(row, col).Tag = 1 And Not ticTacArray(row, col).Tag = 4 Then
        ticTacArray(row, col).Text = "O"
        ticTacArray(row, col).ForeColor = ColorDialog2.Color
        ticTacArray(row, col).Tag = 4

        checkIfGameOver(sender, e)
    Else
        'Some good ole pseudo-recursion(doesn't require a base case(s)).
        computerTurn(sender, e)
    End If

End Sub

Everything works smoothly, except i'm trying to make it seem like the computer has to "think" before making its move. So what i've tried to do is place a System.Threading.Sleep() call in different places in the code above.

The problem is that instead of making the computer look like its thinking, the program waits and then puts the X and O together at the same time. Can someone help me make it so that the program puts an X wherever i click AND THEN wait before it places an O?

Edit: in case any of you are wondering, i realize that the computers AI is ridiculously dumb, but its just to mess around right now. Later on i will implement a serious AI..hopefully.

© Stack Overflow or respective owner

Related posts about vb.net