As a Daniweb.com vb.net detective:D, I assigned myself to this case.
In order for me to fully understand the concept behind the strategy of creating a vb.net Connect 4 game, I had to start from the ground up and got some decent results.( view attached image ) Also attached, are the 2 images needed for the following beta.code to make this project work.New.Project, 1.Button, 1.Panel
Public Class Form1
#Region "-----===-----===-----===-----===-----=== DECLARATIONS ===-----===-----===-----===-----===-----"
Private myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\TEST\" '// folder for the 2 images.
Private imgRed As Image = Image.FromFile(myFolder & "red.png"), imgYellow As Image = Image.FromFile(myFolder & "yellow.png") '// the 2 images.
Private iPlayerInTurn As Integer = 1 '// keeps track of which player.
'// array for PictureBoxes, array for Horizontal score, array for Vertical score. :scores are added to each score array from each PictureBox's.Tag.
Private arPB(6, 9) As PictureBox, arHorzScore(6) As String, arVertScore(9) As String
Private s1Winner As String = "1111", s2Winner As String = "2222" '// score patterns to look for and compare against current score values.
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
createDynamicPictureBoxes()
'//--- FOR.BETA TESTING.
With Button1 : .Text = ".new" : .Cursor = Cursors.Hand : End With
With Me : .Text = "Connect 4 - vb.net"
.Left = .Left - 125 : .Top = .Top - 75
End With
'---\\
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
clearPictureBoxes()
End Sub
#Region "-----===-----===-----===-----===-----=== PICTUREBOXES ===-----===-----===-----===-----===-----"
Private Sub createDynamicPictureBoxes()
Dim xLoc As Integer = 5, yLoc As Integer = 5, pb As PictureBox = Nothing
For i As Integer = 1 To 6
xLoc = 5
For x As Integer = 1 To 9
pb = New PictureBox With {.Size = New Size(25, 25), .BorderStyle = BorderStyle.Fixed3D, .Cursor = Cursors.Hand, .SizeMode = PictureBoxSizeMode.Zoom}
With pb
.Location = New Point(xLoc, yLoc)
.Tag = 0 '// set .Tag as 0, since 1 and 2 is for players 1 and 2.
AddHandler .Click, AddressOf _pb_Click '// set an Event Handler for each PictureBox.
Panel1.Controls.Add(pb)
arPB(i, x) = pb '// set PictureBox to the appropriate PictureBox Array.
xLoc += .Width + 5
End With
Next
yLoc += 30
Next
End Sub
Private Sub clearPictureBoxes()
For i As Integer = 1 To 6 : For x As Integer = 1 To 9 : arPB(i, x).Image = Nothing : arPB(i, x).Tag = 0 : Next : Next '// .Clear images and reset .Tags.
iPlayerInTurn = 1 '// reset Player.
End Sub
Private Sub _pb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
With CType(sender, PictureBox) '// get sender.
If .Image Is Nothing Then '// check if sender contains an image.
.Tag = iPlayerInTurn '// set .Tag to the current player that clicked the PictureBox.
If iPlayerInTurn = 1 Then '// check which player clicked.
.Image = imgRed : iPlayerInTurn = 2 '// set image to player and set playerInTurn for next player.
Else '// if not 1st player...
.Image = imgYellow : iPlayerInTurn = 1
End If
checkScoreStatus() '// check/verify the score status.
End If
End With
End Sub
#End Region
#Region "-----===-----===-----===-----===-----=== SCORE ===-----===-----===-----===-----===-----"
Private Sub checkScoreStatus()
For i As Integer = 1 To 9 : arVertScore(i) = "" : Next '// clear all Vertical scores.
For iHorizontal As Integer = 1 To 6 '// loop thru rows.
arHorzScore(iHorizontal) = "" '// clear row's score.
For iVertical As Integer = 1 To 9 '// loop thru columns.
arHorzScore(iHorizontal) &= arPB(iHorizontal, iVertical).Tag '// set row's score, each score for each PictureBox in row.
arVertScore(iVertical) &= arPB(iHorizontal, iVertical).Tag '// set column's score, each score for each PictureBox in column.
Next
Next
'// since all the scores have been added from each PictureBox's.Tag, now you can verify if each score .Contains a winning score.
For i As Integer = 1 To 6 : checkIfWinningScore(arHorzScore(i)) : Next '// check Horizontal.
For i As Integer = 1 To 9 : checkIfWinningScore(arVertScore(i)) : Next '// check Vertical.
'//===== DIAGONAL SCORE CODE HERE =====\\
'TextBox1.Lines = arHorzScore '// FOR.TESTING
End Sub
Private Sub checkIfWinningScore(ByVal selScore As String)
With selScore '// if the winning score.Contains 4 similar#'s in a row, then "We Got a Weiner" xD.
If .Contains(s1Winner) Then msgWinner(1)
If .Contains(s2Winner) Then msgWinner(2)
End With
End Sub
Private Sub msgWinner(ByVal selWinner As Integer) '// easier to manage. :)
MsgBox("Player " & selWinner & " Wins", MsgBoxStyle.Information)
End Sub
#End Region
End Class I have gotten this beta connect.4.app to respond when there is a connect.4 in a row and in a column, though I have Not gotten the app to respond to the diagonal connect.4 "yet".
If you think that this provided.beta code is something you can work with and integrate into your own app, then I can continue on and possibly solve this connect.4 case in a shorter amount of time.