Results 1 to 14 of 14

Thread: Detecting adjacent label backcolor

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Detecting adjacent label backcolor

    Hello guys!

    I am making a rather simple game and need some help building the logic.

    Basically it is a game of strategy (probably not a very good one mind lol!). What it involves is a grid of 100 squares (10 x 10) and is a two player.

    The players take it in turns to 'capture' a square by clicking on it to make it change the backcolor property to the colour of their team. Once all the squares have been changed from their default colour the game is over and the player with the most squares of their colour wins.

    The really simple answer is, the first person that goes will win, unless I add some logic that will allow players to 'capture' other player's squares. However, I want there to be rules about what they can 'capture' and when, otherwise they'll just be able to keep capturing each others square willy nilly.

    I have it so that the red player cannot simply 'capture' a blue square for the sake of it and vice versa. I want to be able to code the rules so that if a blue square is sharing two (or three, I haven't decided that bit yet) sides of red squares then the red player can 'capture' it. The rules are going to be a little more complicated than that to add a bit of a challenge once the board starts to get filled.

    What I need to know, is how to make the application detect what the surrounding squares colours are if a player tries to capture another player's square.

    The code I have so far is not really relevant to this bit of the exercise, it is merely detecting what colour the current square is at and if it's anything other than the default colour then it doesn't allow it to be selected again. However, if anyone wants to see what I've got let me know.

    Cheers guys

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Detecting adjacent label backcolor

    I assume that each square is represented by a Label... If this is the case, you simply add the 100 labels to a 10x10 array and you can get to any specific label by the array index.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Re: Detecting adjacent label backcolor

    You are indeed correct. However, I'm crap when it comes to arrays. Would you be so kind as to show me how to add them then compare the info that I need please?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,383

    Re: Detecting adjacent label backcolor

    Take a look in my signature at my Connect4 game. I have a 7X7 grid of panels, and in my logic to check if there is a win would be similar to what you're needing.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Re: Detecting adjacent label backcolor

    Just so I can get this clear. I've looked at your Connect4 game dday9.

    Is it best if I declare a grid and then check the co-ordinates of the surrounding squares to get the backcolor property? Or am I missing the point entirely?

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,383

    Re: Detecting adjacent label backcolor

    It is best to do that, because you can easily get any square in that grid and any squares surrouding it.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Re: Detecting adjacent label backcolor

    Cool, so I've created the grid. Now how do I invoke a 'click' on a particular square within the grid?

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,383

    Re: Detecting adjacent label backcolor

    Are you talking about the click event for the label? How are you loading them, dynamically or in the designer? If it's the former, then you'd add a handler:
    Code:
    For x As Integer = 0 To 9
       For y As Integer = 0 To 9
               Dim lbl As New Label
               With lbl
                  .Text = "lbl" & x.ToString() & y.ToString()
                  .Location = New Point(x * lbl.Size.Width + 1, y * lbl.Size.Height + 1)
               End With
               AddHandler lbl.Click AddressOf lbl_Click
    
              Next
    Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Re: Detecting adjacent label backcolor

    I'm trying it without the labels and just using the grid. I've got the following code.

    Code:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Panel1.Controls.Clear()
    
            For x As Integer = 0 To 10
                For y As Integer = 0 To 10
                    Dim pnl As New Panel
                    With pnl
                        .BorderStyle = BorderStyle.FixedSingle
                        .Size = New Size(20, 20)
                        .Location = New Point(x * 26, y * 26)
                        .Tag = "null"
                    End With
                    grid(x, y) = pnl
                    Panel1.Controls.Add(pnl)
    
                Next
            Next
        End Sub
    
        Private Sub Panel1_Click(sender As Object, e As System.EventArgs) Handles Panel1.Click
            If grid(MousePosition.X, MousePosition.Y).Tag.ToString = "null" Then
                grid(Panel.MousePosition.X, Panel.MousePosition.Y).BackColor = Color.Red
            End If
        End Sub
    And it isn't showing any errors in the code, however when I click an area within the grid nothing happens. But after trying a few clicks I get an error message pop up: "Index was outside the bounds of the array."


  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,383

    Re: Detecting adjacent label backcolor

    Well, something to remember is that your array is 0 based. So your grid would look like this:
    Code:
    Dim Grid(9, 9) As Panel
    And the For loops would look like this:
    Code:
    For x As Integer = 0 To 9
                For y As Integer = 0 To 9
    With the for loop you'll have it will go from Grid(0, 0) to Grid(10, 10). The way I show above will keep the index within the bounds. But as for your question about the click event, then I would do something like this: after Panel1.Controls.Add(pnl) add
    Code:
    AddHandler pnl.Click, AddressOf pnl_Click
    that will raise an error saying that pnl_Click aint declared. Go ahead and generate the method stub. In the sub you'll get the panel that's clicked like this:
    Code:
    Dim pnl As Panel = DirectCast(sender, Panel)
    from there you can change pnl's back color to w/e color. In the end it should look like this:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private grid(9, 9) As Panel
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            For x As Integer = 0 To 9
                For y As Integer = 0 To 9
                    Dim pnl As New Panel
                    With pnl
                        .BorderStyle = BorderStyle.FixedSingle
                        .Size = New Size(20, 20)
                        .Location = New Point(x * 26, y * 26)
                        .Tag = "null"
                    End With
                    grid(x, y) = pnl
                    Panel1.Controls.Add(pnl)
    
                    AddHandler pnl.Click, AddressOf pnl_Click
                Next
            Next
    
        End Sub
    
        Private Sub pnl_Click(sender As Object, e As EventArgs)
            Dim pnl As Panel = DirectCast(sender, Panel)
            pnl.BackColor = Color.Red
        End Sub
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Re: Detecting adjacent label backcolor

    You my friend are a genius!

    Just one other question, sorry, is now the detection of the surrounding squares.

    I presume I just need to get the co-ordinates of the squares around the select one and query the backcolor value, but don't know how lol

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,383

    Re: Detecting adjacent label backcolor

    You don't necessarily need the coordinates, this is how I would get the surrounding panels:
    Code:
    Dim pnl As Panel = DirectCast(sender, Panel)
            pnl.BackColor = Color.Red
    
    
            Dim toppnl, leftpnl, bottompnl, rightpnl As Panel
    
            Dim col, row As Integer
            For x As Integer = 0 To 9
                For y As Integer = 0 To 9
                    If pnl.Name = grid(x, y).Name Then
                        col = x
                        row = y
                        Exit For
                    End If
                Next
            Next
    
    
            'The reason we check for the 0 and 9 is because grid(0,0) won't have top or left panel
            If col = 0 Then
                leftpnl = Nothing
                If row = 0 Then
                    toppnl = Nothing
                    rightpnl = grid(col + 1, row)
                    bottompnl = grid(col, row + 1)
                ElseIf row = 9 Then
                    bottompnl = Nothing
                    toppnl = grid(col, row - 1)
                    rightpnl = grid(col + 1, row)
                Else
                    bottompnl = grid(col, row + 1)
                    toppnl = grid(col, row - 1)
                    rightpnl = grid(col + 1, row)
                End If
            ElseIf col = 9 Then
                rightpnl = Nothing
                If row = 0 Then
                    toppnl = Nothing
                    leftpnl = grid(col - 1, row)
                    bottompnl = grid(col, row + 1)
                ElseIf row = 9 Then
                    bottompnl = Nothing
                    toppnl = grid(col, row - 1)
                    leftpnl = grid(col - 1, row)
                Else
                    bottompnl = grid(col, row + 1)
                    toppnl = grid(col, row - 1)
                    leftpnl = grid(col - 1, row)
                End If
            Else
                leftpnl = grid(col - 1, row)
                rightpnl = grid(col + 1, row)
                toppnl = grid(col, row - 1)
                bottompnl = grid(col, row + 1)
            End If
    Last edited by dday9; Feb 21st, 2013 at 06:09 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  13. #13
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Detecting adjacent label backcolor

    You could store the panel's coordinates in the Tag property as a Point when you create it. That way it's more efficient since you don't have to loop the whole grid to "search" for the panel.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Re: Detecting adjacent label backcolor

    Thanks guys. I have tried to digest what your suggestion is doing dday9 but I'm a total tard sometimes lol, what is it actually doing?. How would I do what you've suggested stanav?

Posting Permissions

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



Click Here to Expand Forum to Full Width