S
S
Stepan2018-12-01 18:49:52
random numbers
Stepan, 2018-12-01 18:49:52

How to generate treasure in a maze?

I generate a maze in the following way:
1) I choose a starting point at the very bottom.
2) From it I start the cycle
3) I go forward a random number forward.
4) Then I choose, also randomly, to the right or left to move on.
5) I move a random number of times to the right or left
6) when I reach the very top of the array, I exit the main loop
Then I generate obstacles. That is, a simple loop: if the loop hits the path, then the obstacle is not generated, otherwise I randomly generate an obstacle. Now the very essence: you need to generate a treasure not at the very beginning and not at the end. But the main thing is that there is a path to it!
Path generation:

klet(ys, xs) = 0 'клетка старта
        Dim UpCount As Integer
        Do
            For i = 1 To Int(2 * Rnd() + 1) 'идём вперёд
                If y1 = 0 Then
                    klet(0, x1) = 2 'клетка финиша
                    yf = 0
                    xf = x1
                    Exit Do
                End If
                y1 -= 1
                klet(y1, x1) = 1
            Next

            If Int(4 * Rnd() + 1) = 1 Then 'идём вправо
                UpCount = Int(((x1 - 1) + 1) * Rnd())
                For i = 1 To UpCount
                    If x1 - 1 >= 0 Then x1 -= 1
                    klet(y1, x1) = 1
                Next

            Else 'идём влево
                UpCount = Int(((8 - x1 + 1) + 1) * Rnd())
                For i = 1 To UpCount
                    If x1 + 1 <= Count Then x1 += 1
                    klet(y1, x1) = 1
                Next
            End If
        Loop

Obstacle generation:
For i = 0 To Count
            For j = 0 To Count
                If ((Rnd() * 9) > 4) And klet(i, j) <> 1 And klet(i, j) <> 2 And klet(i, j) <> 3 Then klet(i, j) = 5
            Next
        Next

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question