Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating a Tic Tac Game in Visual Basic.Net

Author: DevASP
Download Source Code : 561_TicTac.zip

This article is about how you can create a Tic Tac game in vb.net.

 Steps you will do.

 Start visual studio and create a new window application.

 Set the following properties of the form.

 

Name = “Form1”

FormBorderStyle = “FixedSingle”

MaximizeBox = “False”

Menu = “MainMenu1”

Text = “TicTacToe”

 

Drop a timer control on the form and set its following properties.

 

Name = “Timer1”

Interval = “1000”

Modifiers = “Friend”

 

Drop a menu control on the form and create the menu as below.

 

File

New Game

Exit

 

 

Options

Player1

            Computer

            Human

Player1

Computer

            Human

 

Drop a status bar on the form and set its following properties.

 

Name = “StatusBar1”

Modifiers = “Friend”

 

 

Now add a new class in your project with the name “Opponent.vb ” and add the following code in it.

 

Public Class Opponent

       

Dim Board(9) As Integer

Dim PlayerNum As Integer

Dim Best As Integer

 

Public WriteOnly Property SetBoard(ByVal i) As Integer

 

Set(ByVal Value As Integer)

       Board(i) = Value

End Set

 

End Property

 

Public Property Player() As Integer

  Get

     Player = PlayerNum

  End Get

 

 Set(ByVal Value As Integer)

    PlayerNum = Value

 End Set

 

End Property

 

Public Sub ClearBoard()

 

Dim i As Integer

 

For i = 1 To 9

   Board(i) = 0

Next i

 

End Sub

 

Public Sub New()

 

  Player = 1

  ClearBoard()

 

End Sub

 

Public Function GetMove() As Integer

 

   algo()

   GetMove = Best

 

End Function

 

Private Function Check(ByVal b1 As Integer, ByVal b2 As Integer, ByVal b3 As Integer) As Integer

 

   Check = 0

If (Board(b1) = Board(b2)) And (Board(b1) = Board(b3)) And (Board(b1) <> 0) Then

 

    Check = Board(b1)

 

End If

 

End Function

 

Private Function CheckWin() As Integer

 

   Dim temp As Integer

   temp = Check(1, 2, 3)

   If temp <> 0 Then GoTo last

   

       temp = Check(4, 5, 6)

   

    If temp <> 0 Then GoTo last

    

        temp = Check(7, 8, 9)

    

    If temp <> 0 Then GoTo last

    

        temp = Check(1, 4, 7)

 

    If temp <> 0 Then GoTo last

    

        temp = Check(2, 5, 8)

 

     If temp <> 0 Then GoTo last

     

       temp = Check(3, 6, 9)

      

     If temp <> 0 Then GoTo last

      

       temp = Check(1, 5, 9)

 

    If temp <> 0 Then GoTo last

 

       temp = Check(3, 5, 7)

 

    If temp <> 0 Then GoTo last

 

   last:

       CheckWin = temp

    

End Function

 

Private Function CPlayer(ByVal p As Integer) As Integer

 

If p = 1 Then

  CPlayer = 2

Else

  CPlayer = 1

  

End Function

 

Private Function go(ByVal p As Integer) As Integer

 

   Dim i As Integer

   Dim k As Integer

   Dim best As Integer

   i = CheckWin()

   If i <> 0 Then

      go = i

       Exit Function

   End If

    

    best = -1

    For i = 1 To 9

    

 If Board(i) = 0 Then

    Board(i) = p

    k = go(CPlayer(p))

    If k = p Then best = p

 If (k = 0) And (best <> p) Then best = 0

 

If (k = CPlayer(p)) And (best <> p) And (best <> 0) Then best = CPlayer(p)

 

     Board(i) = 0

     

End If

 

Next

 

If best = -1 Then

   best = 0

End If

  go = best

 

End Function

 

Private Sub algo()

   Dim i As Integer

   Dim k As Integer

   Dim temp As Integer

   temp = -1

   k = 1

   For i = 1 To 9

   If Board(i) <> 0 Then

   k = 0

   Exit For

 End If

Next

 

If k = 1 Then

  Best = 5

  Exit Sub

End If

  

For i = 1 To 9

   If Board(i) = 0 Then

   Board(i) = PlayerNum

   k = go(CPlayer(PlayerNum))

   If (k = PlayerNum) Then

   Best = i

   temp = PlayerNum

   End If

  If (k = 0) And (temp <> PlayerNum) Then

       Best = i

       temp = 0

  End If

  

If (k = CPlayer(PlayerNum)) And (temp <> PlayerNum) And (temp <> 0) Then

 

        Best = i

        temp = CPlayer(PlayerNum)

     End If

        Board(i) = 0

 End If

 Next

 

End Sub

 

End Class

 

 

Open the code window of form and declare the following global variables.

 

    Dim Board(9) As PictureBox

    Dim Player As Integer

    Dim BValue(9) As Integer

    Dim WonB As Boolean

    Dim Computer As New Opponent()

    Dim Opponent(2) As Integer

 

Create the following functions as given below.

 

Private Sub Set_Board()

   Dim i As Integer

   For i = 1 To 9

   Board(i) = New PictureBox()

   Board(i).BackColor = Color.White

   Board(i).Size = New Size(50, 57)

   Board(i).Name = i

   Next i

 

   Board(1).Location = New Point(8, 8)

   Board(2).Location = New Point(64, 8)

   Board(3).Location = New Point(120, 8)

   Board(4).Location = New Point(8, 72)

   Board(5).Location = New Point(64, 72)

   Board(6).Location = New Point(120, 72)

   Board(7).Location = New Point(8, 136)

   Board(8).Location = New Point(64, 136)

  Board(9).Location = New Point(120, 136)

 

  For i = 1 To 9

     Controls.Add(Board(i))

  Next i

 

AddHandler Board(1).Click, AddressOf Clicked

    

AddHandler Board(2).Click, AddressOf Clicked

 

AddHandler Board(3).Click, AddressOf Clicked

 

AddHandler Board(4).Click, AddressOf Clicked

 

AddHandler Board(5).Click, AddressOf Clicked

 

 

AddHandler Board(6).Click, AddressOf Clicked

 

 

AddHandler Board(7).Click, AddressOf Clicked

 

 

AddHandler Board(8).Click, AddressOf Clicked

 

 

AddHandler Board(9).Click, AddressOf Clicked

 

 

 

End Sub

 

 

Private Sub Change_Player(ByVal PNumber As Integer)

       

If PNumber = 0 Then

  If Player = 1 Then

    Player = 2

   Else

    Player = 1

   End If

  Else

  Player = PNumber

End If

 

If Player = 1 Then

  StatusBar1.Text = "Player : X"

 Else

   StatusBar1.Text = "Player : O"

End If

 

If (Computer.Player = Player) And (Opponent(Player) = 1) Then

 

  Algo(Computer.GetMove)

End If

 

End Sub

 

Private Sub Set_Picture(ByVal Box As Integer, ByVal player As Integer)

 

If player = 1 Then

  Dim pic As New Bitmap("X.bmp")

  Board(Box).Image = pic

Else

  Dim pic As New Bitmap("O.bmp")

  Board(Box).Image = pic

End If

 

 

End Sub

 

 

Private Sub Won(ByVal Box1 As Integer, ByVal Box2 As Integer, ByVal Box3 As Integer)

 

Board(Box1).BorderStyle = BorderStyle.Fixed3D

 

Board(Box2).BorderStyle = BorderStyle.Fixed3D

 

Board(Box3).BorderStyle = BorderStyle.Fixed3D

 

If BValue(Box1) = 1 Then

  MsgBox("Player X has won the game!")

  Else

  MsgBox("Player O has won the game!")

End If

       

     StatusBar1.Text = "Press F2 to start New Game"

     

    WonB = True

 

End Sub

 

Private Sub Check(ByVal Box1 As Integer, ByVal Box2 As Integer, ByVal box3 As Integer)

 

    If (BValue(Box1) = BValue(Box2)) And (BValue(Box1) = BValue(box3)) And (BValue(Box1) <> 0) Then

    

        Won(Box1, Box2, box3)

    End If

   

End Sub

 

Private Sub Check_Win()

        Check(1, 2, 3)

        Check(4, 5, 6)

        Check(7, 8, 9)

        Check(1, 4, 7)

        Check(2, 5, 8)

        Check(3, 6, 9)

        Check(1, 5, 9)

        Check(3, 5, 7)

End Sub

 

Private Sub Algo(ByVal Box As Integer)

   If BValue(Box) <> 0 Then

     StatusBar1.Text = "Illegal Move"

     Beep()

     Timer1.Enabled = True

     Exit Sub

   End If

     Computer.SetBoard(Box) = Player

     BValue(Box) = Player

     Set_Picture(Box, Player)

     Check_Win()

 

 

        Dim I As Integer

        Dim Found As Boolean

        Found = False

        For I = 1 To 9

     

If BValue(I) = 0 Then Found = True

        Next

If Not Found And Not WonB Then

 

MsgBox("Draw Game")

 

StatusBar1.Text = "Press F2 to start New Game"

 

WonB = True

 

End If

 

 

If Found And Not WonB Then

   Change_Player(0)

End If

End Sub

 

 

Private Sub Clicked(ByVal sender As Object, ByVal e As EventArgs)

 

   If Not WonB Then

       Algo(sender.name)

    End If

 

End Sub

 

 

 

Now in the load event of the form write the following code.

 

        Set_Board()

        Opponent(1) = 1

        Opponent(2) = 2

        MenuItem4.Checked = True

        MenuItem9.Checked = True

        Change_Player(1)

        WonB = False

 

 

In the paint event of the form write the following code.

 

  Dim p As New Pen(Color.Black, 3)

       

e.Graphics.DrawLine(p, 61, 8, 61, 193)

 

e.Graphics.DrawLine(p, 117, 8, 117, 193)

 

e.Graphics.DrawLine(p, 8, 68, 170, 68)

 

e.Graphics.DrawLine(p, 8, 132, 170, 132)

 

e.Graphics.DrawRectangle(p, New Rectangle(6, 6, 165, 188))

 

 

 

Write the following code in the Tick event of the timer control.

 

Change_Player(Player)

Timer1.Enabled = False

 

 

Write the following code in the click event of exit menu to exit from the application.

 

Application.Exit()

 

  

Write the following code in the click event of the new menu item to start a new game.

 

Dim i As Integer

WonB = False

 

  For i = 1 To 9

    BValue(i) = 0

    Board(i).Image = New Bitmap(50, 57)

 

    Board(i).BorderStyle=  BorderStyle.None

 

   Next i

   

Computer.ClearBoard()

Change_Player(1)

 

 

Write the following code in the click event of Computer menu item which is under the player 1 menu item.

 

        Opponent(1) = 1

        Computer.Player = 1

        Call MnNew_Click(sender, e)

        MenuItem10.Enabled = False

        MenuItem4.Checked = True

        MenuItem6.Checked = False

 

Write the following code in the click event of Human menu item which is under the player 1 menu item.

 

        Opponent(1) = 2

        Call MnNew_Click(sender, e)

        MenuItem10.Enabled = True

        MenuItem6.Checked = True

        MenuItem4.Checked = False

 

  

Write the following code in the click event of Computer menu item which is under the player 2 menu item.

 

        Opponent(2) = 1

        Computer.Player = 2

        Call MnNew_Click(sender, e)

        MenuItem4.Enabled = False

        MenuItem10.Checked = True

        MenuItem9.Checked = False

 

 

Write the following code in the click event of Human menu item which is under the player 2 menu item.

 

        Opponent(2) = 2

        Call MnNew_Click(sender, e)

        MenuItem4.Enabled = True

        MenuItem9.Checked = True

        MenuItem10.Checked = False

 

 

Run you application and test it.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Check Time According To Different Time Zone Using Visual Basic.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net