Computer Science Canada

Tic-Tac-Toe

Author:  Gooie [ Sat Jan 19, 2008 5:40 pm ]
Post subject:  Tic-Tac-Toe

Hello, there. I am making the move from Turing to Visual Basic because, next year the programming course uses Visual Basic, rather than Turing. So, 2 days ago I started learning it. I absolutely LOVE IT! So much better than Turing. The sad part is, that CompSci's Visual Basic Section is less than vibrant.

I made this Tic-Tac-Toe Game in Visual Basic 2008 (Games are not as easy in Visual Basic!). It's OK... only 2 player for now. This is my 3rd day in Visual Basic so bare with me.

code:

' 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


What do you think?

Author:  michaelp [ Sat Jan 19, 2008 5:45 pm ]
Post subject:  RE:Tic-Tac-Toe

Not bad for your third day with a programming language. IMO, I think the actual playing window could be a little bigger and have a border. That would make it better for me. And make the X's and O's different colours.
Nice game. Smile

Author:  Gooie [ Sat Jan 19, 2008 6:08 pm ]
Post subject:  Re: Tic-Tac-Toe

I implemented those suggestions, I like it better now Very Happy.

Here is how it has changed.

Posted Image, might have been reduced in size. Click Image to view fullscreen.

Author:  michaelp [ Sun Jan 20, 2008 8:56 am ]
Post subject:  RE:Tic-Tac-Toe

Nice! Looks much better now. Very Happy

Author:  CodeMonkey2000 [ Mon Jan 21, 2008 9:53 pm ]
Post subject:  RE:Tic-Tac-Toe

What?!!? Turing is by far nicer to program in than VB.

Author:  Gooie [ Mon Jan 21, 2008 10:48 pm ]
Post subject:  Re: RE:Tic-Tac-Toe

CodeMonkey2000 @ January 21st, 9:53 pm wrote:
What?!!? Turing is by far nicer to program in than VB.


I suppose Turing is just not my cup of tea. I could make Tic-Tac-Toe in two hours. After studying VB for a day or so. After several weeks in Turing I failed at Tic-Tac-Toe. (I know, it's sad). I Eventually improved though. Perhaps everythign I learned in Turing helped me more than I thought in VB? who knows?


: