Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button Friend WithEvents Button5 As System.Windows.Forms.Button Friend WithEvents Button6 As System.Windows.Forms.Button Friend WithEvents Button7 As System.Windows.Forms.Button Friend WithEvents Button8 As System.Windows.Forms.Button Friend WithEvents Button9 As System.Windows.Forms.Button Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button Me.Button5 = New System.Windows.Forms.Button Me.Button6 = New System.Windows.Forms.Button Me.Button7 = New System.Windows.Forms.Button Me.Button8 = New System.Windows.Forms.Button Me.Button9 = New System.Windows.Forms.Button Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(8, 8) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(64, 64) Me.Button1.TabIndex = 0 Me.Button1.Tag = "0,0" ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(96, 8) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(64, 64) Me.Button2.TabIndex = 1 Me.Button2.Tag = "0,1" ' 'Button3 ' Me.Button3.Location = New System.Drawing.Point(184, 8) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(64, 64) Me.Button3.TabIndex = 2 Me.Button3.Tag = "0,2" ' 'Button4 ' Me.Button4.Location = New System.Drawing.Point(184, 88) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(64, 64) Me.Button4.TabIndex = 5 Me.Button4.Tag = "1,2" ' 'Button5 ' Me.Button5.Location = New System.Drawing.Point(96, 88) Me.Button5.Name = "Button5" Me.Button5.Size = New System.Drawing.Size(64, 64) Me.Button5.TabIndex = 4 Me.Button5.Tag = "1,1" ' 'Button6 ' Me.Button6.Location = New System.Drawing.Point(8, 88) Me.Button6.Name = "Button6" Me.Button6.Size = New System.Drawing.Size(64, 64) Me.Button6.TabIndex = 3 Me.Button6.Tag = "1,0" ' 'Button7 ' Me.Button7.Location = New System.Drawing.Point(184, 168) Me.Button7.Name = "Button7" Me.Button7.Size = New System.Drawing.Size(64, 64) Me.Button7.TabIndex = 8 Me.Button7.Tag = "2,2" ' 'Button8 ' Me.Button8.Location = New System.Drawing.Point(96, 168) Me.Button8.Name = "Button8" Me.Button8.Size = New System.Drawing.Size(64, 64) Me.Button8.TabIndex = 7 Me.Button8.Tag = "2,1" ' 'Button9 ' Me.Button9.Location = New System.Drawing.Point(8, 168) Me.Button9.Name = "Button9" Me.Button9.Size = New System.Drawing.Size(64, 64) Me.Button9.TabIndex = 6 Me.Button9.Tag = "2,0" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(264, 246) Me.Controls.Add(Me.Button7) Me.Controls.Add(Me.Button8) Me.Controls.Add(Me.Button9) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.Button5) Me.Controls.Add(Me.Button6) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Tic Tac Toe" Me.ResumeLayout(False) End Sub #End Region Private Sub btnMoveMade_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click, _ Button2.Click, Button3.Click, Button4.Click, _ Button5.Click, Button6.Click, Button7.Click, _ Button8.Click, Button9.Click Dim btnSquareClicked As Button = sender Static chrTTT(2, 2) As Char Static chrPlayer As Char = "X" If btnSquareClicked.Text <> Nothing Then MessageBox.Show("Invalid!") Else btnSquareClicked.Text = chrPlayer Dim XOShow = btnSquareClicked.Text Dim strIndex As String strIndex = btnSquareClicked.Tag Dim intX As Integer = Val(strIndex.Chars(0)) Dim intY As Integer = Val(strIndex.Chars(2)) Call StoreMove(intX, intY, chrPlayer, chrTTT) If IsWinner(chrTTT) Then MessageBox.Show("Game over!") Else If chrPlayer = "X" Then chrPlayer = "O" Else chrPlayer = "X" End If End If End If End Sub Sub StoreMove(ByVal intX As Integer, ByVal intY As Integer, _ ByVal chrPlayer As Char, ByRef chrTTT(,) As Char) chrTTT(intX, intY) = chrPlayer End Sub Function IsWinner(ByRef chrTTT(,) As Char) As Boolean Dim intRow As Integer For intRow = 0 To 2 If chrTTT(intRow, 0) = chrTTT(intRow, 1) And _ chrTTT(intRow, 1) = chrTTT(intRow, 2) And _ (chrTTT(intRow, 0) = "X" Or chrTTT(intRow, 0) = "O") Then Return True ' You win! End If Next intRow Dim intCol As Integer For intCol = 0 To 2 If chrTTT(0, intCol) = chrTTT(1, intCol) And _ chrTTT(1, intCol) = chrTTT(2, intCol) And _ (chrTTT(0, intCol) = "X" Or chrTTT(0, intCol) = "O") Then Return True ' Winner End If Next intCol If chrTTT(0, 0) = chrTTT(1, 1) And chrTTT(1, 1) = chrTTT(2, 2) _ And (chrTTT(0, 0) = "X" Or chrTTT(0, 0) = "O") Then Return True ' Winner again! End If If chrTTT(0, 2) = chrTTT(1, 1) And chrTTT(1, 1) = chrTTT(2, 0) _ And (chrTTT(0, 2) = "X" Or chrTTT(0, 2) = "O") Then Return True 'win End If Dim blnMovesLeft As Boolean = False For intRow = 0 To 2 For intCol = 0 To 2 If chrTTT(intRow, intCol) = Nothing Then blnMovesLeft = True End If Next intCol Next intRow If Not blnMovesLeft Then Return True 'Squares filled! End If Return False ' Game goes on; no winner yet! End Function End Class