Results 1 to 6 of 6

Thread: vb.net | tic tac toe | win sub

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    vb.net | tic tac toe | win sub

    I think I'm having some problems with my private sub win()
    This is my code broken down:
    Declaring Variables
    Code:
    Public Class Form2
        'Sets the score, initially set at 0
        'For both x and 0
        Dim x As Integer = 0
        Dim o As Integer = 0

    My win sub
    Code:
        Private Sub win()
            'Possible wins
            'horizontal 123 465 789
            'vertical 147 258 369
            'Diagonal 159 357
            If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" Or Button4.Text = "X" And Button6.Text = "X" And Button5.Text = "X" Or Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" Then
                MsgBox("X Wins!")
                x = x + 1
                Label1.Text = x
                Button11.PerformClick()
            End If
            If Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" Or Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" Or Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" Then
                MsgBox("X Wins!")
                x = x + 1
                Label1.Text = x
            End If
            If Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" Or Button3.Text = "X" And Button5.Text = "X" And Button7.Text = "X" Then
                MsgBox("X Wins!")
                x = x + 1
                Label1.Text = x
                Button11.PerformClick()
            End If
    
            If Button1.Text = "0" And Button2.Text = "0" And Button3.Text = "0" Or Button4.Text = "0" And Button6.Text = "0" And Button5.Text = "0" Or Button7.Text = "0" And Button8.Text = "0" And Button9.Text = "0" Then
                MsgBox("0 Wins!")
                o = o + 1
                Label1.Text = o
                Button11.PerformClick()
            End If
            If Button1.Text = "0" And Button4.Text = "0" And Button7.Text = "0" Or Button2.Text = "0" And Button5.Text = "0" And Button8.Text = "0" Or Button3.Text = "0" And Button6.Text = "0" And Button9.Text = "0" Then
                MsgBox("0 Wins!")
                o = o + 1
                Label1.Text = o
                Button11.PerformClick()
            End If
            If Button1.Text = "0" And Button5.Text = "0" And Button9.Text = "0" Or Button3.Text = "0" And Button5.Text = "0" And Button7.Text = "0" Then
                MsgBox("0 Wins!")
                o = o + 1
                Label1.Text = o
                Button11.PerformClick()
            End If
    
        End Sub
    Button for x's and o's I have 9 of them
    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            'Sets the button's text to x or 0
            If Label6.Text = "0" Then
                Button2.Text = "X"
                Label6.Text = "X"
            Else : Label6.Text = "X"
                Button2.Text = "0"
                Label6.Text = "0"
            End If
            Button2.Enabled = False
            Call win()
        End Sub
    I have a seperate label that starts as .text = 0 so that x will start any time a button is clicked it changes the .text to = x and Vis-Versa.

    x and 0's score
    Code:
        Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click
            'Shows the score for x
            Label4.Text = x
        End Sub
    
        Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click
            'Shows the score for 0
            Label5.Text = o
        End Sub
    Score Reset
    Code:
        Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
            'Resets the score back to 0
            x = 0
            o = 0
            'Resets current game so people can't cheat
            Button11.PerformClick()
        End Sub
    Game Reset
    Code:
        Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
            'Resets the game w/o reseting the score
            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
            Button2.Text = ""
            Button3.Text = ""
            Button4.Text = ""
            Button5.Text = ""
            Button6.Text = ""
            Button7.Text = ""
            Button8.Text = ""
            Button9.Text = ""
            Button10.Text = ""
        End Sub
    I think its my win sub. But when the game is over, it doesn't change my score, msgbox doesn't come up and the game isn't reset. What up with that?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: vb.net | tic tac toe | win sub

    The problem is that you are treating the string as a numerical value. In the
    parts where it says Label4.Text = x and Label5.Text = o, put in this

    Code:
    Label4.Text = Convert.ToString(x)
    Label5.Text = Convert.ToString(o)
    Also I recreated the tic tac toe game yet mine works great:

    Code:
    Option Explicit On
    Option Strict On
    
    Public Class frmMain
    
        Dim Flag As Boolean = False
        Dim X_Score As Integer = 0
        Dim O_Score As Integer = 0
    
        Private Function Check_For_Win() As Boolean
    
            If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" Then
                X_Score += 1
                MessageBox.Show("X Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "X" Then
                X_Score += 1
                MessageBox.Show("X Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" Then
                X_Score += 1
                MessageBox.Show("X Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" Then
                X_Score += 1
                MessageBox.Show("X Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" Then
                X_Score += 1
                MessageBox.Show("X Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" Then
                X_Score += 1
                MessageBox.Show("X Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" Then
                X_Score += 1
                MessageBox.Show("X Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button3.Text = "X" And Button5.Text = "X" And Button7.Text = "X" Then
                X_Score += 1
                MessageBox.Show("X Wins.", "Important Message")
                Check_For_Win = True
                '----------------------------------------------------------------------------
            ElseIf Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "O" Then
                O_Score += 1
                MessageBox.Show("O Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "O" Then
                O_Score += 1
                MessageBox.Show("O Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "O" Then
                O_Score += 1
                MessageBox.Show("O Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "O" Then
                O_Score += 1
                MessageBox.Show("O Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "O" Then
                O_Score += 1
                MessageBox.Show("O Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "O" Then
                O_Score += 1
                MessageBox.Show("O Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "O" Then
                O_Score += 1
                MessageBox.Show("O Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button3.Text = "O" And Button5.Text = "O" And Button7.Text = "O" Then
                O_Score += 1
                MessageBox.Show("O Wins.", "Important Message")
                Check_For_Win = True
            ElseIf Button1.Text <> "" And Button2.Text <> "" And Button3.Text <> "" And _
                   Button4.Text <> "" And Button5.Text <> "" And Button6.Text <> "" And _
                   Button7.Text <> "" And Button8.Text <> "" And Button9.Text <> "" Then
                MessageBox.Show("Cats Game.", "Important Message")
                Button10.PerformClick()
                Check_For_Win = False
            End If
    
            If Check_For_Win = True Then Button10.PerformClick()
    
            Label3.Text = Convert.ToString(X_Score)
            Label4.Text = Convert.ToString(O_Score)
        End Function
    
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Button1.Text = "" Then
                If Flag = False Then
                    Button1.Text = "X"
                    Flag = True
                Else
                    Button1.Text = "O"
                    Flag = False
                End If
                Check_For_Win()
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If Button2.Text = "" Then
                If Flag = False Then
                    Button2.Text = "X"
                    Flag = True
                Else
                    Button2.Text = "O"
                    Flag = False
                End If
                Check_For_Win()
            End If
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            If Button3.Text = "" Then
                If Flag = False Then
                    Button3.Text = "X"
                    Flag = True
                Else
                    Button3.Text = "O"
                    Flag = False
                End If
                Check_For_Win()
            End If
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            If Button4.Text = "" Then
                If Flag = False Then
                    Button4.Text = "X"
                    Flag = True
                Else
                    Button4.Text = "O"
                    Flag = False
                End If
                Check_For_Win()
            End If
        End Sub
    
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            If Button5.Text = "" Then
                If Flag = False Then
                    Button5.Text = "X"
                    Flag = True
                Else
                    Button5.Text = "O"
                    Flag = False
                End If
                Check_For_Win()
            End If
        End Sub
    
        Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
            If Button6.Text = "" Then
                If Flag = False Then
                    Button6.Text = "X"
                    Flag = True
                Else
                    Button6.Text = "O"
                    Flag = False
                End If
                Check_For_Win()
            End If
        End Sub
    
        Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            If Button7.Text = "" Then
                If Flag = False Then
                    Button7.Text = "X"
                    Flag = True
                Else
                    Button7.Text = "O"
                    Flag = False
                End If
                Check_For_Win()
            End If
        End Sub
    
        Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
            If Button8.Text = "" Then
                If Flag = False Then
                    Button8.Text = "X"
                    Flag = True
                Else
                    Button8.Text = "O"
                    Flag = False
                End If
                Check_For_Win()
            End If
        End Sub
    
        Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
            If Button9.Text = "" Then
                If Flag = False Then
                    Button9.Text = "X"
                    Flag = True
                Else
                    Button9.Text = "O"
                    Flag = False
                End If
                Check_For_Win()
            End If
        End Sub
    
        Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
            Button1.Text = ""
            Button2.Text = ""
            Button3.Text = ""
            Button4.Text = ""
            Button5.Text = ""
            Button6.Text = ""
            Button7.Text = ""
            Button8.Text = ""
            Button9.Text = ""
        End Sub
    End Class

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: vb.net | tic tac toe | win sub

    Dang, I knew it was something stupid like that. I also notice you have option explicit and strict on. I didn't do that either.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    New Member
    Join Date
    May 2012
    Posts
    4

    Re: vb.net | tic tac toe | win sub

    I am getting an error with my sub win where the last column and the last row don't register a win. All the other combinations work. If X wins the last(right) column, it will not count as a win, but if O then clicks on that board it registers as an X win. I have no clue what to do. Any suggestions?

    I attached the code and a marked screenshot for reference.
    Attached Images Attached Images  
    Attached Files Attached Files

  5. #5
    New Member
    Join Date
    May 2012
    Posts
    4

    Re: vb.net | tic tac toe | win sub

    A correction to my last post - the last two rows and the last two columns do not work. It only registers a win when someone clicks in the first column or first row, after a win has occurred.

  6. #6

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: vb.net | tic tac toe | win sub

    Wow dude, grave digging much? Post a thread with your problem on a new thread.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width