Okay, so it's almost done but then when I get 2 "o" and one "x", then the game says it wins. Any help? I'm pretty sure I wrote it correctly but it doesn't work.
download it here:
http://www.mediafire.com/download.php?mvjlwnnergm
Printable View
Okay, so it's almost done but then when I get 2 "o" and one "x", then the game says it wins. Any help? I'm pretty sure I wrote it correctly but it doesn't work.
download it here:
http://www.mediafire.com/download.php?mvjlwnnergm
this is a terrible game that is not close to finished
i can overwrite a users X with my O and vice
i do not know who's turn it is
it does not tell me which team won
the game never ends
you didn't give it a title
it doesn't detect winners correctly at all
what does the "BU" button do?!?!
SO MUCH MORE
i would help you but i cannot open the solution, can you paste the win detection code here?
and i only wrote a TicTacToe in vb6 not vb.net
sorry, but goodluck man
Philly0494
Hi,Quote:
Originally Posted by hacksign23
Here's an example how to program a Tic Tac Toe game.
http://visualbasic.about.com/od/usin.../aa093002a.htm
Hope it helps,
sparrow1
I attached the Tic-Tac-Toe game I made for my VB.Net 101 class many years ago. The AI is absolutely horrific but the basic idea is there. Perhaps it'll help?
"I'm pretty sure I wrote it correctly but it doesn't work."
I hate it when that happens. ;)
a 3 x 3 array.
in the array check for 3 of the same:
check each row
check each col
check (0,0) (1,1) (2,2)
check (0,2) (1,1) (2,0)
edit at 10:19 CDTCode:Dim xoS As String(,) = New String(2, 2) {}
'init array - start of game
For x As Integer = 0 To xoS.GetUpperBound(0) 'EDIT to correct
For y As Integer = 0 To xoS.GetUpperBound(1)
xoS(x, y) = "NONE"
Next
Next
lol to the first post. Oh, if you install it, that's the really crappy one(stage 2/10) I know it's crappy but doing the rest would be easy.
Anyways:
http://i123.photobucket.com/albums/o...ack/errors.jpg
OKAY I KNOW IT LOOKS UGLY. I didn't add the check to check when the game's over but BEAR WITH ME. These are all examples of how it messes up.
Here's the WHOLE entire check:
Code:Public Class GameEngine
Inherits System.Windows.Forms.Form '?? i'm not sure why that's there
Dim pb1 As Boolean 'pb1-9 are checks for the paint to draw line or not.
Dim pb2 As Boolean ' false = Don't draw, true = draw
Dim pb3 As Boolean
Dim pb4 As Boolean
Dim pb5 As Boolean
Dim pb6 As Boolean
Dim pb7 As Boolean
Dim pb8 As Boolean
Dim pb9 As Boolean
Dim player As Boolean ' x or o. False=O,True=X
' that's just my example.
'Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
'Dim linePen1 As New Pen(Color.Red, 1)
'
' With e.Graphics
' .DrawLine(linePen1, 0, 0, PictureBox1.Width, PictureBox1.Height)
' End With
'End Sub
Dim box(9) As Integer 'state of box (0=nothing,1=o,2=x) (box(1) = top left, box(2) = top middle, box(3) = top right, box(4) = middle left, etc. OH! box(0) is the state. if 1,2,3, and o wins, 1230. if 2,4,7,x wins, 2471
Dim linePen1 As New Pen(Color.Red, 4)
Private Sub GameEngine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
box(0) = 0
box(1) = 0
box(2) = 0
box(3) = 0
box(4) = 0
box(5) = 0
box(6) = 0
box(7) = 0
box(8) = 0
box(9) = 0
'look at next post
Private Sub CS()
'CS is the check.
'box(x) = 1 is O
'box(y) = 2 is X
If (box(1) And box(2) = 1) Then 'just a test to see if it works or not. fail :[
If box(3) = 1 Then
box(0) = 1230
Call win()
End If
End If
If (box(1) And box(2) And box(3) = 2) Then
box(0) = 1231
Call win()
End If
If (box(1) And box(4) And box(7) = 1) Then
box(0) = 1470
Call win()
End If
If (box(1) And box(4) And box(7) = 2) Then
box(0) = 1471
Call win()
End If
If (box(1) And box(5) And box(9) = 1) Then
box(0) = 1590
Call win()
End If
If (box(1) And box(5) And box(9) = 2) Then
box(0) = 1591
Call win()
End If
If (box(2) And box(5) And box(8) = 1) Then
box(0) = 2580
Call win()
End If
If (box(2) And box(5) And box(8) = 2) Then
box(0) = 2581
Call win()
End If
If (box(3) And box(6) And box(9) = 1) Then
box(0) = 3690
Call win()
End If
If (box(3) And box(6) And box(9) = 2) Then
box(0) = 3691
Call win()
End If
If (box(4) And box(5) And box(6) = 1) Then
box(0) = 4560
Call win()
End If
If (box(4) And box(5) And box(6) = 2) Then
box(0) = 4561
Call win()
End If
If (box(7) And box(8) And box(9) = 1) Then
box(0) = 7890
Call win()
End If
If (box(7) And box(8) And box(9) = 2) Then
box(0) = 7891
Call win()
End If
If (box(3) And box(5) And box(7) = 1) Then
box(0) = 3570
Call win()
End If
If (box(3) And box(5) And box(7) = 2) Then
box(0) = 3571
Call win()
End If
End Sub
Private Sub win()
'win. draws the lines.
Select Case box(0)
Case 1230, 1231
pb1 = True 'paint checks = true.
pb2 = True
pb3 = True
PictureBox1.Refresh()'draws line
PictureBox2.Refresh()
PictureBox3.Refresh()
Case 4560, 4561
pb4 = True
pb5 = True
pb6 = True
PictureBox4.Refresh()
PictureBox5.Refresh()
PictureBox6.Refresh()
Case 7890, 7891
pb7 = True
pb8 = True
pb9 = True
PictureBox7.Refresh()
PictureBox8.Refresh
PictureBox9.Refresh()
Case 1470, 1471
pb1 = True
pb4 = True
pb7 = True
PictureBox1.Refresh()
PictureBox4.Refresh()
PictureBox7.Refresh()
Case 2580, 2581
pb2 = True
pb5 = True
pb8 = True
PictureBox2.Refresh()
PictureBox5.Refresh()
PictureBox8.Refresh()
Case 3690, 3691
pb3 = True
pb6 = True
pb9 = True
PictureBox3.Refresh()
PictureBox6.Refresh()
PictureBox9.Refresh()
Case 1590, 1591
pb1 = True
pb5 = True
pb9 = True
PictureBox1.Refresh()
PictureBox5.Refresh()
PictureBox9.Refresh()
Case 3570, 3571
pb3 = True
pb5 = True
pb7 = True
PictureBox3.Refresh()
PictureBox5.Refresh()
PictureBox7.Refresh()
End Select
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
If box(1) <> 0 Then Exit Sub 'check whether box is ticked or not.
If player = False Then '
player = True
box(1) = 1
Dim RandomNumber As Integer, randomNum As New Random 'For the picture.
RandomNumber = randomNum.Next(1, 11)
If RandomNumber = 1 Then
PictureBox1.Image = My.Resources.o1
ElseIf RandomNumber = 2 Then
PictureBox1.Image = My.Resources.o2
ElseIf RandomNumber = 3 Then
PictureBox1.Image = My.Resources.o3
ElseIf RandomNumber = 4 Then
PictureBox1.Image = My.Resources.o4
ElseIf RandomNumber = 5 Then
PictureBox1.Image = My.Resources.o5
ElseIf RandomNumber = 6 Then
PictureBox1.Image = My.Resources.o6
ElseIf RandomNumber = 7 Then
PictureBox1.Image = My.Resources.o7
ElseIf RandomNumber = 8 Then
PictureBox1.Image = My.Resources.o8
ElseIf RandomNumber = 9 Then
PictureBox1.Image = My.Resources.o9
ElseIf RandomNumber = 10 Then
PictureBox1.Image = My.Resources.o10
End If
ElseIf player = True Then
player = False
box(1) = 2
Dim RandomNumber As Integer, randomNum As New Random
RandomNumber = randomNum.Next(1, 11)
If RandomNumber = 1 Then
PictureBox1.Image = My.Resources.x1
ElseIf RandomNumber = 2 Then
PictureBox1.Image = My.Resources.x2
ElseIf RandomNumber = 3 Then
PictureBox1.Image = My.Resources.x3
ElseIf RandomNumber = 4 Then
PictureBox1.Image = My.Resources.x4
ElseIf RandomNumber = 5 Then
PictureBox1.Image = My.Resources.x5
ElseIf RandomNumber = 6 Then
PictureBox1.Image = My.Resources.x6
ElseIf RandomNumber = 7 Then
PictureBox1.Image = My.Resources.x7
ElseIf RandomNumber = 8 Then
PictureBox1.Image = My.Resources.x8
ElseIf RandomNumber = 9 Then
PictureBox1.Image = My.Resources.x9
ElseIf RandomNumber = 10 Then
PictureBox1.Image = My.Resources.x10
End If
End If
Call CS()
End Sub
that pretty much happens for all 9.
Private Sub picb1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If pb1 = False Then Exit Sub
Select Case box(0)
Case 1470, 1471
e.Graphics.DrawLine(linePen1, 75, 5, 75, 150)
Case 1230, 1231
e.Graphics.DrawLine(linePen1, 5, 75, 150, 75)
Case 1590, 1591
e.Graphics.DrawLine(linePen1, 5, 5, PictureBox1.Width, PictureBox1.Height)
End Select
End Sub
Private Sub picb2(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
If pb2 = False Then Exit Sub
Select Case box(0)
Case 1230, 1231
e.Graphics.DrawLine(linePen1, 0, 75, 150, 75)
Case 2580, 2581
e.Graphics.DrawLine(linePen1, 75, 5, 75, 150)
End Select
End Sub
Private Sub picb3(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox3.Paint
If pb3 = False Then Exit Sub
Select Case box(0)
Case 1230, 1231
e.Graphics.DrawLine(linePen1, 0, 75, 145, 75)
Case 3690, 3691
e.Graphics.DrawLine(linePen1, 75, 5, 75, 150)
Case 3570, 3571
e.Graphics.DrawLine(linePen1, 145, 5, 0, 150)
End Select
End Sub
Private Sub picb4(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox4.Paint
If pb4 = False Then Exit Sub
Select Case box(0)
Case 4560, 4561
e.Graphics.DrawLine(linePen1, 5, 75, 150, 75)
Case 1470, 1471
e.Graphics.DrawLine(linePen1, 75, 0, 75, 150)
End Select
End Sub
Private Sub picb5(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox5.Paint
If pb5 = False Then Exit Sub
Select Case box(0)
Case 4560, 4561
e.Graphics.DrawLine(linePen1, 0, 75, 150, 75)
Case 2580, 2581
e.Graphics.DrawLine(linePen1, 75, 0, 75, 150)
Case 3570, 3571
e.Graphics.DrawLine(linePen1, 150, 0, 0, 150)
Case 1590, 1591
e.Graphics.DrawLine(linePen1, 0, 0, 150, 150)
End Select
End Sub
Private Sub picb6(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox6.Paint
If pb6 = False Then Exit Sub
Select Case box(0)
Case 4560, 4561
e.Graphics.DrawLine(linePen1, 0, 75, 150, 75)
Case 3690, 3691
e.Graphics.DrawLine(linePen1, 75, 0, 75, 150)
End Select
End Sub
Private Sub picb7(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox7.Paint
If pb7 = False Then Exit Sub
Select Case box(0)
Case 7890, 7891
e.Graphics.DrawLine(linePen1, 0, 75, 150, 75)
Case 1470, 1471
e.Graphics.DrawLine(linePen1, 75, 0, 75, 145)
Case 3570, 3571
e.Graphics.DrawLine(linePen1, 150, 0, 5, 145)
End Select
End Sub
Private Sub picb8(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox8.Paint
If pb8 = False Then Exit Sub
Select Case box(0)
Case 7890, 7891
e.Graphics.DrawLine(linePen1, 0, 75, 150, 75)
Case 2580, 2581
e.Graphics.DrawLine(linePen1, 75, 0, 75, 145)
End Select
End Sub
Private Sub picb9(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox9.Paint
If pb9 = False Then Exit Sub '
Select Case box(0)
Case 7890, 7891
e.Graphics.DrawLine(linePen1, 0, 75, 145, 75)
Case 3690, 3691
e.Graphics.DrawLine(linePen1, 75, 0, 75, 145)
Case 1590, 1591
e.Graphics.DrawLine(linePen1, 0, 0, 145, 145)
End Select
End Sub
part 3... drawing the lines
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'reset
player = False ' starts with "o"
box(0) = 0
box(1) = 0
box(2) = 0
box(3) = 0
box(4) = 0
box(5) = 0
box(6) = 0
box(7) = 0
box(8) = 0
box(9) = 0
PictureBox1.Image = Nothing
PictureBox2.Image = Nothing
PictureBox3.Image = Nothing
PictureBox4.Image = Nothing
PictureBox5.Image = Nothing
PictureBox6.Image = Nothing
PictureBox7.Image = Nothing
PictureBox8.Image = Nothing
PictureBox9.Image = Nothing
pb1 = False
pb2 = False
pb3 = False
pb4 = False
pb5 = False
pb6 = False
pb7 = False
pb8 = False
pb9 = False
End Sub
End Class
reset (button)
i did not test all the possiblities, but this should be close
edited last at - 7PM CDT 10/13/2008
Code:'Square reference
'1 2 3
'4 5 6
'7 8 9
'as array coord.
'00 01 02
'10 11 12
'20 21 22
Dim xoS As String(,) = New String(2, 2) {}, winAs As String
Private Sub StartGame()
'init array - start of game / application
For x As Integer = 0 To xoS.GetUpperBound(0)
For y As Integer = 0 To xoS.GetUpperBound(1)
xoS(x, y) = "NONE" 'set all elements
Next
Next
End Sub
Private Function MarkSquare(ByVal theSq As Integer, ByVal XorO As String) As Boolean
Dim r As Integer 'row
r = (theSq - 1) \ 3 'calc the row
Dim c As Integer 'column
c = (theSq - (r * 3)) - 1 'calc the column
If xoS(r, c) <> "NONE" Then Return False 'already picked!!! should not happen
xoS(r, c) = XorO 'set the square
End Function
'check each row
'check each col
'check (0,0) (1,1) (2,2)
'check (0,2) (1,1) (2,0)
Private Function checkwin() As Boolean
Dim OorX() As String = New String() {"X", "O"}
Dim inArow As Integer
For y As Integer = 0 To OorX.Length - 1
'odd balls - see square reference above
If OorX(y) = xoS(0, 0) AndAlso OorX(y) = xoS(1, 1) AndAlso OorX(y) = xoS(2, 2) Then
winAs = "ODD"
Return True
End If
If OorX(y) = xoS(0, 2) AndAlso OorX(y) = xoS(1, 1) AndAlso OorX(y) = xoS(2, 0) Then
winAs = "ODD"
Return True
End If
winAs = "ROWS"
For r As Integer = 0 To 2 'check by rows
inArow = 0
For c As Integer = 0 To 2
If OorX(y) = xoS(r, c) Then
inArow += 1
End If
Next
If inArow = 3 Then Return True
Next
winAs = "COLS"
For c As Integer = 0 To 2 'check by cols
inArow = 0
For r As Integer = 0 To 2
If OorX(y) = xoS(r, c) Then
inArow += 1
End If
Next
If inArow = 3 Then Return True
Next
Next
winAs = ""
Return False
End Function
:[[ it's so confusing.Quote:
Originally Posted by dbasnett
copy the previous code to your project. then add this code to a button click, and single step (F8) and see what it does.
Code:Stop
StartGame()
MarkSquare(3, "O") 'see square reference
MarkSquare(6, "O")
MarkSquare(9, "O")
checkwin()
Debug.WriteLine(winAs)
StartGame()
MarkSquare(7, "O")
MarkSquare(8, "O")
MarkSquare(9, "O")
checkwin()
Debug.WriteLine(winAs)