|
-
Oct 24th, 2011, 10:43 PM
#1
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?
-
Oct 27th, 2011, 10:43 AM
#2
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
-
Oct 27th, 2011, 12:45 PM
#3
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.
-
May 16th, 2012, 06:53 PM
#4
New Member
-
May 16th, 2012, 07:09 PM
#5
New Member
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.
-
May 17th, 2012, 12:31 AM
#6
Re: vb.net | tic tac toe | win sub
Wow dude, grave digging much? Post a thread with your problem on a new thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|