I'm trying to create a random path generator and I'm running into problems. Here is what I currently have:
The problem comes when I come to my Do loop in generate laberynth. If none of my conditional statements come back true then it just stays in an infinate loop. So what should I do so that if none of the conditions come true, then 'go back' a node?
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private grid(9, 9) As PictureBox
Private startNode As Point
Private endNode As Point
Private r As New Random
Private Sub generate_grid()
'Clear any existing controls
pnl_Grid.Controls.Clear()
'Loop from 0 to the nud's values -1
'The -1 is because I start from 0
For x As Integer = 0 To 9
For y As Integer = 0 To 9
'Declare a new instance of a picturebox
Dim pb As New PictureBox
'Set the picturebox's properties
With pb
.BackColor = Color.Black
.Location = New Point(x * 50, y * 50)
.Size = New Size(50, 50)
End With
'Add it to the panel
pnl_Grid.Controls.Add(pb)
'Set the grid(x, y) to the picturebox
grid(x, y) = pb
Next
Next
End Sub
Private Sub generate_known_nodes()
'Set the start/end nodes base on the nud's start/end x/y
startNode = New Point(0, r.Next(0, 9))
endNode = New Point(0, r.Next(0, 9))
'Let those start/end nodes stand out by setting the color to red
grid(startNode.X, startNode.Y).BackColor = Color.Red
grid(endNode.X, endNode.Y).BackColor = Color.Red
End Sub
Private Sub generate_laberynth()
Dim priorNode As Point = startNode
Do Until priorNode = endNode
Dim bool As Boolean = False
Do Until bool = True
Dim i As Integer = r.Next(0, 4)
Select Case i
Case 0
If priorNode.X - 1 <> -1 AndAlso priorNode.Y - 1 <> -1 AndAlso priorNode.Y + 1 <> 10 Then
If grid(priorNode.X - 1, priorNode.Y).BackColor <> Color.White AndAlso grid(priorNode.X, priorNode.Y - 1).BackColor <> Color.White AndAlso grid(priorNode.X, priorNode.Y + 1).BackColor <> Color.White Then
priorNode.X -= 1
bool = True
End If
End If
Case 1
If priorNode.X + 1 <> 10 AndAlso priorNode.Y - 1 <> -1 AndAlso priorNode.Y + 1 <> 10 Then
If grid(priorNode.X + 1, priorNode.Y).BackColor <> Color.White AndAlso grid(priorNode.X, priorNode.Y - 1).BackColor <> Color.White AndAlso grid(priorNode.X, priorNode.Y + 1).BackColor <> Color.White Then
priorNode.X += 1
bool = True
End If
End If
Case 2
If priorNode.X - 1 <> -1 AndAlso priorNode.Y - 1 <> -1 AndAlso priorNode.X + 1 <> 10 Then
If grid(priorNode.X, priorNode.Y - 1).BackColor <> Color.White AndAlso grid(priorNode.X - 1, priorNode.Y).BackColor <> Color.White AndAlso grid(priorNode.X + 1, priorNode.Y).BackColor <> Color.White Then
priorNode.Y -= 1
bool = True
End If
End If
Case 3
If priorNode.X - 1 <> -1 AndAlso priorNode.Y + 1 <> 10 AndAlso priorNode.X + 1 <> 10 Then
If grid(priorNode.X, priorNode.Y + 1).BackColor <> Color.White AndAlso grid(priorNode.X - 1, priorNode.Y).BackColor <> Color.White AndAlso grid(priorNode.X + 1, priorNode.Y).BackColor <> Color.White Then
priorNode.Y += 1
bool = True
End If
End If
End Select
Loop
If priorNode <> endNode Then
grid(priorNode.X, priorNode.Y).BackColor = Color.White
End If
Loop
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Call generate_grid()
Call generate_known_nodes()
Call generate_laberynth()
End Sub
End Class