-1

I'm trying to make a single player tic tac toe game. I already have 2 player working using some codes I found online but I'm not having any luck inputting the codes for single player with what I have already. I have a start menu that asks if you want to play 1 or 2 players then it goes to the game form to start the game (where all the main code is) Also I am new to vb and don't know all the terminology yet so if you can help out please be detailed and leave comments in the code.

What I'd like to do is make the computer move on a random choice for the first move then based on what is already played it will make the choice off of that. I think I can get that part on my own I just need help figuring out how to incorporate the random number generator into the code I already have.

One last thing I would like to know, is this vb.net or vb6? I don't really know the difference since this is my first Visual Basic project.

This is where I assume the code would go but if I am wrong let me know and I can post the entire code:

Public Class Form1
Dim flag As Boolean = False
Private mute As Integer = 0

Private Sub Buttons(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click

    If flag = False Then
        ' DirectCast(sender, Button).Image = (My.Resources.Mario)
        DirectCast(sender, Button).Text = "X"
        flag = True
        My.Computer.Audio.Play(My.Resources.fireball, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player O "
        buttonDisable()
        computerMove()
    Else
        ' DirectCast(sender, Button).Image = (My.Resources.Luigi)
        DirectCast(sender, Button).Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If muteOff.Visible = True Then
        My.Computer.Audio.Stop()
    End If
    win()
    checkDraw()
End Sub

AND THIS IS THE CODE BELOW I WROTE TO MAKE A "SMART" COMPUTER MOVE I think it can be improved but I'm not sure how. Note: its not the entire code needed but I'm sure you can get the hint of what I was doing.

Private Sub computerMove()
    'XXO
    If Button1.Text = "X" And Button2.Text = "X" And Button3.Enabled = True Then
        Button3.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button2.Text = "X" And Button3.Text = "X" And Button1.Enabled = True Then
        Button1.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button4.Text = "X" And Button5.Text = "X" And Button6.Enabled = True Then
        Button6.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button6.Text = "X" And Button5.Text = "X" And Button4.Enabled = True Then
        Button4.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button7.Text = "X" And Button8.Text = "X" And Button9.Enabled = True Then
        Button9.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button9.Text = "X" And Button8.Text = "X" And Button7.Enabled = True Then
        Button7.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button1.Text = "X" And Button4.Text = "X" And Button7.Enabled = True Then
        Button7.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button7.Text = "X" And Button4.Text = "X" And Button1.Enabled = True Then
        Button1.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button2.Text = "X" And Button5.Text = "X" And Button8.Enabled = True Then
        Button8.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button8.Text = "X" And Button5.Text = "X" And Button2.Enabled = True Then
        Button2.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button3.Text = "X" And Button6.Text = "X" And Button9.Enabled = True Then
        Button9.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button9.Text = "X" And Button6.Text = "X" And Button3.Enabled = True Then
        Button3.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button1.Text = "X" And Button5.Text = "X" And Button9.Enabled = True Then
        Button9.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button9.Text = "X" And Button5.Text = "X" And Button1.Enabled = True Then
        Button1.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button3.Text = "X" And Button5.Text = "X" And Button7.Enabled = True Then
        Button7.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button7.Text = "X" And Button5.Text = "X" And Button3.Enabled = True Then
        Button3.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    'XOX
    If Button1.Text = "X" And Button7.Text = "X" And Button4.Enabled = True Then
        Button4.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button1.Text = "X" And Button3.Text = "X" And Button2.Enabled = True Then
        Button2.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
    If Button1.Text = "X" And Button9.Text = "X" And Button5.Enabled = True Then
        Button5.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button4.Text = "X" And Button6.Text = "X" And Button5.Enabled = True Then
        Button5.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button2.Text = "X" And Button8.Text = "X" And Button5.Enabled = True Then
        Button5.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button3.Text = "X" And Button9.Text = "X" And Button6.Enabled = True Then
        Button6.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button3.Text = "X" And Button7.Text = "X" And Button5.Enabled = True Then
        Button5.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button7.Text = "X" And Button9.Text = "X" And Button8.Enabled = True Then
        Button8.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If

    If Button1.Text = "X" And Button3.Text = "X" And Button2.Enabled = True Then
        Button2.Text = "O"
        flag = False
        My.Computer.Audio.Play(My.Resources.marioJump, AudioPlayMode.Background)
        playerTurn.Text = " Turn: Player X "
        buttonDisable()
    End If
End Sub

I have one last problem. Not only with this part but in other areas of the code too I would like to make a function? (I'm not good with the terminology so I'll just say) button. Make a button that when clicked it will enable a event (which I can do) but also disable a event somewhere else in the code. Example: When I am at the start screen and it ask if I want to play 1 or 2 players I want the single player button to enable the computerMove() sub and disable or change the code somewhere else.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Looks a lot like VB.NET – jessehouwing Dec 28 '14 at 19:51
  • If I understand correctly what you want is a tic tac toe AI. Take a look here : http://stackoverflow.com/questions/15753572/simple-tic-tac-toe-ai This is for javascript but it should not be to hard to adapt for VB. – tigrou Dec 28 '14 at 19:53
  • I'm not sure if I understand that. Even if it was in vb it might take me a minute to get it ;) Thank you tho! – Anthony Bova II Dec 28 '14 at 20:57

1 Answers1

0

To answer your first question about a random computer move: You could just create a function to return a random Integer between (including) 1 and 9 since you have 9 buttons. I recently had to do something similar and my result looks like this.

Dim rnd As New System.Random
Private Function generateRandomInt(ByVal min As Integer, ByVal max As Integer)
    Dim value As Integer = rnd.Next(min, max + 1)
    Return value
End Function

Then just call

generateRandomInt(1, 9)

You can check the Random class for more background information.

And about the CPU behavior: I am no game developer but your approach seems a bit over-complex. Rather than creating if-cases for each possible scenario, you should probably write a method that checks a field (or button in your case) and its surrounding fields dynamically to determine whether or not the human player is close to having a row of 3. I'm sure though that there are many examples and tutorials about this which you can just copy.

FlyingM
  • 217
  • 2
  • 7
  • 17