Tic-Tac-Toe

Public Class frmTicTacToe 'Definte the tic-tac-toe board Dim strMatrix(2, 2) As String Dim intCounter As Integer = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load BackColor = Color.Beige End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click strMatrix(0, 2) = XorO(Button1) Button1.Enabled = False Button1.BackColor = Color.Snow

'Button1.Enabled = False If CheckForWin = True Then End End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click strMatrix(0, 1) = XorO(Button2) Button2.Enabled = False Button2.BackColor = Color.Snow 'Button2.Enabled = False If CheckForWin = True Then End End If

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click strMatrix(0, 0) = XorO(Button3) Button3.Enabled = False Button3.BackColor = Color.Snow 'Button3.Enabled = False If CheckForWin = True Then End End If

End Sub Public Function XorO(ByVal btnButton) intCounter += 1 If intCounter Mod 2 = 0 Then btnButton.Text = "X" Else btnButton.Text = "O" End If Return btnButton.Text End Function

Public Function CheckForWin As Boolean Dim strWinHorix As String = "" Dim strWinVert As String = ""

For x = 0 To 2 For y = 0 To 2 strWinHorix = strWinHorix & strMatrix(x, y) Next If strWinHorix = "XXX" Then MsgBox("Player X Wins!!") Return True End If

If strWinHorix = "OOO" Then MsgBox("Player O Wins!!") Return True End If

strWinHorix = "" Next

For x = 0 To 2 For y = 0 To 2 strWinVert = strWinVert & strMatrix(y, x) Next If strWinVert = "XXX" Then MsgBox("Player X Wins!!") Return True End If

If strWinVert = "OOO" Then MsgBox("Player O Wins!!") End If strWinVert = "" Next

End Function

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click strMatrix(2, 1) = XorO(Button8) Button8.Enabled = False Button8.BackColor = Color.Snow 'Button8.Enabled = False If CheckForWin = True Then End End If End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click strMatrix(1, 2) = XorO(Button4) Button4.Enabled = False Button4.BackColor = Color.Snow 'Button4.Enabled = False If CheckForWin = True Then End End If End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click strMatrix(1, 1) = XorO(Button5) Button5.Enabled = False Button5.BackColor = Color.Snow 'Button5.Enabled = False If CheckForWin = True Then End End If End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click strMatrix(1, 0) = XorO(Button6) Button6.Enabled = False Button6.BackColor = Color.Snow 'Button6.Enabled = False If CheckForWin = True Then End End If End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click strMatrix(2, 2) = XorO(Button7) Button7.Enabled = False Button7.BackColor = Color.Snow 'Button7.Enabled = False If CheckForWin = True Then End End If End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click strMatrix(2, 0) = XorO(Button9) Button9.Enabled = False Button9.BackColor = Color.Snow 'Button9.Enabled = False If CheckForWin = True Then End End If End Sub

End Class