Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

[RESOLVED] Issue with preventing 3 numbers in a row

$
0
0
I've went back to my Bejeweled project and I'm currently stuck loading the gems. Right now I call:
Code:

'Nested Loop for each picturebox in the board array
        For x As Integer = 0 To 5
            For y As Integer = 0 To 7
                'New instance of a picturebox
                Dim pb As New PictureBox

                'Set it's properties
                With pb
                    .SizeMode = PictureBoxSizeMode.Zoom
                    .Size = New Size(50, 50)
                    .Location = New Point(x * 50, y * 50)
                    .Name = String.Format("BoardPiece{0}{1}", x, y)
                    Dim i As Integer = r.Next(0, 4)
                    .Image = rnd_image(i)
                    .Tag = i
                End With

                'Add it to the board array/panel
                arr_board(x, y) = pb
                pnl_board.Controls.Add(pb)
            Next
        Next

Which is fine, but when the game starts there will be some that are in 3, 4, or 5 in a row. To try and prevent that I add:
Code:

        'Check for matches and replace them
        For x As Integer = 2 To 5
            For y As Integer = 2 To 7
                'If:
                'x, y = x - 1, y
                'x, y = x - 2, y
                If arr_board(x, y).Tag.ToString = arr_board(x - 1, y).Tag.ToString AndAlso _
                    arr_board(x, y).Tag.ToString = arr_board(x - 2, y).Tag.ToString Then
                    'Get i
                    Dim i As Integer = CInt(arr_board(x, y).Tag)

                    'Loop until i is different
                    Do Until i <> CInt(arr_board(x, y).Tag)
                        i = r.Next(0, 4)
                        Console.WriteLine(i)
                    Loop

                    'Set a new image/tag
                    With arr_board(x, y)
                        .Image = rnd_image(i)
                        .Tag = i
                    End With

                    'If:
                    'x, y = x, y - 1
                    'x, y = x, y - 2
                ElseIf arr_board(x, y).Tag.ToString = arr_board(x, y - 1).Tag.ToString AndAlso _
                arr_board(x, y - 1).Tag.ToString = arr_board(x - 2, y).Tag.ToString Then
                    'Get i
                    Dim i As Integer = CInt(arr_board(x, y).Tag)

                    'Loop until i is different
                    Do Until i <> CInt(arr_board(x, y).Tag)
                        i = r.Next(0, 4)
                    Loop

                    'Set a new image/tag
                    With arr_board(x, y)
                        .Image = rnd_image(i)
                        .Tag = i
                    End With
                End If
            Next
        Next

However, whenever I run my program, I still get 3, 4, or 5 in a row. Could somebody shed some light for me?

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>