How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
Go Back  Xtreme Visual Basic Talk > > > How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ


Reply
 
Thread Tools Display Modes
  #1  
Old 02-28-2009, 01:12 AM
Eprouvez Eprouvez is offline
Newcomer
 
Join Date: Jan 2009
Posts: 5
Thumbs up How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ


EprouveZ’s Tic-Tac-Toe Tutorial!

Welcome to my tutorial on how to make a Tic-Tac-Toe game in VB2008!
Please Subscribe!

http://www.youtube.com/user/Eprouvez

Lets get started...

First Step

Start a new project, name it “Tic-Tac-Toe” or whatever floats your boat.
http://i724.photobucket.com/albums/w...ouvezz/One.jpg

Second Step

Apply these settings to your Form1 (Except for the name, make it whatever).

Change the…

FormBorderStyle – FixedToolWindow
StartPosition – CentreScreen
TopMost – True
Text – To whatever you want to name the program.

Step Three

Make One Button, and then adjust the size until it looks fairly square.
Copy that button another EIGHT times so there is Nine in total.

http://i724.photobucket.com/albums/w...vezz/Three.jpg

Step Four

Change the font of the buttons to something fancy looking, and make the text bigger.

http://i724.photobucket.com/albums/w...uvezz/Four.jpg

Step Five

Add three timers.

Timer1: Is to see if someone has won the game.
Timer2: Is to see if there is a draw.
Timer3: Is activated by Timer2, that when it reaches its timer interval, it will reset the game.

Step Six

This is where we start the code.
Double-click on Timer2.
You want to make it so Timer2 picks up if no-one wins, in that case, when all the buttons are disabled (We’ll come back to this later on).

So what you want to do is make it so it checks that all nine buttons are disabled, and if they are we can have a message box, or a label saying “No Winner!” or something.
The code should look something like this…

http://i724.photobucket.com/albums/w...ouvezz/Six.jpg

Once you’ve decided what you want it to show, put in the code. I prefer labels for this one.

Here is the code, to make it easier for you.

Code:
Public Class Form1

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        'Checks for a draw
        If Button1.Enabled = False Then
            If Button2.Enabled = False Then
                If Button3.Enabled = False Then
                    If Button4.Enabled = False Then
                        If Button5.Enabled = False Then
                            If Button6.Enabled = False Then
                                If Button7.Enabled = False Then
                                    If Button8.Enabled = False Then
                                        If Button9.Enabled = False Then
                                            'Here you can put what you want it to do when noone wins!
                                            'I'm going to use a label
                                            'Make a new label, and put it where you want
                                            Label3.Text = "No Winner!"
                                            Timer3.Enabled = True
                                            Timer3.Start()
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub
End Class
Step Seven

Finish your layout, make it something like this for beginners.

http://i724.photobucket.com/albums/w...vezz/Seven.jpg

On this form, it has…

10 Buttons
2 Groupboxes
4 Labels
1 Checkbox (This is for a random wave effect)
4 Radiobuttons
&
3 Timers

BE CAREFUL, if you are going to use my code, it may effect how your applications work because my labels are mixed about, the rest of the Form is in order going Left to right, down.

Step Eight

I’ve made it really simple for you, and I’m gonna make it simplier, by supplying code.
All though, you may want to change the Timer intervals to make them faster, I suggest…

Timer1: 10
Timer2: 10
Timer3: 1500

Here’s the code.

http://www.mediafire.com/?ghgzmhdznwm

Just pretty much take out all the code you already have and put this in.
It’ll make your life a whole lot easier.

Thanks for reading this tutorial.

Regards,
Eprouvez

P.S Here’s the project files & etc! =]

http://www.mediafire.com/?zigqzmz0yuy
http://www.mediafire.com/?m1jiigimnyq
Reply With Quote
  #2  
Old 02-28-2009, 02:54 PM
Iceplug's Avatar
IceplugHow To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Is it truly necessary to need three timers for a Tic Tac Toe game? Also, two timers with the same interval is redundant (and, I think, more wasteful as the compiled app uses two threads when one would suffice) and you should be able to do it all with one timer at the most, but whatever floats the boat, I guess. Just something I would consider for future projects.
A couple more things:
The code that you posted above can be trimmed down considerably by using And or AndAlso:

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
'Checks for a draw
If Button1.Enabled = False AndAlso Button2.Enabled = False AndAlso Button3.Enabled = False AndAlso Button4.Enabled = False AndAlso Button5.Enabled = False AndAlso Button6.Enabled = False AndAlso Button7.Enabled = False AndAlso Button8.Enabled = False AndAlso Button9.Enabled = False Then
'Here you can put what you want it to do when noone wins!
'I'm going to use a label
'Make a new label, and put it where you want
Label3.Text = "No Winner!"
Timer3.Enabled = True
Timer3.Start()
End If
End Sub
or you can use the fact that an If statement only requires conditions anyway:

If Not (Button1.Enabled OrElse Button2.Enabled OrElse Button3.Enabled OrElse Button4.Enabled OrElse Button5.Enabled OrElse Button6.Enabled OrElse Button7.Enabled OrElse Button8.Enabled OrElse Button9.Enabled) Then
'Here you can put what you want it to do when noone wins!
'I'm going to use a label
'Make a new label, and put it where you want
Label3.Text = "No Winner!"
Timer3.Enabled = True
Timer3.Start()
End If
This one says pretty directly: "If none of the buttons are enabled then ..."

Another example of what I mentioned above with using conditions directly in an If statement:
If RadioButton1.Checked = True Then 'Player One is X, Player Two is O
can be shortened to
If RadioButton1.Checked Then 'Player One is X, Player Two is O


If I recall correctly, the Timer's .Start does the same thing as setting .Enabled to True, but I'm not 100% sure on that.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 02-28-2009, 09:17 PM
Eprouvez Eprouvez is offline
Newcomer
 
Join Date: Jan 2009
Posts: 5
Thumbs up Thanks!

Hey Iceplug,
Thanks for the advice, I don't actually know so much of Vb2008, but I'm learning as I go, thank you for telling me how to shorten the code! I just used what I knew, and I'll be able to Use these for all my other projects now.
Also using Timer1.enabled = true does actually start the timer, I just use Timer1.Start() just in case I'm wrong haha.
Thank you for taking your time to read over my project, as this is only for beginners, If statements I find could be better to start off with.

Oh by the way, the reason I used three timers is because...

Timer1 is to check for the win.
Timer2 is to check for a draw.
Timer3 makes a 1 and a half second break before resetting all the buttons.

With my code, It would seem a bit "Crowded" To have it all in one timer, maybe with your code it would seem a lot easier, and could actually put it in one Timer.

Thanks again,
Eprouvez.
Reply With Quote
  #4  
Old 03-01-2009, 08:46 AM
Iceplug's Avatar
IceplugHow To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Consolidating timers only requires a little bit of forethought, a variable and some math using addition or the Mod operator.

Consider a situation similar to what you have above: I have three timers on the form: one performs what we will refer to as 'Operation X', two performs 'Operation Y', and three performs 'Operation Z'. (Each operation contains the code that you have for that timer event.)
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Operation X
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
'Operation Y
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
'Operation Z
End Sub
The interval settings are as you have above:
Timer1 has interval of 10, Timer2 interval is 10, and Timer3 interval is 1500.
First of all, timer1 and timer2 have the same interval, so you can simply have timer1 execute Operation X and Operation Y:
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Operation X
'Operation Y
End Sub
This will allow you to get rid of Timer2.
To get rid of timer3, you can declare a variable in the declarations area of your form. (Note, in VB2003, the declarations area is any place outside of a Sub or a Function. I assume the same applies to VB2008. However, it is typically a good idea to put them all before the first Sub in your Form code.)
Code:
Dim Counter As Integer
What Counter is going to do is it is going to count up the number of milliseconds until it reaches the desired interval for your third timer (so it will count up to 1500). To make it count up based on the time elapsed, we can put it in Timer1!
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Operation X
'Operation Y
Counter += 10  'Counts up 10 milliseconds.
End Sub
The next thing is to check when Counter has reached your desired interval. You can do this with an If statement immediately after you raise Counter by 10:
Code:
If Counter >= 1500 Then
    'Operation Z.
    Counter = 0
End If
Notice that Counter must be set back to 0, otherwise it will continuously run.
And now, you can get rid of Timer3.

If you need to disable one of the operations, you can also declare some Boolean data types in the declarations section
Dim XEnable, YEnable, ZEnable As Boolean
And you can skip disabled operations with another If block
If XEnable Then
'Operation X
End If
etc.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #5  
Old 03-11-2009, 02:41 AM
Eprouvez Eprouvez is offline
Newcomer
 
Join Date: Jan 2009
Posts: 5
Default

Haha thanks man!
=]
Reply With Quote
  #6  
Old 03-11-2009, 07:27 AM
darkforcesjedi's Avatar
darkforcesjediHow To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,964
Default

Why are you using timers at all? The button's Click event fires when it is clicked. You only need to check for a win each time a button is clicked. Not 100 times per second. Also, the resolution of the timer is only around 50ms. Setting it to a shorter interval won't make it run any faster.
__________________
To err is human; to debug, divine.
Reply With Quote
  #7  
Old 03-11-2009, 10:28 AM
AtmaWeapon's Avatar
AtmaWeaponHow To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I agree with darkforcesjedi; there's no real need for timers at all. When you click a button, make a move, determine if there's a winner/draw, then wait for the next button click. Having a timer wastes resources. I've attached a project that goes a little overboard with OOP but separates the logic of the tic-tac-toe grid from the form itself; this lets the form worry more about UI than about the logic of tic-tac-toe.
Attached Files
File Type: zip TicTacToe.zip (14.6 KB, 27 views)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 03-27-2009, 04:54 AM
Eprouvez Eprouvez is offline
Newcomer
 
Join Date: Jan 2009
Posts: 5
Talking Hey

I know that there was no real reason for timer OTHER than it taking up less space instead of putting the code under each button.

Just pointing out, I'm just a beginner! Haha.
Reply With Quote
  #9  
Old 04-01-2009, 07:05 PM
Iceplug's Avatar
IceplugHow To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

If you are writing a tutorial for beginners, I don't think you should try to push the efficiency concept right off the bat, however (!) the code should try to look simple enough so that a beginner doesn't get overwhelmed, which is why I suggested at least getting rid of multiple timers and the heavily nested If statements. Granted, Tic Tac Toe, while a simple game in itself, is not entirely without planning.
(And yes, like darkforcesjedi and AtmaWeapon have said, you shouldn't need a timer... I would use a custom Function to check for wins at the end of the Button Click procedure... that way you avoid having "the code under each button")
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #10  
Old 04-28-2009, 10:57 AM
Eprouvez Eprouvez is offline
Newcomer
 
Join Date: Jan 2009
Posts: 5
Talking Hello

Hahaha... Just one tiny little problemo...

I don't know how Hahaha

Would you be able to give me a brief over run on how it is done?
Please?
Reply With Quote
  #11  
Old 04-28-2009, 12:18 PM
AtmaWeapon's Avatar
AtmaWeaponHow To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Read the thread. I bet someone attached an example to their post.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
 
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
How To: VB2008 Tic-Tac-Toe Tutorial - EprouveZ
 
-->