Page 1 of 1

Tic Tac Toe [UPTATED]

Posted: Sat Apr 03, 2010 9:09 pm
by 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***

Re: Tic Tac Toe

Posted: Sun Apr 04, 2010 5:08 am
by Livengood
The only thing is that the score is not working, i can fix that :D

Re: Tic Tac Toe

Posted: Sun Apr 04, 2010 8:15 am
by hungryhounduk
Hi
Nice little game

well done :)

Chris

Re: Tic Tac Toe

Posted: Sun Apr 04, 2010 1:34 pm
by 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

Re: Tic Tac Toe

Posted: Mon Apr 05, 2010 2:04 am
by GoodGuy17
Very cool little game. :)
Tip: Use labels instead of buttons, use boolean values to see if one has already been clicked. cooll;

Re: Tic Tac Toe

Posted: Tue Apr 06, 2010 11:23 am
by Vitiry
Thanks, Yeah but I just did this fast :p

Re: Tic Tac Toe [UPTATED]

Posted: Wed Apr 07, 2010 12:09 am
by 35000vr
Kool,even though i don't like that game xD

Re: Tic Tac Toe [UPTATED]

Posted: Wed Apr 07, 2010 1:23 am
by 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;

Re: Tic Tac Toe [UPTATED]

Posted: Tue Jul 05, 2011 8:03 am
by Rookie
thanks! fight;