Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,140 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,276 people online right now. Registration is fast and FREE... Join Now!




TicTacToe on Visual Basic 5

 
Reply to this topicStart new topic

TicTacToe on Visual Basic 5

Mo14
19 Jul, 2008 - 08:21 AM
Post #1

New D.I.C Head
*

Joined: 18 Jul, 2008
Posts: 7


My Contributions
Ok this is my code for my TicTacToe game on VB the game runs but when I add code CheckForAWinner() code and I click on any one button Message Box comes up saying Game Over, Player X Win I restart the game same thing click on any button Message Box comes up
would like some help with that? Thank You

CODE
Public Class Form1

    'Environment Variables
    Dim Counter As Integer = 0      'Counter variable for timer
    Dim gameBoard(16) As String     'Declare a 16 element array
    Dim isWinner As Boolean         'Keep track of whether there is a winner


    'This is done avoid confusion by keeping the array element  
    Dim player As String = "X"      'Declare player variable and initialize to "X"


    'Menu Object New Game
    Private Sub NewGameToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
        Call ClearBoard()
    End Sub

    'Start Game Button on the Board
    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartGame.Click
        Call ClearBoard()
    End Sub

    'Menu Object Exit
    Private Sub ExitToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
        Timer1.Enabled = False
    End Sub

    'Menu Object About
    Private Sub AboutToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        MessageBox.Show("Tic-Tac-Toe v1.0 for CS123")
    End Sub

    'Game Timer Object
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = True
        Counter = Counter + 1
        Timer.Text = Counter
    End Sub

    Private Sub ClearBoard()
        'Deletes the contents of the labels, and then enable them
        'Also enables the game board, clears the gameBoard array, and resets the isWinner variable

        Button1.Text = String.Empty     'Removes the X from the gameboard display
        Button2.Text = String.Empty
        Button3.Text = String.Empty
        Button4.Text = String.Empty
        Button5.Text = String.Empty
        Button6.Text = String.Empty
        Button7.Text = String.Empty
        Button8.Text = String.Empty
        Button9.Text = String.Empty
        Button10.Text = String.Empty
        Button11.Text = String.Empty
        Button12.Text = String.Empty
        Button13.Text = String.Empty
        Button14.Text = String.Empty
        Button15.Text = String.Empty
        Button16.Text = String.Empty


        Button1.Enabled = True      'Enables each button of the game board for play
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        Button6.Enabled = True
        Button7.Enabled = True
        Button8.Enabled = True
        Button9.Enabled = True
        Button10.Enabled = True
        Button11.Enabled = True
        Button12.Enabled = True
        Button13.Enabled = True
        Button14.Enabled = True
        Button15.Enabled = True
        Button16.Enabled = True

        Button1.BackColor = Color.Navy         'Button back color when game starts
        Button2.BackColor = Color.Navy
        Button3.BackColor = Color.Navy
        Button4.BackColor = Color.Navy
        Button5.BackColor = Color.Navy
        Button6.BackColor = Color.Navy
        Button7.BackColor = Color.Navy
        Button8.BackColor = Color.Navy
        Button9.BackColor = Color.Navy
        Button10.BackColor = Color.Navy
        Button11.BackColor = Color.Navy
        Button12.BackColor = Color.Navy
        Button13.BackColor = Color.Navy
        Button14.BackColor = Color.Navy
        Button15.BackColor = Color.Navy
        Button16.BackColor = Color.Navy

        'Set each button in the grid to Nothing
        For subscript As Integer = 0 To 15
            gameBoard(subscript) = Nothing
        Next subscript

        'Sets the winner value to false (no winner)
        isWinner = False

        Timer1.Enabled = True       'Start the new game timer
        Counter = 0                 'Set the counter value to 0
    End Sub

    'Subroutine to enable the click functionality for each button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Text = player       'Set the game board display to the current player value
        Button1.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(1) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button2.Text = player       'Set the game board display to the current player value
        Button2.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(2) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button3.Text = player       'Set the game board display to the current player value
        Button3.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(3) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Button4.Text = player       'Set the game board display to the current player value
        Button4.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(4) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Button5.Text = player       'Set the game board display to the current player value
        Button5.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(5) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Button6.Text = player       'Set the game board display to the current player value
        Button6.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(6) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Button7.Text = player       'Set the game board display to the current player value
        Button7.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(7) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Button8.Text = player       'Set the game board display to the current player value
        Button8.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(8) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Button9.Text = player       'Set the game board display to the current player value
        Button9.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(9) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Button10.Text = player       'Set the game board display to the current player value
        Button10.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(10) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Button11.Text = player       'Set the game board display to the current player value
        Button11.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(11) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Button12.Text = player       'Set the game board display to the current player value
        Button12.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(12) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Button13.Text = player       'Set the game board display to the current player value
        Button13.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(13) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        Button14.Text = player       'Set the game board display to the current player value
        Button14.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(14) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        Button15.Text = player       'Set the game board display to the current player value
        Button15.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(15) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub

    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        Button16.Text = player       'Set the game board display to the current player value
        Button16.Enabled = False     'Disables the square so it can't be selected / deselected
        gameBoard(16) = player       'Set the game board display to the current player value

        'Call CheckForAWin()
        Call CheckForAWinner()

        Call ComputerMove()
    End Sub


    'Routine to make move for computer (player "O")
    Private Sub ComputerMove()
        player = "O"                 'Make sure the player variable is set to "O"

        'Find and select the first unused square
        If Button1.Enabled = True Then
            Button1.Text = player
            Button1.Enabled = False
            gameBoard(1) = player
        ElseIf Button2.Enabled = True Then
            Button2.Text = player
            Button2.Enabled = False
            gameBoard(2) = player
        ElseIf Button3.Enabled = True Then
            Button3.Text = player
            Button3.Enabled = False
            gameBoard(3) = player
        ElseIf Button4.Enabled = True Then
            Button4.Text = player
            Button4.Enabled = False
            gameBoard(4) = player
        ElseIf Button5.Enabled = True Then
            Button5.Text = player
            Button5.Enabled = False
            gameBoard(5) = player
        ElseIf Button6.Enabled = True Then
            Button6.Text = player
            Button6.Enabled = False
            gameBoard(6) = player
        ElseIf Button7.Enabled = True Then
            Button7.Text = player
            Button7.Enabled = False
            gameBoard(7) = player
        ElseIf Button8.Enabled = True Then
            Button8.Text = player
            Button8.Enabled = False
            gameBoard(8) = player
        ElseIf Button9.Enabled = True Then
            Button9.Text = player
            Button9.Enabled = False
            gameBoard(9) = player
        ElseIf Button10.Enabled = True Then
            Button10.Text = player
            Button10.Enabled = False
            gameBoard(10) = player
        ElseIf Button11.Enabled = True Then
            Button11.Text = player
            Button11.Enabled = False
            gameBoard(11) = player
        ElseIf Button12.Enabled = True Then
            Button12.Text = player
            Button12.Enabled = False
            gameBoard(12) = player
        ElseIf Button13.Enabled = True Then
            Button13.Text = player
            Button13.Enabled = False
            gameBoard(13) = player
        ElseIf Button14.Enabled = True Then
            Button14.Text = player
            Button14.Enabled = False
            gameBoard(14) = player
        ElseIf Button15.Enabled = True Then
            Button15.Text = player
            Button15.Enabled = False
            gameBoard(15) = player
        ElseIf Button16.Enabled = True Then
            Button16.Text = player
            Button16.Enabled = False
            gameBoard(16) = player
        End If

    End Sub

    'Check each (horizontal) row for a win
    Private Sub CheckForAWinner()

        If gameBoard(1) = player And gameBoard(2) = player And gameBoard(3) = player And gameBoard(4) = player Then
            isWinner = True
        End If

        If gameBoard(5) = player And gameBoard(6) = player And gameBoard(7) = player And gameBoard(8) = player Then
            isWinner = True
        End If

        If gameBoard(9) = player And gameBoard(10) = player And gameBoard(11) = player And gameBoard(12) = player Then
            isWinner = True
        End If

        If gameBoard(13) = player And gameBoard(14) = player And gameBoard(15) = player And gameBoard(16) = player Then
            isWinner = True
        End If


        'Check each (vertical) column for a win
        If gameBoard(1) = player And gameBoard(5) = player And gameBoard(9) = player And gameBoard(13) = player Then
            isWinner = True
        End If

        If gameBoard(2) = player And gameBoard(6) = player And gameBoard(10) = player And gameBoard(14) = player Then
            isWinner = True
        End If

        If gameBoard(3) = player And gameBoard(7) = player And gameBoard(11) = player And gameBoard(15) = player Then
            isWinner = True
        End If

        If gameBoard(4) = player And gameBoard(8) = player And gameBoard(12) = player And gameBoard(16) = player Then
            isWinner = True
        End If


        'Check each diagonal for a win
        If gameBoard(1) = player And gameBoard(6) = player And gameBoard(11) = player And gameBoard(16) = player Then
            isWinner = True
        End If

        If gameBoard(13) = player And gameBoard(10) = player And gameBoard(7) = player And gameBoard(4) = player Then
            isWinner = True
        End If


        'If a Winner is found
        Timer1.Enabled = False      'Stop the Timer


        'Display winner message
        MessageBox.Show("Game over! Player " & player & " wins.", "TicTacToe", MessageBoxButtons.OK, MessageBoxIcon.Information)


        'Dialbe all Buttons when winner is found
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = False
        Button7.Enabled = False
        Button8.Enabled = False
        Button9.Enabled = False
        Button10.Enabled = False
        Button11.Enabled = False
        Button12.Enabled = False
        Button13.Enabled = False
        Button14.Enabled = False
        Button15.Enabled = False
        Button16.Enabled = False


        'Set focus to New Game button
        StartGame.Focus()

        If isWinner = False Then
        ElseIf Button1.Enabled = False And Button2.Enabled = False And Button3.Enabled = False And Button4.Enabled = False And Button5.Enabled = False And Button6.Enabled = False And Button7.Enabled = False And Button8.Enabled = False And Button9.Enabled = False And Button10.Enabled = False And Button11.Enabled = False And Button12.Enabled = False And Button13.Enabled = False And Button14.Enabled = False And Button15.Enabled = False And Button16.Enabled = False And isWinner = False Then
            Timer1.Enabled = False              'Stop the timer
            MessageBox.Show("Tie Game, No Winner")

            StartGame.Focus()                   'Set focus to new game button

        Else                                    'No winner or tie, so switch players and continue game
            If player = "X" Then
                player = "O"
                Call ComputerMove()
            Else
                player = "X"
            End If
        End If


    End Sub


End Class

User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: TicTacToe On Visual Basic 5
19 Jul, 2008 - 10:03 AM
Post #2

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2250
My Contributions
Look at CheckForAWinner() and so what when isWinner is True
Is there a missing if statement?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: TicTacToe On Visual Basic 5
19 Jul, 2008 - 10:07 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET smile.gif
User is offlineProfile CardPM
+Quote Post

Mo14
RE: TicTacToe On Visual Basic 5
19 Jul, 2008 - 12:08 PM
Post #4

New D.I.C Head
*

Joined: 18 Jul, 2008
Posts: 7


My Contributions
QUOTE(AdamSpeight2008 @ 19 Jul, 2008 - 01:03 PM) *

Look at CheckForAWinner() and so what when isWinner is True
Is there a missing if statement?



I am not sure if there are any IF statements missing. I keep going through it again and again, this is where I need help, to see if there is anything missing!!!!
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: TicTacToe On Visual Basic 5
19 Jul, 2008 - 12:14 PM
Post #5

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2250
My Contributions
QUOTE(Mo14 @ 19 Jul, 2008 - 09:08 PM) *

QUOTE(AdamSpeight2008 @ 19 Jul, 2008 - 01:03 PM) *

Look at CheckForAWinner() and so what when isWinner is True
Is there a missing if statement?



I am not sure if there are any IF statements missing. I keep going through it again and again, this is where I need help, to see if there is anything missing!!!!


There's a comment in in the CheckForAWinner
'If a Winner is found

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:12AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month