Results 1 to 8 of 8
  1. #1
    jimmy541's Avatar
    Join Date
    Mar 2010
    Gender
    male
    Posts
    1
    Reputation
    10
    Thanks
    0

    [Tutorial]Tic Tac Toe Game[Leeched]

    [Leeched Tutorial]

    This tutorial will show you how to make a tic tac toe in visual basic 2008:

    Credits To ReflectionForBrain.com for this tutorial.

    The source code is also available for download

    How to Make Tic Tac Toe



    For this tutorial you need 12 Buttons, 2 Labels, and a timer :

    Set the timer property Enable to True then change its interval to 1

    Set the labels Visible property to False

    Add the controls to the form like the following picture:



    Then we start adding the codes:

    First we add the following right after Public Class Form1:


    Code:
    Dim random As String = 1
    We add the code for the timer: (The timer is used to generate a random number)

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
    random += 1
    
    If random = 10 Then
    
    random = 1
    
    End If
    
    End Sub
    We add the following code so computer makes the move:

    Code:
    Public Sub PC()
    If random = 1 And Button1.Enabled = True Then
    Button1.Text = "O"
    Button1.Enabled = False
    End If
    If random = 2 And Button2.Enabled = True Then
    Button2.Text = "O"
    Button2.Enabled = False
    End If
    If random = 3 And Button3.Enabled = True Then
    Button3.Text = "O"
    Button3.Enabled = False
    End If
    If random = 4 And Button4.Enabled = True Then
    Button4.Text = "O"
    Button4.Enabled = False
    End If
    If random = 5 And Button5.Enabled = True Then
    Button5.Text = "O"
    Button5.Enabled = False
    End If
    If random = 6 And Button6.Enabled = True Then
    Button6.Text = "O"
    Button6.Enabled = False
    End If
    If random = 7 And Button7.Enabled = True Then
    Button7.Text = "O"
    Button7.Enabled = False
    End If
    If random = 8 And Button8.Enabled = True Then
    Button8.Text = "O"
    Button8.Enabled = False
    End If
    If random = 9 And Button9.Enabled = True Then
    Button9.Text = "O"
    Button9.Enabled = False
    End If
    End Sub
    The computer plays its first move randomly. After the user makes more than one move, the computer can't use the above code because it has to decide according of the user input. Therefore, we also add the following code. The following code is too long, (human being can calculate that in less than a second)

    Code:
    Public Sub Options()
    'If user hits 1
    If Label1.Text = 1 Then
    If Button2.Text = "X" And Button3.Enabled = True Then
    With Button3
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button3.Text = "X" And Button2.Enabled = True Then
    With Button2
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button5.Text = "X" And Button9.Enabled = True Then
    With Button9
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button9.Text = "X" And Button5.Enabled = True Then
    With Button5
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button4.Text = "X" And Button7.Enabled = True Then
    With Button7
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button7.Text = "X" And Button4.Enabled = True Then
    With Button4
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button8.Text = "X" Or Button6.Text = "X" Then
    hit()
    End If
     
    End If
    'If user hits 2
    If Label1.Text = 2 Then
    If Button1.Text = "X" And Button3.Enabled = True Then
    With Button3
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button1.Text = "X" And Button1.Enabled = True Then
    With Button1
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button5.Text = "X" And Button8.Enabled = True Then
    With Button8
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button8.Text = "X" And Button5.Enabled = True Then
    With Button5
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button6.Text = "X" Or Button4.Text = "X" Or Button7.Text = "X" Or Button9.Text = "X" Then
    hit()
    End If
    End If
    'If user hits 3
    If Label1.Text = 3 Then
    If Button1.Text = "X" And Button2.Enabled = True Then
    With Button2
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button2.Text = "X" And Button1.Enabled = True Then
    With Button1
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button6.Text = "X" And Button9.Enabled = True Then
    With Button9
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button9.Text = "X" And Button6.Enabled = True Then
    With Button6
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button5.Text = "X" And Button7.Enabled = True Then
    With Button7
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button7.Text = "X" And Button5.Enabled = True Then
    With Button5
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button8.Text = "X" Or Button4.Text = "X" Then
    hit()
    End If
    End If
    'If user hits 4
    If Label1.Text = 4 Then
    If Button1.Text = "X" And Button7.Enabled = True Then
    With Button7
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button7.Text = "X" And Button1.Enabled = True Then
    With Button1
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button5.Text = "X" And Button6.Enabled = True Then
    With Button6
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button6.Text = "X" And Button5.Enabled = True Then
    With Button5
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button2.Text = "X" Or Button3.Text = "X" Or Button8.Text = "X" Or Button9.Text = "X" Then
    hit()
    End If
    End If
    'If user hits 5
    If Label1.Text = 5 Then
    If Button1.Text = "X" And Button9.Enabled = True Then
    With Button9
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button9.Text = "X" And Button1.Enabled = True Then
    With Button1
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button2.Text = "X" And Button8.Enabled = True Then
    With Button8
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button8.Text = "X" And Button2.Enabled = True Then
    With Button2
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button3.Text = "X" And Button7.Enabled = True Then
    With Button7
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button7.Text = "X" And Button3.Enabled = True Then
    With Button3
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button6.Text = "X" And Button4.Enabled = True Then
    With Button4
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button4.Text = "X" And Button6.Enabled = True Then
    With Button6
    .Text = "O"
    .Enabled = False
    End With
    End If
    End If
     
    'If user hits 6
    If Label1.Text = 6 Then
    If Button3.Text = "X" And Button9.Enabled = True Then
    With Button9
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button9.Text = "X" And Button3.Enabled = True Then
    With Button3
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button5.Text = "X" And Button4.Enabled = True Then
    With Button4
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button4.Text = "X" And Button5.Enabled = True Then
    With Button5
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button1.Text = "X" Or Button2.Text = "X" Or Button7.Text = "X" Or Button8.Text = "X" Then
    hit()
    End If
    End If
     
    'If user hits 7
    If Label1.Text = 7 Then
    If Button1.Text = "X" And Button4.Enabled = True Then
    With Button4
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button4.Text = "X" And Button1.Enabled = True Then
    With Button1
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button5.Text = "X" And Button3.Enabled = True Then
    With Button3
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button3.Text = "X" And Button5.Enabled = True Then
    With Button5
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button8.Text = "X" And Button9.Enabled = True Then
    With Button9
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button9.Text = "X" And Button8.Enabled = True Then
    With Button8
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button6.Text = "X" Or Button2.Text = "X" Then
    hit()
    End If
    End If
     
    'If user hits 8
    If Label1.Text = 8 Then
    If Button2.Text = "X" And Button5.Enabled = True Then
    With Button5
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button5.Text = "X" And Button2.Enabled = True Then
    With Button2
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button9.Text = "X" And Button7.Enabled = True Then
    With Button7
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button7.Text = "X" And Button9.Enabled = True Then
    With Button9
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button6.Text = "X" Or Button3.Text = "X" Or Button1.Text = "X" Or Button4.Text = "X" Then
    hit()
    End If
    End If
    'If user hits 9
    If Label1.Text = 9 Then
    If Button6.Text = "X" And Button3.Enabled = True Then
    With Button3
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button3.Text = "X" And Button6.Enabled = True Then
    With Button6
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button5.Text = "X" And Button1.Enabled = True Then
    With Button1
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button1.Text = "X" And Button5.Enabled = True Then
    With Button5
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button8.Text = "X" And Button7.Enabled = True Then
    With Button7
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button7.Text = "X" And Button8.Enabled = True Then
    With Button8
    .Text = "O"
    .Enabled = False
    End With
    ElseIf Button2.Text = "X" Or Button4.Text = "X" Then
    hit()
    End If
    End If
    The following code will check if there is a winner and show the result on a message box:

    Code:
    Public Sub CheckIfComputerWins()
    If Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "O" _
    Or Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "O" _
    Or Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "O" _
    Or Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "O" _
    Or Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "O" _
    Or Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "O" _
    Or Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "O" _
    Or Button7.Text = "O" And Button5.Text = "O" And Button3.Text = "O" Then
    MsgBox("Computer Wins")
    Button1.Text = ""
    Button1.Enabled = True
    Button2.Text = ""
    Button2.Enabled = True
    Button3.Text = ""
    Button3.Enabled = True
    Button4.Text = ""
    Button4.Enabled = True
    Button5.Text = ""
    Button5.Enabled = True
    Button6.Text = ""
    Button6.Enabled = True
    Button7.Text = ""
    Button7.Enabled = True
    Button8.Text = ""
    Button8.Enabled = True
    Button9.Text = ""
    Button9.Enabled = True
    ElseIf Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" _
    Or Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "X" _
    Or Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" _
    Or Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" _
    Or Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" _
    Or Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" _
    Or Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" _
    Or Button7.Text = "X" And Button5.Text = "X" And Button3.Text = "X" Then
    MsgBox("Congratulations, You Win")
    Button1.Text = ""
    Button1.Enabled = True
    Button2.Text = ""
    Button2.Enabled = True
    Button3.Text = ""
    Button3.Enabled = True
    Button4.Text = ""
    Button4.Enabled = True
    Button5.Text = ""
    Button5.Enabled = True
    Button6.Text = ""
    Button6.Enabled = True
    Button7.Text = ""
    Button7.Enabled = True
    Button8.Text = ""
    Button8.Enabled = True
    Button9.Text = ""
    Button9.Enabled = True
    End If
    End Sub
    We also need to add a code when the user doesn't make a proper move: (When the computer doesn't have to think because the user didn't make a threatening move)

    Code:
    Public Sub hit()
    For Each ctl As Control In Me.Controls
    If Label1.Text < 9 Then
    If (ctl.Name.StartsWith("Button" & Label1.Text + 1)) Then
    Dim btn As Button = DirectCast(ctl, Button)
    If btn.Enabled = True Then
    btn.Text = "O"
    btn.Enabled = False
    Else
    Label1.Text = Label1.Text + 1
    End If
    End If
    Else
    If (ctl.Name.StartsWith("Button1")) Then
    Dim btn As Button = DirectCast(ctl, Button)
    If btn.Enabled = True Then
    btn.Text = "O"
    btn.Enabled = False
    Else
    Label1.Text = Label1.Text + 1
    End If
    End If
    End If
    Next
    End Sub
    Now we program PC Start Button (Button10): When the user ask the computer to start first:

    Code:
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    
    Timer1.Stop()
    
    Button1.Text = "O"
    
    Label1.Text = 1
    
    Button1.Enabled = False
    
    Button10.Enabled = False
    
    End Sub
    The last piece of code when the user hit Restart Button: (button11)

    Code:
    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    Button1.Text = ""
    Button1.Enabled = True
    Button2.Text = ""
    Button2.Enabled = True
    Button3.Text = ""
    Button3.Enabled = True
    Button4.Text = ""
    Button4.Enabled = True
    Button5.Text = ""
    Button5.Enabled = True
    Button6.Text = ""
    Button6.Enabled = True
    Button7.Text = ""
    Button7.Enabled = True
    Button8.Text = ""
    Button8.Enabled = True
    Button9.Text = ""
    Button9.Enabled = True
    Button10.Enabled = True
    Timer1.Start()
    End Sub
    Last edited by jimmy541; 03-04-2010 at 05:53 PM. Reason: Credits was wrong. It is ReflectionForBrain.com that;s the name of the company, not ReflectionBrain.

  2. #2
    jacksimon1's Avatar
    Join Date
    Sep 2008
    Gender
    male
    Location
    hi
    Posts
    32
    Reputation
    10
    Thanks
    2
    Eh it's actually pretty easy to make this if you know how to make a keygen then this is a snap.

  3. #3
    Pixipixel_'s Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    VCExpress.exe || France :D
    Posts
    2,087
    Reputation
    27
    Thanks
    742
    My Mood
    Cool
    Yeah easy to make, but tic tac toe are so fun :3
    Thanks for the tut, it will help beginners i guess =)

  4. #4
    XGelite_backup's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Posts
    40
    Reputation
    10
    Thanks
    3
    My Mood
    Tired
    idontlikeleechedtutorials.

  5. #5
    Tom's Avatar
    Join Date
    Sep 2007
    Gender
    male
    Location
    in my gf's pants
    Posts
    1,861
    Reputation
    37
    Thanks
    594
    My Mood
    Cool
    Cool,I will make this later on.Thanks

  6. The Following User Says Thank You to Tom For This Useful Post:

    NextGen1 (03-07-2010)

  7. #6
    Houston's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    The Netherlands
    Posts
    1,941
    Reputation
    175
    Thanks
    2,468
    My Mood
    Blah
    I don't understand this game

  8. #7
    docoon's Avatar
    Join Date
    Nov 2009
    Gender
    male
    Location
    behind you
    Posts
    101
    Reputation
    10
    Thanks
    13
    My Mood
    Amazed
    please put picture I'm a newbie with this ..

  9. #8
    NextGen1's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Not sure really.
    Posts
    6,312
    Reputation
    382
    Thanks
    3,019
    My Mood
    Amazed
    Oh, Grrrrrrrrrrrrrrrr.... OP has one post, there is a picture or two, and you bumped a topic that's over a month old.

    Read MPGH Rules, and more specifically VB rules, no bumping if last post in thread is >= 7 days.

    Create a thread or PM someone.

    Sorry Closed.

    What a shame.
    Last edited by NextGen1; 04-20-2010 at 03:46 PM.


     


     


     



    The Most complete application MPGH will ever offer - 68%




Similar Threads

  1. [Release] My Fucking Simple BIG TIC TAC TOE Game
    By Lyoto Machida in forum C++/C Programming
    Replies: 49
    Last Post: 06-29-2011, 10:29 AM
  2. [Release]Tic Tac Toe Game by GuGy
    By GuGy in forum Visual Basic Programming
    Replies: 9
    Last Post: 01-31-2011, 02:23 PM
  3. BLack Horseman Tic Tac Toe Game
    By aLcohoL_95 in forum General
    Replies: 15
    Last Post: 04-14-2010, 01:43 PM
  4. Just owned some Tic Tac Toe :)
    By skip in forum C++/C Programming
    Replies: 1
    Last Post: 08-30-2008, 05:03 AM
  5. ARUN BEAT ME ON TIC TAC TOE
    By CAPTAIN OBVIOUS in forum Flaming & Rage
    Replies: 22
    Last Post: 04-30-2008, 05:33 PM

Tags for this Thread