' Mackie Drew
' January 19, 2008
' Tic-Tac-Toe
Public Class GameWindow
' Declare Varibales
Dim Player As Boolean = True ' X is true, O is false
Dim PositionsFilled As Int16 = 0 'Counts for Tie
Dim Grid(0 To 2, 0 To 2) As Integer ' This Multidimensional array is used to conclude victory and to produce potential AI
Friend Sub WinCheck()
' Checks for a win Verticallt and Horizontally
For i = 0 To 2 Step 1
If Grid(0, i) + Grid(1, i) + Grid(2, i) = 30 Then
MsgBox("O wins!")
Me.Close()
ElseIf Grid(0, i) + Grid(1, i) + Grid(2, i) = 3 Then
MsgBox("X wins!")
Me.Close()
End If
If Grid(i, 0) + Grid(i, 1) + Grid(i, 2) = 30 Then
MsgBox("O wins!")
Me.Close()
ElseIf Grid(i, 0) + Grid(i, 1) + Grid(i, 2) = 3 Then
MsgBox("X wins!")
Me.Close()
End If
Next i
' Checks for a win Diagonally
If Grid(0, 2) + Grid(1, 1) + Grid(2, 0) = 30 Then
MsgBox("O wins!")
Me.Close()
ElseIf Grid(0, 2) + Grid(1, 1) + Grid(2, 0) = 3 Then
MsgBox("X wins!")
Me.Close()
End If
If Grid(0, 0) + Grid(1, 1) + Grid(2, 2) = 30 Then
MsgBox("O wins!")
Me.Close()
ElseIf Grid(0, 0) + Grid(1, 1) + Grid(2, 2) = 3 Then
MsgBox("X wins!")
Me.Close()
End If
' Checks For a Tie.
If PositionsFilled > 8 Then
MsgBox("Tie! :(")
Me.Close()
End If
End Sub
' This Procedure handles, turns and Visual Output
Friend Sub OnButtonClick(ByVal GridX As Integer, ByVal GridY As Integer, ByVal Name As System.Object)
' This statement makes sure the button doesn't already have an X in it.
If Name.Text = " " Then
If Player Then
Name.Text = "X" ' Visual Output
Grid(GridX, GridY) = 1 ' These values are used for collision
Player = False ' Sets turn to O
Else
Name.Text = "O" ' Visual Output
Grid(GridX, GridY) = 10 ' These values are used for collision
Player = True ' Sets turn to X
End If
PositionsFilled += 1 ' This values makes helps check for a Tie
End If
WinCheck() ' Calls for WinCheck Procedure
End Sub
' This calls the OnButtonClick Procedure for each button
Private Sub B00_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B00.Click
OnButtonClick(0, 0, B00)
End Sub
Private Sub B10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B10.Click
OnButtonClick(1, 0, B10)
End Sub
Private Sub B20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B20.Click
OnButtonClick(2, 0, B20)
End Sub
Private Sub B01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B01.Click
OnButtonClick(0, 1, B01)
End Sub
Private Sub B11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B11.Click
OnButtonClick(1, 1, B11)
End Sub
Private Sub B21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B21.Click
OnButtonClick(2, 1, B21)
End Sub
Private Sub B02_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B02.Click
OnButtonClick(0, 2, B02)
End Sub
Private Sub B12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B12.Click
OnButtonClick(1, 2, B12)
End Sub
Private Sub B22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B22.Click
OnButtonClick(2, 2, B22)
End Sub
End Class
|