Mod operator

Hello!

New challenge for us… lol

// 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

I’m getting a syntax error on If…

i’m crazy. i know it.

No errors here (besides rows and columns do not exist)
Maybe an invisible character?

As an aside, you could skip the if entirely with:

startX = startX + 1 - ( startX mod 2 )
1 Like

I copied the actual code, it works fine.

Crazy question : in the screenshot
Does it say ( IF ) or does it say (Lowercase L + F)

3 Likes

As we didn’t get the error area, maybe that is the problem:

Sounds like a good suggestion. The only thing that suggests it’s not that is the fact that the syntax colouring has made it blue.

Show us the error messages presented for the code.

2 Likes

Copied and ran code and no error. I replaced rows - columns with 100 as they do not exist for me.

Ran and no error

I was able to insert an invisible ASCII character (well is a space but not standard) and the IF still blue:

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.

1 Like

nope. checked again. wrote again…

You are still missing the bottom area of the IDE from your screenshots.

full method:

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

You can’t embed a Function like that.

2 Likes

You don’t get syntax error for End Function?

That is why is important the bottom part of the IDE.

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 think he copied and pasted the function from the Navigator.

1 Like

Instead of just saying

Tell us what syntax error is reported in the IDE. That’s why people are asking you to show the lower portion of the IDE.

2 Likes

no!

Syntax Error… thats all…

no error about “end function” above

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.