Tic Tac Toe [UPTATED]

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
9 posts Page 1 of 1
Contributors
User avatar
Vitiry
Just Registered
Just Registered
Posts: 9
Joined: Sun Feb 14, 2010 1:37 pm

Tic Tac Toe [UPTATED]
Vitiry
Hi, this is my first tutorial on CodeNstuff.
I'm gonna show you how to make an a tic tac toe application.

STEP 1

First, Press this button :
Image
Then name it what you want :
Image
STEP 2

Now, In properties of Form1, Name it what you want. I'll name my application to Tic Tac Toe.
Image
STEP 3

Now right click on the form like this and press View Code.
Image
STEP 4

This will pop up.
Image
Enter this code under Private Class Form1
Code: Select all
Dim turn As Integer
Just like this :
Image
STEP 5

Now, Add :

9 Buttons & 6 Labels.
Arrange them like this.
Image
STEP 6

Rename all buttons to nothing.
Rename :
Label1 to X
Label2 to Turn
Label3 to X :
Label4 to 0
Label5 to O :
Label6 to 0

Now it will look like this :
Image
STEP 7

Right click on the form again and select View Code.
Now instert this code beetween Dim turn as integer and End Class
Code: Select all
'This is the code that makes you win or loose'
Private Sub win()
        'PLAYER X'
        If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" Then
            MsgBox("Player X Won")
            Label4.Text += 1
            Call disablebuttons()
        ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" Then
            MsgBox("Player X Won")
            Label4.Text += 1
            Call disablebuttons()
        ElseIf Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" Then
            MsgBox("Player X won")
            Label4.Text += 1
            Call disablebuttons()
        ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" Then
            MsgBox("Player X Won")
            Label4.Text += 1
            Call disablebuttons()
        ElseIf Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" Then
            MsgBox("Player X Won")
            Label4.Text += 1
            Call disablebuttons()
        ElseIf Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" Then
            MsgBox("Player X Won")
            Label4.Text += 1
            Call disablebuttons()
        ElseIf Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "X" Then
            MsgBox("Player X Won")
            Label4.Text += 1
            Call disablebuttons()
            'PLAYER O'
        ElseIf Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "O" Then
            MsgBox("Player O Won")
            Label6.Text += 1
            Call disablebuttons()
        ElseIf Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "O" Then
            MsgBox("Player O Won")
            Label6.Text += 1
            Call disablebuttons()
        ElseIf Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "O" Then
            MsgBox("Player O Won")
            Label6.Text += 1
            Call disablebuttons()
        ElseIf Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "O" Then
            MsgBox("Player O Won")
            Label6.Text += 1
            Call disablebuttons()
        ElseIf Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "O" Then
            MsgBox("Player O Won")
            Label6.Text += 1
            Call disablebuttons()
        ElseIf Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "O" Then
            MsgBox("Player O Won")
            Label6.Text += 1
            Call disablebuttons()
        ElseIf Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "0" Then
            MsgBox("Player O Won")
            Label6.Text += 1
            Call disablebuttons()
        End If
    End Sub
Dont worry about all errors, We'll fix it later.

STEP 8

Now, Add this sub beetween End sub and End Class :
Code: Select all
'This code makes the buttons text display X or O it also checks if there is three in a row'
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If turn = 1 Then
            Button2.Text = "O"
            Label1.Text = "X"
        Else
            Button2.Text = "X"
            Label1.Text = "O"
        End If
        turn += 1
        If turn > 2 Then
            turn = 1
        End If
        Call win()
        Button2.Enabled = False
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        If turn = 1 Then
            Button9.Text = "O"
            Label1.Text = "X"
        Else
            Button9.Text = "X"
            Label1.Text = "O"
        End If
        turn += 1
        If turn > 2 Then
            turn = 1
        End If
        Call win()
        Button9.Enabled = False
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If turn = 1 Then
            Button8.Text = "O"
            Label1.Text = "X"
        Else
            Button8.Text = "X"
            Label1.Text = "O"
        End If
        turn += 1
        If turn > 2 Then
            turn = 1
        End If
        Call win()
        Button8.Enabled = False
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        If turn = 1 Then
            Button7.Text = "O"
            Label1.Text = "X"
        Else
            Button7.Text = "X"
            Label1.Text = "O"
        End If
        turn += 1
        If turn > 2 Then
            turn = 1
        End If
        Call win()
        Button7.Enabled = False
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If turn = 1 Then
            Button6.Text = "O"
            Label1.Text = "X"
        Else
            Button6.Text = "X"
            Label1.Text = "O"
        End If
        turn += 1
        If turn > 2 Then
            turn = 1
        End If
        Call win()
        Button6.Enabled = False
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If turn = 1 Then
            Button5.Text = "O"
            Label1.Text = "X"
        Else
            Button5.Text = "X"
            Label1.Text = "O"
        End If
        turn += 1
        If turn > 2 Then
            turn = 1
        End If
        Call win()
        Button5.Enabled = False
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If turn = 1 Then
            Button4.Text = "O"
            Label1.Text = "X"
        Else
            Button4.Text = "X"
            Label1.Text = "O"
        End If
        turn += 1
        If turn > 2 Then
            turn = 1
        End If
        Call win()
        Button4.Enabled = False
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If turn = 1 Then
            Button3.Text = "O"
            Label1.Text = "X"
        Else
            Button3.Text = "X"
            Label1.Text = "O"
        End If
        turn += 1
        If turn > 2 Then
            turn = 1
        End If
        Call win()
        Button3.Enabled = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If turn = 1 Then
            Button1.Text = "O"
            Label1.Text = "X"
        Else
            Button1.Text = "X"
            Label1.Text = "O"
        End If
        turn += 1
        If turn > 2 Then
            turn = 1
        End If
        Call win()
        Button1.Enabled = False
    End Sub

STEP 9

After you have done that add this sub beetween End Sub and End Class.
Code: Select all
'This code makes all buttons non clickable when a round ends'
Private Sub disablebuttons()
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = False
        Button7.Enabled = False
        Button8.Enabled = False
        Button9.Enabled = False
    End Sub
 
'This code makes the buttons able to click when you start a new round'  
 Private Sub enablebuttons()
        Button1.Enabled = True
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        Button6.Enabled = True
        Button7.Enabled = True
        Button8.Enabled = True
        Button9.Enabled = True
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
    End Sub
You will notice that all errors will dissapear.

STEP 10

Now add :

2 Buttons.

In properties of

Button10 rename it to Reset
Button11 rename it to New Round

Arrange the buttons like this :
Image
STEP 11
Right click on the form again and select View Code
Add this code beetween End sub and End Class :
Code: Select all
'This code restarts the whole game plus the score'
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        Label4.Text = 0
        Label6.Text = 0
        Call enablebuttons()
    End Sub
 'This code starts a new round'   
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
     Call enablebuttons()
    End Sub
STEP 12

Now your done! Press the green play button at the top to debug it!


Enjoy!

/Vitiry

***You can arrange the buttons and the labels just like you want, this is just the recommended way***
You do not have the required permissions to view the files attached to this post.
Last edited by Vitiry on Tue Apr 06, 2010 2:22 pm, edited 10 times in total.
Signature isnt necessary.
User avatar
Livengood
Serious Programmer
Serious Programmer
Posts: 445
Joined: Tue Feb 16, 2010 6:24 am

Re: Tic Tac Toe
Livengood
The only thing is that the score is not working, i can fix that :D
Image
User avatar
hungryhounduk
VIP - Site Partner
VIP - Site Partner
Posts: 2870
Joined: Mon Jul 27, 2009 11:58 am

Re: Tic Tac Toe
hungryhounduk
Hi
Nice little game

well done :)

Chris
Image
User avatar
Vitiry
Just Registered
Just Registered
Posts: 9
Joined: Sun Feb 14, 2010 1:37 pm

Re: Tic Tac Toe
Vitiry
Livengood wrote:
The only thing is that the score is not working, i can fix that :D
The score works?.. But you can make it better :)
hungryhounduk wrote:
Hi
Nice little game

well done :)

Chris
Thanks!

/Vitiry
Signature isnt necessary.
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: Tic Tac Toe
GoodGuy17
Very cool little game. :)
Tip: Use labels instead of buttons, use boolean values to see if one has already been clicked. cooll;
User avatar
Vitiry
Just Registered
Just Registered
Posts: 9
Joined: Sun Feb 14, 2010 1:37 pm

Re: Tic Tac Toe
Vitiry
Thanks, Yeah but I just did this fast :p
Signature isnt necessary.
User avatar
35000vr
Top Poster
Top Poster
Posts: 176
Joined: Sat Mar 06, 2010 5:09 pm

Re: Tic Tac Toe [UPTATED]
35000vr
Kool,even though i don't like that game xD
‼ <----- Copy it,it is together and if you backspace it it will both erase
☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"<-----Some Cool symbols.
♂<-----Boy Symbol :)
²ƽ<--------Mini 2 and 5!
ð<----Not sure what it is.
☺☻<-----Smiles
♪♫<----Music Notes
Others:ß┬ƒ○║■ã¿┼↑
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: Tic Tac Toe [UPTATED]
CodenStuff
Hello,

Nice Tic Tac Toe game, I call it naughts and crosses though lol. You should keep going with this and add some nice graphics/sounds and a computer player cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Rookie
Top Poster
Top Poster
Posts: 85
Joined: Thu May 19, 2011 10:28 am

Re: Tic Tac Toe [UPTATED]
Rookie
thanks! fight;
9 posts Page 1 of 1
Return to “Tutorials”