// Escolher a célula inicial
Var rng As New Random
Var startX As Integer = rng.InRange(1, rows - 2)
Var startY As Integer = rng.InRange(1, columns - 2)
If startX Mod 2 = 0 Then
startX = startX + 1
End If
If startY Mod 2 = 0 Then
startY = startY + 1
End If
If I can get a true invisible ASCII character (not one that is a space), I guess it will still show IF in blue but get syntax error/item does not exist.
Public Function generateNewMaze(columns As Integer, rows As Integer) As String
// Garantir que o número de colunas seja ímpar e >= 31
If columns < 31 Then columns = 31
If columns Mod 2 = 0 Then columns = columns + 1
// Garantir que o número de linhas seja >= 23
If rows < 23 Then rows = 23
If rows Mod 2 = 0 Then rows = rows + 1
// Inicializar o labirinto como uma matriz cheia de paredes (#)
Var maze(-1, -1) As String
Redim maze(rows - 1, columns - 1)
For i As Integer = 0 To rows - 1
For j As Integer = 0 To columns - 1
maze(i, j) = "#"
Next
Next
// Função auxiliar para verificar se uma célula está dentro dos limites
Function IsInBounds(x As Integer, y As Integer) As Boolean
Return x > 0 And x < rows - 1 And y > 0 And y < columns - 1
End Function
// Lista de fronteiras (paredes adjacentes)
Var frontier() As Pair
// Escolher a célula inicial
Var rng As New Random
Var startX As Integer = rng.InRange(1, rows - 2)
Var startY As Integer = rng.InRange(1, columns - 2)
if startX Mod 2 = 0 then
startX = startX +1
end if
if startY Mod 2 = 0 then
startY = startY +1
end if
maze(startX, startY) = "." // Marcar como parte do labirinto
frontier.AddRow(New Pair(startX, startY))
// Movimentos possíveis (cima, baixo, esquerda, direita)
Var directions() As Pair = Array(New Pair(-2, 0), New Pair(2, 0), New Pair(0, -2), New Pair(0, 2))
While frontier.LastRowIndex >= 0
// Escolher uma fronteira aleatória
Var currentIndex As Integer = rng.InRange(0, frontier.LastRowIndex)
Var current As Pair = frontier(currentIndex)
frontier.RemoveRowAt(currentIndex)
// Verificar vizinhos válidos
Var validNeighbors() As Pair
For Each dir As Pair In directions
Var nx As Integer = current.Left + dir.Left
Var ny As Integer = current.Right + dir.Right
If IsInBounds(nx, ny) And maze(nx, ny) = "." Then
validNeighbors.AddRow(New Pair(nx, ny))
End If
Next
// Se houver exatamente um vizinho válido, conectar as células
If validNeighbors.Count = 1 Then
Var neighbor As Pair = validNeighbors(0)
Var wallX As Integer = (current.Left + neighbor.Left) / 2
Var wallY As Integer = (current.Right + neighbor.Right) / 2
maze(wallX, wallY) = "."
maze(current.Left, current.Right) = "."
// Adicionar novas fronteiras
For Each dir As Pair In directions
Var fx As Integer = current.Left + dir.Left
Var fy As Integer = current.Right + dir.Right
If IsInBounds(fx, fy) And maze(fx, fy) = "#" Then
frontier.AddRow(New Pair(fx, fy))
End If
Next
End If
Wend
// Converter o labirinto em texto
Var result As String = ""
For i As Integer = 0 To rows - 1
For j As Integer = 0 To columns - 1
result = result + maze(i, j)
Next
result = result + EndOfLine
Next
Return result
End Function
We want to see the part at the bottom of the window, shown here in AlbertoD’s example. Just below the work Issues. That could hint at what the problem is. as you can see it may highlight the part of the line causing a problem. Alberto’s suggestion is that for example it could be the space between ‘If’ and ‘startX’, if it’s not a normal space. Some other related problem.
I took your code and pasted it into a new method.
I removed the embedded function, and created a dummy with the same name.
It works fine without an error.
Please do this:
Create a new method called GenerateMaze as string
Add rows and columns to the window or module as properties
Copy your code above from this screen (not from your existing code)
Paste it inside the new GenerateMaze function.
Remove these lines:
// Função auxiliar para verificar se uma célula está dentro dos limites
Function IsInBounds(x As Integer, y As Integer) As Boolean
Return x > 0 And x < rows - 1 And y > 0 And y < columns - 1
End Function
Add the function to the window or module
Function IsInBounds(x As Integer, y As Integer) As Boolean
Return x > 0 And x < rows - 1 And y > 0 And y < columns - 1
End Function
It needs rows and columns, but if they are properties of the window or module, you can set them before calling GenerateMaze, and they will be usable by both functions.