1,105,281 Community Members

Connect 4 game

Member Avatar
connect4
Newbie Poster
1 post since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello everyone,


By the way im portuguese so , sorry for my english and im new in VB.net so sorry if i dont understand somethings.


I really need everyones help, you may think that my question is kind off easy for you , but for me its a bit hard to get it on code.
so that said , im developing a program in windows forms,acctually its a game "connect 4" my program its almost all done but i need the most important part of the code.
Ive done matrix (kind off a board where u can see save positionings, you may call it to bidemensional array) the game is 6*9. having 6 lines and 9 collums.
Im using PictureBoxes for my board .I already have a function that verifys if you can play in a certain picturebox. All the pictureboxes are in order (see below the code so


you can get it)so i think its easy for knowing were the player plays.I have 2 kinds of playing pieces, the red(PECAVERMELHA and PECAMARELA -->"its in portuguese the playing


pieces ones).


Some words in portuguese that i have in the programme.
Amarela = yellow
Vermelha= red
ganhar = Win
trancar = lock
marcardor = marker
quadro = board


So the question is how do i make a fuction or more that ill verify if it has 4 playing pieces in the horizontal , vertical and diagonal way?
I kinda know what i have to do so it would really help me if u programmers cold help me with code.


So, thank you everyone who can help me :D

Contact Me:
josepanike@gmail.com

All of our code

Public Class Form1

    Public marcador, trancar(53) As Boolean
    Public quadro(6, 9) As PictureBox

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        i = 0
        marcador = True
        For i = 0 To 53
            trancar(i) = False
        Next

        '  _______________________________________________________
        ' |                                                       |  
        ' |Array bidimensional colunas por linhas do Connect 4    |
        ' |_______________________________________________________|


        quadro(1, 1) = Me.PB11
        quadro(1, 2) = Me.PB12
        quadro(1, 3) = Me.PB13
        quadro(1, 4) = Me.PB14
        quadro(1, 5) = Me.PB15
        quadro(1, 6) = Me.PB16
        quadro(1, 7) = Me.PB17
        quadro(1, 8) = Me.PB18
        quadro(1, 9) = Me.PB19

        quadro(2, 1) = Me.PB21
        quadro(2, 2) = Me.PB22
        quadro(2, 3) = Me.PB23
        quadro(2, 4) = Me.PB24
        quadro(2, 5) = Me.PB25
        quadro(2, 6) = Me.PB26
        quadro(2, 7) = Me.PB27
        quadro(2, 8) = Me.PB28
        quadro(2, 9) = Me.PB29

        quadro(3, 1) = Me.PB31
        quadro(3, 2) = Me.PB32
        quadro(3, 3) = Me.PB33
        quadro(3, 4) = Me.PB34
        quadro(3, 5) = Me.PB35
        quadro(3, 6) = Me.PB36
        quadro(3, 7) = Me.PB37
        quadro(3, 8) = Me.PB38
        quadro(3, 9) = Me.PB39

        quadro(4, 1) = Me.PB41
        quadro(4, 2) = Me.PB42
        quadro(4, 3) = Me.PB43
        quadro(4, 4) = Me.PB44
        quadro(4, 5) = Me.PB45
        quadro(4, 6) = Me.PB46
        quadro(4, 7) = Me.PB47
        quadro(4, 8) = Me.PB48
        quadro(4, 9) = Me.PB49

        quadro(5, 1) = Me.PB51
        quadro(5, 2) = Me.PB52
        quadro(5, 3) = Me.PB53
        quadro(5, 4) = Me.PB54
        quadro(5, 5) = Me.PB55
        quadro(5, 6) = Me.PB56
        quadro(5, 7) = Me.PB57
        quadro(5, 8) = Me.PB58
        quadro(5, 9) = Me.PB59

        quadro(6, 1) = Me.PB61
        quadro(6, 2) = Me.PB62
        quadro(6, 3) = Me.PB63
        quadro(6, 4) = Me.PB64
        quadro(6, 5) = Me.PB65
        quadro(6, 6) = Me.PB66
        quadro(6, 7) = Me.PB67
        quadro(6, 8) = Me.PB68
        quadro(6, 9) = Me.PB69


        Label1.Text = ("Player's 1 turn")
    End Sub

    ' Função para verificar se onde o utilizador deu o click tem alguma peça jogada. 
    'Se tiver nao pode jogar, se nao tiver verifica se tem alguma por baixo se nao tiver nao deixa o jogador jogar a peça.

    Function verifica(ByVal x As Integer, ByVal y As Integer) As Boolean

        Dim i As Integer

        If quadro(x, y).Image Is Nothing And x = 1 Then
            Return True
        Else
            For i = (x - 1) To 1 Step -1
                If quadro(i, y).Image Is Nothing Then
                    Return False
                End If
            Next
            Return True
        End If

    End Function

    Function ganhar_amarelo(ByVal x As Integer, ByVal y As Integer) As Boolean

    End Function

    Function ganhar_vermelho(ByVal x As Integer, ByVal y As Integer) As Boolean

    End Function

    Private Sub PictureBox11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
    PB61.Click, PB62.Click, PB63.Click, PB64.Click, PB65.Click, PB66.Click, PB67.Click, PB68.Click, PB69.Click, _
    PB51.Click, PB52.Click, PB53.Click, PB54.Click, PB55.Click, PB56.Click, PB57.Click, PB58.Click, PB59.Click, _
    PB41.Click, PB42.Click, PB43.Click, PB44.Click, PB45.Click, PB46.Click, PB47.Click, PB48.Click, PB49.Click, _
    PB31.Click, PB32.Click, PB33.Click, PB34.Click, PB35.Click, PB36.Click, PB37.Click, PB38.Click, PB39.Click, _
    PB21.Click, PB22.Click, PB23.Click, PB24.Click, PB25.Click, PB26.Click, PB27.Click, PB28.Click, PB29.Click, _
    PB11.Click, PB12.Click, PB13.Click, PB14.Click, PB15.Click, PB16.Click, PB17.Click, PB18.Click, PB19.Click



        Dim x, y As Integer
        Dim nome_picture As String
        nome_picture = sender.name

        x = nome_picture.Substring(2, 1)
        y = nome_picture.Substring(3, 1)

        If verifica(x, y) = True And quadro(x, y).Image Is Nothing Then
            If marcador = True Then
                Label1.Text = ("Player's 2 turn")
                quadro(x, y).Image = My.Resources.PECAVERMELHA
                ganhar_amarelo(x, y)
                marcador = False
                If quadro(1, 1).Image Is My.Resources.PECAVERMELHA And quadro(1, 2).Image Is My.Resources.PECAVERMELHA _
                And quadro(1, 3).Image Is My.Resources.PECAVERMELHA And quadro(1, 4).Image Is My.Resources.PECAVERMELHA Then
                    MsgBox("Player's 2 WIN")
                End If
            Else
                Label1.Text = ("Player's 1 turn")
                quadro(x, y).Image = My.Resources.PECAMARELA
                ganhar_vermelho(x, y)
                marcador = True
                If quadro(1, 1).Image Is My.Resources.PECAMARELA And quadro(1, 2).Image Is My.Resources.PECAMARELA _
                And quadro(1, 3).Image Is My.Resources.PECAMARELA And quadro(1, 4).Image Is My.Resources.PECAMARELA Then
                    MsgBox("Player's 1 WIN")
                End If
            End If
        End If
    End Sub
End Class

Our game
http://i42.tinypic.com/34pmdtg.jpg

Member Avatar
codeorder
Postaholic
2,027 posts since Aug 2010
Reputation Points: 197 [?]
Q&As Helped to Solve: 390 [?]
Skill Endorsements: 10 [?]
 
0
 

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.

Attachments connect.4_.png 14.84KB red.png 1.76KB yellow.png 1.67KB
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: