Free Online Articles Directory
18.11.2008 Sign In Register Hello Guest
Email:
Password:
Remember Me 
forgot your password?


Simple Tic-tac-toe Game Logic

Author: Ariful Ambia Author Ranking Blue | Posted: 13-08-2008 | Comments: 0 | Views: 114 | Rating:  (94) Article Popularity - Blue (?) Got a Question? Ask.
Sign Up Now!

In this article I will discuss about a very simple game. Yes, it is a Tic-Tak-Toe game of 3X3 dimension. It is a very simple game and I believe each and every one knows the rules of this game. So I am not going to mention anything about the game rules here. This article also gives you an idea about AI (Artificial Inelegancy) used in computer game. It is like giving thinking ability to you PC.

In this game you can play against computer. That is man vs. computer. The game is developed in VB6.0 and coded in very easy way to make the algorithm easy to understand. There are comments for each chunk of code which acts like a particular AI or games rule. I hope anyone can understand what I am going to do seeing the codes and comments. I used some Matrix formula to calculate/ find next move after a Human move.

Here is the complete listing of source code.



Private Sub Command1_Click(Index As Integer)
If Command1(Index).Caption = "" Then
Command1(Index).Caption = "X"
Call computer
Call win
Else
Beep
End If
End Sub
Public Sub computer()
Dim i, numb1, numb0, cou1, cou2 As Integer
'************CALCULATE THE NUMBER OF "X"************
For i = 0 To 8
If Command1(i).Caption = "X" Then
numb1 = numb1 + 1
End If
Next i
'*****************WIN OPPERTUNITY************
If (Command1(0).Caption = "O" And Command1(1).Caption = "O" And Command1(2).Caption = "") Then
Command1(2).Caption = "O"
Exit Sub
End If

If (Command1(0).Caption = "O" And Command1(2).Caption = "O" And Command1(1).Caption = "") Then
Command1(1).Caption = "O"
Exit Sub
End If

If (Command1(0).Caption = "O" And Command1(3).Caption = "O" And Command1(6).Caption = "") Then
Command1(6).Caption = "O"
Exit Sub
End If

If (Command1(0).Caption = "O" And Command1(6).Caption = "O" And Command1(3).Caption = "") Then
Command1(3).Caption = "O"
Exit Sub
End If
'****************************************
If (Command1(8).Caption = "O" And Command1(5).Caption = "O" And Command1(2).Caption = "") Then
Command1(2).Caption = "O"
Exit Sub
End If

If (Command1(8).Caption = "O" And Command1(2).Caption = "O" And Command1(5).Caption = "") Then
Command1(5).Caption = "O"
Exit Sub
End If

If (Command1(8).Caption = "O" And Command1(7).Caption = "O" And Command1(6).Caption = "") Then
Command1(6).Caption = "O"
Exit Sub
End If

If (Command1(8).Caption = "O" And Command1(6).Caption = "O" And Command1(7).Caption = "") Then
Command1(7).Caption = "O"
Exit Sub
End If
'********************************
If (Command1(6).Caption = "O" And Command1(7).Caption = "O" And Command1(8).Caption = "") Then
Command1(8).Caption = "O"
Exit Sub
End If

If (Command1(6).Caption = "O" And Command1(3).Caption = "O" And Command1(0).Caption = "") Then
Command1(0).Caption = "O"
Exit Sub
End If
'*****************************************
If (Command1(2).Caption = "O" And Command1(1).Caption = "O" And Command1(0).Caption = "") Then
Command1(0).Caption = "O"
Exit Sub
End If

If (Command1(2).Caption = "O" And Command1(5).Caption = "O" And Command1(8).Caption = "") Then
Command1(8).Caption = "O"
Exit Sub
End If

'***************LOGICS IF THE CENTER BOX CONTAIN "O"***********
If Command1(4).Caption = "O" Then
'*********************CROSS(X) LOGICS*****************
For q = 0 To 8 Step 2
If q = 4 Then
GoTo 2
End If
If Command1(q).Caption = "O" Then
n = 4 * 2 - q
If Command1(n).Caption = "" Then
Command1(n).Caption = "O"
Exit Sub
End If
End If
2 Next q
'*********************PLUSE(+) LOGICS*****************
For w = 1 To 7 Step 2
If w = 4 Then
GoTo 4
End If
If Command1(w).Caption = "O" Then
n = 4 * 2 - w
If Command1(n).Caption = "" Then
Command1(n).Caption = "O"
Exit Sub
End If
End If
4 Next w
End If

'************************PROCTECT GAME***********************
If (Command1(0).Caption = "X" And Command1(1).Caption = "X" And Command1(2).Caption = "") Then
Command1(2).Caption = "O"
Exit Sub
End If

If (Command1(0).Caption = "X" And Command1(2).Caption = "X" And Command1(1).Caption = "") Then
Command1(1).Caption = "O"
Exit Sub
End If

If (Command1(0).Caption = "X" And Command1(3).Caption = "X" And Command1(6).Caption = "") Then
Command1(6).Caption = "O"
Exit Sub
End If

If (Command1(0).Caption = "X" And Command1(6).Caption = "X" And Command1(3).Caption = "") Then
Command1(3).Caption = "O"
Exit Sub
End If
'****************************************
If (Command1(8).Caption = "X" And Command1(5).Caption = "X" And Command1(2).Caption = "") Then
Command1(2).Caption = "O"
Exit Sub
End If

If (Command1(8).Caption = "X" And Command1(2).Caption = "X" And Command1(5).Caption = "") Then
Command1(5).Caption = "O"
Exit Sub
End If

If (Command1(8).Caption = "X" And Command1(7).Caption = "X" And Command1(6).Caption = "") Then
Command1(6).Caption = "O"
Exit Sub
End If

If (Command1(8).Caption = "X" And Command1(6).Caption = "X" And Command1(7).Caption = "") Then
Command1(7).Caption = "O"
Exit Sub
End If

If (Command1(1).Caption = "X" And Command1(2).Caption = "X" And Command1(0).Caption = "") Then
Command1(0).Caption = "O"
Exit Sub
End If

If (Command1(6).Caption = "X" And Command1(7).Caption = "X" And Command1(8).Caption = "") Then
Command1(8).Caption = "O"
Exit Sub
End If
If (Command1(2).Caption = "X" And Command1(5).Caption = "X" And Command1(8).Caption = "") Then
Command1(8).Caption = "O"
Exit Sub
End If

If (Command1(3).Caption = "X" And Command1(6).Caption = "X" And Command1(0).Caption = "") Then
Command1(0).Caption = "O"
Exit Sub
End If


'**************PUT "O" IN CENTER BOX IF BLANK****************
If (numb1 = 1 And Command1(4).Caption = "") Then
Command1(4).Caption = "O"
Exit Sub
End If
'***************LOGICS IF THE CENTER BOX CONTAIN "X"***********
If Command1(4).Caption = "X" Then

'*********************CROSS(X) LOGICS*****************
For q = 0 To 8 Step 2
If q = 4 Then
GoTo 5
End If
If Command1(q).Caption = "X" Then
n = 4 * 2 - q
If Command1(n).Caption = "" Then
Command1(n).Caption = "O"
Exit Sub
End If
End If
5 Next q
'*********************PLUSE(+) LOGICS*****************
For q = 1 To 7 Step 2
If q = 4 Then
GoTo 6
End If
If Command1(q).Caption = "X" Then
n = 4 * 2 - q
If Command1(n).Caption = "" Then
Command1(n).Caption = "O"
Exit Sub
End If
End If
6 Next q


If Command1(0).Caption = "" Then
Command1(0).Caption = "O"
Exit Sub
End If

If Command1(2).Caption = "" Then
Command1(2).Caption = "O"
Exit Sub
End If

If Command1(6).Caption = "" Then
Command1(6).Caption = "O"
Exit Sub
End If

If Command1(8).Caption = "" Then
Command1(8).Caption = "O"
Exit Sub
End If
End If

'*******************MASTER LOGICS(1)*********
If (Command1(3).Caption = "X" And Command1(1).Caption = "X") Then
If Command1(0).Caption = "" Then
Command1(0).Caption = "O"
Exit Sub
End If
End If

If (Command1(1).Caption = "X" And Command1(5).Caption = "X") Then
If Command1(2).Caption = "" Then
Command1(2).Caption = "O"
Exit Sub
End If
End If

If (Command1(5).Caption = "X" And Command1(7).Caption = "X") Then
If Command1(8).Caption = "" Then
Command1(8).Caption = "O"
Exit Sub
End If
End If

If (Command1(3).Caption = "X" And Command1(7).Caption = "X") Then
If Command1(6).Caption = "" Then
Command1(6).Caption = "O"
Exit Sub
End If
End If

'************************master logics(2)***************

If (Command1(3).Caption = "X") Then
If Command1(2).Caption = "X" And Command1(0).Caption = "" Then
Command1(0).Caption = "O"
Exit Sub
End If

If Command1(8).Caption = "X" And Command1(6).Caption = "" Then
Command1(6).Caption = "O"
Exit Sub
End If
End If

If (Command1(5).Caption = "X") Then
If Command1(0).Caption = "X" And Command1(2).Caption = "" Then
Command1(2).Caption = "O"
Exit Sub
End If

If Command1(6).Caption = "X" And Command1(8).Caption = "" Then
Command1(8).Caption = "O"
Exit Sub
End If
End If

If (Command1(7).Caption = "X") Then
If Command1(0).Caption = "X" And Command1(6).Caption = "" Then
Command1(6).Caption = "O"
Exit Sub
End If

If Command1(2).Caption = "X" And Command1(8).Caption = "" Then
Command1(8).Caption = "O"
Exit Sub
End If
End If

If (Command1(1).Caption = "X") Then
If Command1(6).Caption = "X" And Command1(0).Caption = "" Then
Command1(0).Caption = "O"
Exit Sub
End If
If Command1(8).Caption = "X" And Command1(2).Caption = "" Then
Command1(2).Caption = "O"
Exit Sub
End If

End If

'**********************************************
If (numb1 = 5) Then
MsgBox "*****Game over*****"
Call newer
GoTo 10
End If

'*************Master Logics(1)*****************
For K = 1 To 7 Step 2
If Command1(4).Caption = "O" And Command1(K).Caption = "" Then
Command1(K).Caption = "O"
Exit Sub
End If
Next K

8 cou1 = Int(Rnd * 9)
If Command1(cou1).Caption = "" Then
Command1(cou1).Caption = "O"
Else
GoTo 8
End If

10 End Sub

Private Sub exit_Click()
End
End Sub

Private Sub Form_Load()
Open "C:WIN.TXT" For Append As #1
Close #1
End Sub

Private Sub new_Click()
Call newer
End Sub
Public Sub win()
'**********************YOU WIN*************
If (Command1(1).Caption = "X" And Command1(0).Caption = "X" And Command1(2).Caption = "X") Then
MsgBox "***You win***"
Call newer
End If

If (Command1(0).Caption = "X" And Command1(4).Caption = "X" And Command1(8).Caption = "X") Then
MsgBox "***You win***"
Call newer
End If

If (Command1(0).Caption = "X" And Command1(3).Caption = "X" And Command1(6).Caption = "X") Then
MsgBox "***You win***"
Call newer
End If

If (Command1(1).Caption = "X" And Command1(4).Caption = "X" And Command1(7).Caption = "X") Then
MsgBox "***You win***"
Call newer
End If

If (Command1(2).Caption = "X" And Command1(5).Caption = "X" And Command1(8).Caption = "X") Then
MsgBox "***You win***"
Call newer
End If

If (Command1(3).Caption = "X" And Command1(4).Caption = "X" And Command1(5).Caption = "X") Then
MsgBox "***You win***"
Call newer
End If

If (Command1(6).Caption = "X" And Command1(7).Caption = "X" And Command1(8).Caption = "X") Then
MsgBox "***You win***"
Call newer
End If


If (Command1(6).Caption = "X" And Command1(4).Caption = "X" And Command1(2).Caption = "X") Then
MsgBox "***You win***"
Call newer
End If



'*****************YOU LOST*************
If (Command1(1).Caption = "O" And Command1(0).Caption = "O" And Command1(2).Caption = "O") Then
MsgBox "***You loset***"
Call newer
End If
If (Command1(4).Caption = "O" And Command1(0).Caption = "O" And Command1(8).Caption = "O") Then
MsgBox "***You loset***"
Call newer
End If
If (Command1(3).Caption = "O" And Command1(0).Caption = "O" And Command1(6).Caption = "O") Then
MsgBox "***You loset***"
Call newer
End If
If (Command1(1).Caption = "O" And Command1(4).Caption = "O" And Command1(7).Caption = "O") Then
MsgBox "***You loset***"
Call newer
End If
If (Command1(2).Caption = "O" And Command1(5).Caption = "O" And Command1(8).Caption = "O") Then
MsgBox "***You loset***"
Call newer
End If
If (Command1(3).Caption = "O" And Command1(4).Caption = "O" And Command1(5).Caption = "O") Then
MsgBox "***You loset***"
Call newer
End If
If (Command1(6).Caption = "O" And Command1(7).Caption = "O" And Command1(8).Caption = "O") Then
MsgBox "***You loset***"
Call newer
End If
If (Command1(6).Caption = "O" And Command1(4).Caption = "O" And Command1(2).Caption = "O") Then
MsgBox "***You loset***"
Call newer
End If
End Sub

Public Sub newer()
For i = 0 To 8
Command1(i).Caption = ""
Next i
End Sub




You can download full project from BD Experts site

Rate this Article: Current: 0 / 5 stars - 0 vote(s).

Article Source: http://www.articlesbase.com/programming-articles/simple-tictactoe-game-logic-519923.html

Print this Article Print article   Email to a Friend Send to friend   Publish this Article on your Website Publish this Article   Send Author Feedback Author feedback  
Ariful AmbiaAbout the Author:

I am basically a Application/web developer. Microsoft technology is my working tool. I am interested with new advance technologies of programming and database. I am also running a website http://www.bd-experts.com which is mostly a article sharing website.

Submitting articles has become one of the most popular means to drive traffic to your website and promote yourself and your business. Join us today - It's Free!

Article Comments

Comment on this article Comment on this article
Your Name
Your Email:
Comment Body
Enter Validation Code: Captcha


Related Articles

The Relevance of Information Processing to Jurors' Decision-making
By: David S. Davis, Ph.D. | 08/12/2006 | Law
This article discusses the first of three major factors that account for the difficulty the defense has with presenting a general causation case to a jury.

Creation Theories
By: Stacey T Pollock | 25/05/2008 | Philosophy
How to look at creation theories.

Health & Safety News
By: Logic SHE Solutions | 14/01/2007 | Press Releases
If you would like to read about any articles concerning health and safety, asbestos or any other topics. Please click on the PDF icon of your choice.

Living Fearlessly
By: Anthony Bennett | 04/08/2008 | NLP Hypnosis
SOME SAY HOW WE DEAL WITH FEAR IS A CHOICE! But for thousands a spider, a thunderstorm, a short hop on a plane or standing up to talk at a business meeting can send them into a panic so severe that it apears to them to be beyond their control. What can be done to get back in control and overcome panic and anxious tought patterns?

Of Wigs and Well Dressed Dogs!
By: Steve Cowan | 06/04/2007 | Pets
As a baby, everything makes sense. If your Dad flew around the room after breakfast, hey, why not? As you get older, however, it gets less understandable by the day.....

What is Manufacturing Resource Planning and Scheduling Software?
By: Vince Levenda | 20/08/2008 | Strategic Planning
Production scheduling enables forecasting and planning of manufacturing resource requirements and machine capacity utilisation. This is to conserve manufacturing resources. Optimum resource utilisation can maximise productivity and thus minimise production costs. Production scheduling enables planning and tracking of manufacturing orders across multiple areas of processing. Scheduling is based on order priority. Priorities for production are assigned for each proprocessing area on each shift.

Keeping a Distance From Religion
By: Dr. Randy Wysong | 05/07/2007 | Home & Family
Good logic and science require that questions be faced with an open and receptive mind. Yet the notions of creation and a greater intelligence than ours are rejected a priori by materialists not because of the weight of evidence but because of rejection of religion.

Defining Intelligence
By: Crisan Flaviu | 25/08/2008 | Culture
Intelligence represents the amount of experience a person accumulates in a given time interval. High intelligence means that a large amount of experience is obtained over a short interval of time, while low intelligence means that little experience is obtained over a long period of time. Simply put, intelligence measures the rate at which a person acquires new experience.

Got a Question? Ask.

Ask the community a question about this article:

Frequently Asked Questions

Database programmer
By: Ryan | 17-10-2008
what is it like being a database programmer?

What encoder works with Flash 6?
By: cahanckgraphics | 17-10-2008
What encoder works with Flash 6?

What is the keygen of Nero-6.6.0.8a?
By: akshar | 17-10-2008
what is the keygen of Nero-6.6.0.8a?

Please Suggest Some?
By: nelson | 16-10-2008
I?ve been using a test management tool for quite some time but it has not lived up to the expectations. So I?m planning to invest in a good one. Please suggest some?

Why doesn't the software for the Belkin 2x1 usb ...
By: Glenda | 16-10-2008
why doesn't the software for the Belkin 2x1 usb peripheral switch work.  When I click on the icon on my desktop, nothing happens.  What is the problem.

Uncompressing c drive
By: cloewe413 | 14-10-2008
While under properties in My computer, the C drive was compressed.  Now the computer won't boot up.  How do I get this uncompressed so the computer will boot?

Q&A Powered by:
Powered by Yedda 

Latest Programming Articles

Cool Desktop Wallpapers
By: Danny | 18/11/2008
Cool desktop wallpaper is accomplishments arrangement that displayed in the computer operating system. The wallpapers usually be acclimated in JPEG, BMP and GIF book formats. That wallpaper can be acclimated with Microsoft Windows, Linux and Macintosh Mac OS. Each adviser can be altered requirements, alike admitting wallpaper images advised for accepted monitors can be scaled up or bottomward to the fit size. Those are accessible on the internet for free. Some categories of wallpapers are a

Tips for Buying Software Online
By: Daniel Jowssey | 17/11/2008
Buying software online not only helps save the planet, it also has other benefits, including: * Ease and Simplicity. You can purchase software in your underwear at 4am if you really want to. Shopping online doesn’t have to be done within regular business hours, nor do you need to look your best to do it. It’s also easy to shop around for the best prices and takes less time than driving to the shops.

Mvc Design Pattern
By: TuVinhSoft .,JSC | 14/11/2008
Model-view-controller (MVC) is an architectural pattern used in software engineering. In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (View) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface.

Advantages of Low Cost Contract Programmers in Freelance Programming
By: Joanna Gadel | 12/11/2008
It observed that web industry is getting tougher thus the necessity of freelance contract programmer is required for developing more effective website with flexible features. This article states the fruitful advantages of freelance programmers in contract programming.

A Guide to Cnc Kits
By: Martin Applebaum | 09/11/2008
CNC kits are a way in which to construct your CNC machine. This article will provide some information on these machines.

A Guide to Cnc Tube Bending Machines
By: Martin Applebaum | 08/11/2008
Are you familiar with a CNC tube bending machine? This article will shed some light on the main function and components of this machine.

Ways to Hire Dedicated Php Programmers
By: Jucick | 08/11/2008
It’s not at all easy to hire dedicated PHP programmers unless you know where and how to find them. Whether you need to fix, update or enhance your website you naturally want the job done quick and right.

Top 4 Reasons Why Addressing Web Accessibility is Important
By: Matt Cave | 05/11/2008
There are very high chances that web accessibility is more important to the performance of your web site than you realize. Article takes a look at the top 4 reasons why it would be important to address the issue of web accessibility.

Article Categories





Give Feedback

Sign up for our email newsletter

Receive updates, enter your email below