-
May 7th, 2013, 02:01 PM
#1
Thread Starter
Junior Member
Connect Four Help
Hello, I'm trying to make a connect four game. It's all going well until i try to determine the winner. I'm using images for the black and red pieces (which is required) and I can't do a statement like If pic1.image=pic7.image and intplayer=1 then messagebox.show("black wins"). I get an error with the "=". here's my code, skip to the bottom for the winner part. Feel free to give any suggestions on an easier way to do this if you'd like. This is a 7x6 board. I figured there would be a loop or something but I don't know how to do that. And my teacher mentioned something about two dimensional arrays..? Thanks!
Public Class Form1
Dim btnclicked As Integer
Dim imgplayer As Image = Image.FromFile("black.png")
Dim intcounter1 As Integer
Dim intcounter2 As Integer
Dim intcounter3 As Integer
Dim intcounter4 As Integer
Dim intcounter5 As Integer
Dim intcounter6 As Integer
Dim intcounter7 As Integer
Dim intplayer As Integer = 1 'represents black
Private Sub btnMove_Click(sender As Object, e As EventArgs) Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click
btnclicked = sender.tag
If btnclicked = 1 Then
Call Move1()
ElseIf btnclicked = 2 Then
Call Move2()
ElseIf btnclicked = 3 Then
Call Move3()
ElseIf btnclicked = 4 Then
Call Move4()
ElseIf btnclicked = 5 Then
Call Move5()
ElseIf btnclicked = 6 Then
Call Move6()
ElseIf btnclicked = 7 Then
Call Move7()
End If
End Sub
Sub Move1()
If intcounter1 < 1 And btnclicked = 1 Then
pic6.Image = imgplayer
ElseIf intcounter1 < 2 And btnclicked = 1 Then
pic5.Image = imgplayer
ElseIf intcounter1 < 3 And btnclicked = 1 Then
pic4.Image = imgplayer
ElseIf intcounter1 < 4 And btnclicked = 1 Then
pic3.Image = imgplayer
ElseIf intcounter1 < 5 And btnclicked = 1 Then
pic2.Image = imgplayer
ElseIf intcounter1 < 6 And btnclicked = 1 Then
pic1.Image = imgplayer
End If
intcounter1 = intcounter1 + 1
If intplayer = 1 Then
intplayer = 2
imgplayer = Image.FromFile("red.png")
Else
intplayer = 1
imgplayer = Image.FromFile("black.png")
End If
End Sub
Sub Move2()
If intcounter2 < 1 And btnclicked = 2 Then
pic12.Image = imgplayer
ElseIf intcounter2 < 2 And btnclicked = 2 Then
pic11.Image = imgplayer
ElseIf intcounter2 < 3 And btnclicked = 2 Then
pic10.Image = imgplayer
ElseIf intcounter2 < 4 And btnclicked = 2 Then
pic9.Image = imgplayer
ElseIf intcounter2 < 5 And btnclicked = 2 Then
pic8.Image = imgplayer
ElseIf intcounter2 < 6 And btnclicked = 2 Then
pic7.Image = imgplayer
End If
intcounter2 = intcounter2 + 1
If intplayer = 1 Then
intplayer = 2
imgplayer = Image.FromFile("red.png")
Else
intplayer = 1
imgplayer = Image.FromFile("black.png")
End If
End Sub
Sub Move3()
If intcounter3 < 1 And btnclicked = 3 Then
pic18.Image = imgplayer
ElseIf intcounter3 < 2 And btnclicked = 3 Then
pic17.Image = imgplayer
ElseIf intcounter3 < 3 And btnclicked = 3 Then
pic16.Image = imgplayer
ElseIf intcounter3 < 4 And btnclicked = 3 Then
pic15.Image = imgplayer
ElseIf intcounter3 < 5 And btnclicked = 3 Then
pic14.Image = imgplayer
ElseIf intcounter3 < 6 And btnclicked = 3 Then
pic13.Image = imgplayer
End If
intcounter3 = intcounter3 + 1
If intplayer = 1 Then
intplayer = 2
imgplayer = Image.FromFile("red.png")
Else
intplayer = 1
imgplayer = Image.FromFile("black.png")
End If
End Sub
Sub Move4()
If intcounter4 < 1 And btnclicked = 4 Then
pic24.Image = imgplayer
ElseIf intcounter4 < 2 And btnclicked = 4 Then
pic23.Image = imgplayer
ElseIf intcounter4 < 3 And btnclicked = 4 Then
pic22.Image = imgplayer
ElseIf intcounter4 < 4 And btnclicked = 4 Then
pic21.Image = imgplayer
ElseIf intcounter4 < 5 And btnclicked = 4 Then
pic20.Image = imgplayer
ElseIf intcounter4 < 6 And btnclicked = 4 Then
pic19.Image = imgplayer
End If
intcounter4 = intcounter4 + 1
If intplayer = 1 Then
intplayer = 2
imgplayer = Image.FromFile("red.png")
Else
intplayer = 1
imgplayer = Image.FromFile("black.png")
End If
End Sub
Sub Move5()
If intcounter5 < 1 And btnclicked = 5 Then
pic30.Image = imgplayer
ElseIf intcounter5 < 2 And btnclicked = 5 Then
pic29.Image = imgplayer
ElseIf intcounter5 < 3 And btnclicked = 5 Then
pic28.Image = imgplayer
ElseIf intcounter5 < 4 And btnclicked = 5 Then
pic27.Image = imgplayer
ElseIf intcounter5 < 5 And btnclicked = 5 Then
pic26.Image = imgplayer
ElseIf intcounter5 < 6 And btnclicked = 5 Then
pic25.Image = imgplayer
End If
intcounter5 = intcounter5 + 1
If intplayer = 1 Then
intplayer = 2
imgplayer = Image.FromFile("red.png")
Else
intplayer = 1
imgplayer = Image.FromFile("black.png")
End If
End Sub
Sub Move6()
If intcounter6 < 1 And btnclicked = 6 Then
pic36.Image = imgplayer
ElseIf intcounter6 < 2 And btnclicked = 6 Then
pic35.Image = imgplayer
ElseIf intcounter6 < 3 And btnclicked = 6 Then
pic34.Image = imgplayer
ElseIf intcounter6 < 4 And btnclicked = 6 Then
pic33.Image = imgplayer
ElseIf intcounter6 < 5 And btnclicked = 6 Then
pic32.Image = imgplayer
ElseIf intcounter6 < 6 And btnclicked = 6 Then
pic31.Image = imgplayer
End If
intcounter6 = intcounter6 + 1
If intplayer = 1 Then
intplayer = 2
imgplayer = Image.FromFile("red.png")
Else
intplayer = 1
imgplayer = Image.FromFile("black.png")
End If
End Sub
Sub Move7()
If intcounter7 < 1 And btnclicked = 7 Then
pic42.Image = imgplayer
ElseIf intcounter7 < 2 And btnclicked = 7 Then
pic41.Image = imgplayer
ElseIf intcounter7 < 3 And btnclicked = 7 Then
pic40.Image = imgplayer
ElseIf intcounter7 < 4 And btnclicked = 7 Then
pic39.Image = imgplayer
ElseIf intcounter7 < 5 And btnclicked = 7 Then
pic38.Image = imgplayer
ElseIf intcounter7 < 6 And btnclicked = 7 Then
pic37.Image = imgplayer
End If
intcounter7 = intcounter7 + 1
If intplayer = 1 Then
intplayer = 2
imgplayer = Image.FromFile("red.png")
Else
intplayer = 1
imgplayer = Image.FromFile("black.png")
End If
End Sub
Sub Winner()
If 'Where I Need Help
MessageBox.Show("Black Wins")
End If
End Sub
End Class
-
May 8th, 2013, 02:13 PM
#2
Re: Connect Four Help
I wrote a bunch of stuff about using the .Tag property of the PictureBox to hold a value indicating the image, but I see that you already know about the .Tag property, so I'll just say: Use that.
Aside from that:
1) You only need one sub, not all seven. Make IntCounter an array of Integer. The buttonClick variable holds the index into the array, so everywhere you have something like intCounterX, you'd use intCounter(buttonClick).
2) Load the images from the files only once and store them in variables (which you are already doing, actually), then just switch which variable image you are drawing.
3) Always use AndAlso and OrElse in place of And and Or unless it is essential that you do otherwise (I have seen one case other than bit manipulations where it was essential, and that was dodgy programming).
4) If the .Tag property of the picturebox holds something like 0 = empty, 1 = black, 2 = red, then comparing the pictureboxes would just mean comparing:
CInt(pictureboxX.Tag) = CInt(pictureboxY.Tag)
which is MUCH easier than attempting to compare images.
5) Turn Option Strict ON unless your instructor foolishly forbids it (it has been known to happen). You aren't doing anything terribly wrong in that code with your implicit conversions, as they should be safe, but they are always slower than explicit conversions and often they are not safe. Why not learn good habits right from the start?
My usual boring signature: Nothing
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
|