dcsimg
Results 1 to 4 of 4

Thread: Solution for connect 4

  1. #1

    Thread Starter
    Registered User
    Join Date
    Aug 2018
    Posts
    1

    Solution for connect 4

    Hi all,
    I have just finished coding (below) my connect 4 game and am wondering what the best way to go about making a "winner" finder. So that when someone get 4 in a row just a message box or something will appear. The code isnt to complicated, just uses textboxes and colours them depending on the variable (the button code is repeated for the next 6 columns just felt it unnecessary to post here).
    Any help is much apprieciated.
    Code:
    Public Class Form1
        Dim matrix(5, 6, 1) As Integer
        Dim b As Integer
        Dim a As Integer = 0
        Dim c As Integer
        Dim d As Integer
        Dim f As Integer
        Dim g As Integer
        Dim h As Integer
        Dim i As Integer
    
        Private Sub btn_new_Click(sender As Object, e As EventArgs) Handles btn_new.Click
            Dim p1 As String
            Dim p2 As String
            Dim o As Control  'Assings the letter "o" to a control
            For Each o In Me.Controls
                If TypeOf o Is TextBox Then
                    o.BackColor = Color.White 'makes the program search for all textboxes in the form and change the colour to white when clicked
                End If
            Next
            Dim y As Control
            For Each y In Me.Controls
                If TypeOf y Is Button Then
                    y.Enabled = True
                End If
            Next
            a = 0
            b = 0
            c = 0
            d = 0
            f = 0
            g = 0
            h = 0
            i = 0
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If a = 0 Then
                If b Mod 2 = 0 Then
                    TextBox1.BackColor = Color.Red
                End If
                If b Mod 2 <> 0 Then
                    TextBox1.BackColor = Color.Blue
                End If
            End If
    
            If a = 1 Then
                If b Mod 2 = 0 Then
                    TextBox14.BackColor = Color.Red
                End If
                If b Mod 2 <> 0 Then
                    TextBox14.BackColor = Color.Blue
                End If
            End If
            If a = 2 Then
                If b Mod 2 = 0 Then
                    TextBox21.BackColor = Color.Red
                End If
                If b Mod 2 <> 0 Then
                    TextBox21.BackColor = Color.Blue
                End If
            End If
            If a = 3 Then
                If b Mod 2 = 0 Then
                    TextBox28.BackColor = Color.Red
                End If
                If b Mod 2 <> 0 Then
                    TextBox28.BackColor = Color.Blue
                End If
            End If
            If a = 4 Then
                If b Mod 2 = 0 Then
                    TextBox35.BackColor = Color.Red
                End If
                If b Mod 2 <> 0 Then
                    TextBox35.BackColor = Color.Blue
                End If
            End If
            If a = 5 Then
                If b Mod 2 = 0 Then
                    TextBox42.BackColor = Color.Red
                End If
                If b Mod 2 <> 0 Then
                    TextBox42.BackColor = Color.Blue
                End If
            End If
            a = a + 1
            b = b + 1
            If a = 6 Then
                Button1.Enabled = False
            End If
        End Sub
    Last edited by si_the_geek; Aug 20th, 2018 at 07:46 AM. Reason: added Code tags

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    399

    Re: Solution for connect 4

    Sorry, I won't be posting any actual code.

    A big part of writing a program is to have a good understanding of the entire problem and taking that into consideration before you start writing code.

    The code you posted above makes me think that you plowed ahead with just getting the visual functionality to work without consideration as to how you could then get the "win condition" logic to work.

    Presumably this is homework, and as is always the case when people ask homework questions, we have no idea what has been taught so far, etc. So suggestions are frequently made that are beyond the scope of the class or contain concepts not yet taught.

    Having individual textboxes that are differentiated by a numeric value in their name complicates the "win condition" logic. Were I asked to solve this, the first thing I would do is create a two dimensional textbox object array and populate that array with each textbox on the form, the 0th row in the array being the bottom-most row of the game, and move upward.

    Once that is done, I would set up a function that contains loops that would check for horizontal wins, vertical wins, and diagonal wins, always testing if 4 "touching" textboxes have the same backcolor, and this function would be called after each move. If a winner is found, the function would return a state of True, otherwise it would return False. Based on this function's return status, the code would then either allow the game to continue or would declare the winner and end the game.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,118

    Re: Solution for connect 4

    Welcome to VBForums

    While not related to your question, you seem to not be aware that If statements have an optional Else, which would make your code shorter and easier to read (and make it run slightly faster). Take this section of code as an example:
    Code:
            If a = 0 Then
                If b Mod 2 = 0 Then
                    TextBox1.BackColor = Color.Red
                End If
                If b Mod 2 <> 0 Then
                    TextBox1.BackColor = Color.Blue
                End If
            End If
            If a = 1 Then
                If b Mod 2 = 0 Then
                    TextBox14.BackColor = Color.Red
                End If
                If b Mod 2 <> 0 Then
                    TextBox14.BackColor = Color.Blue
                End If
            End If
    ...it can be reduced to the following, which has the same effect:
    Code:
            If a = 0 Then
                If b Mod 2 = 0 Then
                    TextBox1.BackColor = Color.Red
                Else      '(we already know b Mod 2 is not 0, because it would have gone into the block above)
                    TextBox1.BackColor = Color.Blue
                End If
            ElseIf a = 1 Then      '(if a is 0 this check doesn't need to happen, so the Else before the If avoids it)
                If b Mod 2 = 0 Then
                    TextBox14.BackColor = Color.Red
                Else
                    TextBox14.BackColor = Color.Blue
                End If
            End If
    If you want to, you can take it even further (but you might find it hard to read) by using the If operator (rather than the If statement) for the "If b" sections:
    Code:
            If a = 0 Then
                TextBox1.BackColor = If(b Mod 2 = 0, Color.Red, Color.Blue)
            ElseIf a = 1 Then
                TextBox14.BackColor = If(b Mod 2 = 0, Color.Red, Color.Blue)
            End If

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,583

    Re: Solution for connect 4

    You have the following array in your code.

    Dim matrix(5, 6, 1) As Integer

    I assume that was created to reflect the status of the game. I'm not sure that you need the third dimension, as it takes more space, but you can choose to use it as you wish.
    Given, that the game "board" is 6 high (0-5), and 7 wide (0-6), those are the first two dimensions of your matrix. The third dimension is 2 (0-1) which would infer that dimension 0 would be for one players piece locations, and dimension 1 would be for the other players piece location.

    If you updated the matrix with each drop of a piece, after the first player has reached 4 pieces dropped, you would start checking for a win after every drop. You would look for 4 in a row, column, or diagonal in either dimension 0, if that is the player who just dropped a piece, or in dimension 1 because that player dropped a piece. (I assume the player / dimension choice would be based on b mod 2).

    While, having the third dimension in the matrix takes more space, it can make the determining the winner code simpler, because you would use the same code for either player, and you don't have to check if a piece belongs to player 1 or player 2 in the code, since only one player's pieces would be in the dimension of the matrix you are scanning.

    I would think the scan could be optimized, since the last piece dropped would have to be the one creating a connection of 4 (or more), you would only have to start with that piece and see if it connects to 3 other pieces, vertically, horizontally, or diagonally.

    The second option with the matrix, which I probably would have taken, not having considered using a third dimension to separate player's pieces into separate planes, would just be two dimensional matrix (5,6), and storing the player's number (probably using 1 and 2 for the players pieces, and using 0 for the empty positions). The scan would then be pretty much the same, just initializing which pieces it was looking for (1 or 2) depending on which player dropped the current piece, and trying to find four connecting pieces with that number (starting from the just dropped piece position).
    Last edited by passel; Aug 21st, 2018 at 10:48 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width