title: How to make a computer move in a single player tic tac toe game
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>This is where I assume the code would go but if I am wrong let me know and I can post the entire code:</p>
  
  <pre><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
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>