Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random maze map. math help
#1
[Image: map.png]

HELLO... i am making a maze. i want to make this code smaller but want it to work exactly the same... i am not good with maths. any help would be good. but i want it to do the same thing it does when running. i included my code. it loops to show the result of the random map.
Code: (Select All)
DIM SCREENRAM(28, 24), SCREENROM(28, 24): FOR VERT = 1 TO 24: FOR HORI = 1 TO 28: READ SCREENROM(HORI, VERT): NEXT: NEXT
RANDOMIZE TIMER
' --------------------------------------------------------------------------------------------------------------------------------
COLOR 1
' COPY ROM TO RAM
FOR I = 1 TO 8
    FOR VERT = 1 TO 24
        FOR HORI = 1 TO 28
            SCREENRAM(HORI, VERT) = SCREENROM(HORI, VERT)
            LOCATE HORI, VERT
            PRINT CHR$(SCREENRAM(HORI, VERT) + 177);
        NEXT
    NEXT
    'RANDOM VERT
    FOR VERT = 4 TO 20 STEP 4
        FOR HORI = 4 TO 24 STEP 2
            IF INT(RND * 8) = 1 THEN
                SCREENRAM(HORI, VERT) = 1: SCREENRAM(HORI + 1, VERT) = 1
                SCREENRAM(HORI, VERT + 1) = 1: SCREENRAM(HORI + 1, VERT + 1) = 1
            END IF
        NEXT
    NEXT
    ' RANDOM HORI
    FOR VERT = 4 TO 20 STEP 2
        FOR HORI = 4 TO 24 STEP 4
            IF INT(RND * 8) = 1 THEN
                SCREENRAM(HORI, VERT) = 1: SCREENRAM(HORI + 1, VERT) = 1
                SCREENRAM(HORI, VERT + 1) = 1: SCREENRAM(HORI + 1, VERT + 1) = 1
            END IF
        NEXT
    NEXT
    'DISPLAY MAP
    FOR VERT = 1 TO 24
        FOR HORI = 1 TO 28
            LOCATE HORI, VERT
            IF SCREENRAM(HORI, VERT) = 1 THEN PRINT "Û"; ELSE PRINT " ";
        NEXT
    NEXT
    'DO AGAIN
    DO
    LOOP WHILE INKEY$ = ""
NEXT I


' --------------------------------------------------------------------------------------------------------------------------------

DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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0,0,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DATA 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
DATA 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Reply
#2
Code: (Select All)
Dim SCREENRAM(28, 24), SCREENROM(28, 24): For VERT = 1 To 24: For HORI = 1 To 28: Read SCREENROM(HORI, VERT): Next: Next
Randomize Timer
' --------------------------------------------------------------------------------------------------------------------------------
Color 1
For I = 1 To 8 ' COPY ROM TO RAM
    For VERT = 1 To 24
        For HORI = 1 To 28
            SCREENRAM(HORI, VERT) = SCREENROM(HORI, VERT)
    Next HORI, VERT
    For VERT = 4 To 20 Step 2 'RANDOM FILL
        For HORI = 4 To 24 Step (VERT Mod 4) + 2
            If Int(Rnd * 8) = 1 Then
                SCREENRAM(HORI, VERT) = 1: SCREENRAM(HORI + 1, VERT) = 1
                SCREENRAM(HORI, VERT + 1) = 1: SCREENRAM(HORI + 1, VERT + 1) = 1
            End If
    Next HORI, VERT
    For VERT = 1 To 24 'DISPLAY MAP
        For HORI = 1 To 28
            Locate HORI, VERT
            If SCREENRAM(HORI, VERT) = 1 Then Print "Û"; Else Print " ";
    Next HORI, VERT
    Sleep 'PAUSE AND DO AGAIN
Next I

' --------------------------------------------------------------------------------------------------------------------------------

Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

How's something like this look?  Seems to give equivalent results to me and it's reduced the code a bit.   Honestly, if it's doing what you want it to do, as is, then I wouldn't worry about trying to shorten the code.  Less code is NOT always better code.  Write what works.  <<-- That's my advice.
Reply
#3
can you explain how the step (vert mod 4) +2 works

For HORI = 4 To 24 Step (VERT Mod 4) + 2

this is cool 
Code: (Select All)
FOR VERT = 4 TO 20 STEP 4
        FOR HORI = 4 TO 24 STEP 2
            IF INT(RND * 8) = 1 THEN
                SCREENRAM(HORI, VERT) = 1: SCREENRAM(HORI + 1, VERT) = 1
                SCREENRAM(HORI, VERT + 1) = 1: SCREENRAM(HORI + 1, VERT + 1) = 1
            END IF
        NEXT
    NEXT
    ' RANDOM HORI
    FOR VERT = 4 TO 20 STEP 2
        FOR HORI = 4 TO 24 STEP 4
            IF INT(RND * 8) = 1 THEN
                SCREENRAM(HORI, VERT) = 1: SCREENRAM(HORI + 1, VERT) = 1
                SCREENRAM(HORI, VERT + 1) = 1: SCREENRAM(HORI + 1, VERT + 1) = 1
            END IF
        NEXT
    NEXT


TO>>>


For VERT = 4 To 20 Step 2 'RANDOM FILL
        For HORI = 4 To 24 Step (VERT Mod 4) + 2
            If Int(Rnd * 8) = 1 Then
                SCREENRAM(HORI, VERT) = 1: SCREENRAM(HORI + 1, VERT) = 1
                SCREENRAM(HORI, VERT + 1) = 1: SCREENRAM(HORI + 1, VERT + 1) = 1
            End If
    Next HORI, VERT
thank you... SMcNeill
Reply
#4
Sure!  Let's take a moment to compare your two loops.  These are the only things that are different in those loops:


    FOR VERT = 4 TO 20 STEP 4
        FOR HORI = 4 TO 24 STEP 2

    FOR VERT = 4 TO 20 STEP 2
        FOR HORI = 4 TO 24 STEP 4

Everything else you have is exactly the same, except the STEP statements get reversed.  What I did was take a moment and ask myself, "Is there some way I can make this work, and similar results while simplifying it?"

In the end, what we have here is a loop with VERT which counts by two.    Basically it breaks down to this little bit of logic:

Code: (Select All)
FOR VERT = 4 TO 20 STEP 2
   IF VERT MOD 4 = 0 THEN  'IF VERT is 4, 8, 12, 16, 20, 24  (in other words, it is on STEP 4
        FOR HORI = 4 TO 24 STEP 2  'Then we do the HORI counter by counting by 2
   ELSE  'otherwise VERT is 6, 10, 14, 18, 22 (It's the STEP 2 values)
        FOR HORI = 4 TO 24 STEP 4 'THEN we do the HORI counter by counting by 4

This is very similar to the logic which you had originally, and it's now wrapped up in one main VERT loop.

But, let's take that logic a step further and see if we can simplify it even more.

        For HORI = 4 To 24 Step (VERT Mod 4) + 2  <-- Let's look at this line.

VERT is going to be these values, counting by 2:  {4, 6, 8, 10, 12, 14, 16, 18, 20}
If we do the simple math on them, what we get is (VERT MOD 4) + 2
When VERT is 4, VERT mod 4 = 0...  0 + 2 = 2.  The STEP on HORI is 2.
When VERT is 6, VERT mod 4 = 2...  2 + 2 = 4.  The STEP on HORI is 4.
When VERT is 8, VERT mod 4 = 0...  0 + 2 = 2.  The STEP on HORI is 2.
When VERT is 10, VERT mod 4 = 2...  2 + 2 = 4.  The STEP on HORI is 4.

See the same pattern emerging as what I layed out in the IF statements previously?
IF VERT is {4, 8, 12, 16, 20), then we do the HORI step of 2.
IF VERT is {6, 10, 14, 18} then we do the HORI step of 4.

It eliminates the need for any IF statement or repeated logic and calculates the STEP, toggling between 2 and 4. 

In the end, it's my attempt to bring the two routines together and into a single loop to reduce redundant code.

Another easy solution here would be to make this into a SUB and call it, since the process is the same and just the two STEP values are basically different.

Take a look at this alternative:

Code: (Select All)
Dim Shared SCREENRAM(28, 24), SCREENROM(28, 24): For VERT = 1 To 24: For HORI = 1 To 28: Read SCREENROM(HORI, VERT): Next: Next
Randomize Timer
' --------------------------------------------------------------------------------------------------------------------------------
Color 1
For I = 1 To 8 ' COPY ROM TO RAM
    For VERT = 1 To 24
        For HORI = 1 To 28
            SCREENRAM(HORI, VERT) = SCREENROM(HORI, VERT)
    Next HORI, VERT
    Rand 2, 4 'RANDOM VERT
    Rand 4, 2 ' RANDOM HORI
    For VERT = 1 To 24 'DISPLAY MAP
        For HORI = 1 To 28
            Locate HORI, VERT
            If SCREENRAM(HORI, VERT) = 1 Then Print "Û"; Else Print " ";
    Next HORI, VERT
    Sleep 'DO AGAIN
Next I

Sub Rand (Step_Value1, Step_Value2)
    For VERT = 4 To 20 Step Step_Value1
        For HORI = 4 To 24 Step Step_Value2
            If Int(Rnd * 8) = 1 Then
                SCREENRAM(HORI, VERT) = 1: SCREENRAM(HORI + 1, VERT) = 1
                SCREENRAM(HORI, VERT + 1) = 1: SCREENRAM(HORI + 1, VERT + 1) = 1
            End If
    Next HORI, VERT
End Sub

' --------------------------------------------------------------------------------------------------------------------------------

Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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of coding better than the previous.  It's not as brief and short, but it's easier logic and not something which would take time and effort to sort out what it's doing.  You don't need to ask, "Can you explain how this works?"

Today, I can explain the logic behind the (VERT MOD 4) + 2.  In a years time, though??  It may be one of those pieces of code where I'd have to sit down and cuss myself for "being too damn clever for my own good", as I end up asking that same question.  It's not IMMEDIATELY obvious what's going on, takes a bit of thinking and logic to sort out the logic behind it, and even though it's LESS code, it's going to be much harder to maintain and share with other people.

Less code is NOT always *BETTER* code.

You need to write code that you understand, can upkeep, and modify/expand if you ever need to.  Exercises like, "How much can I reduce this code by?" are nice as LOGIC exercises to help you run your brain in different directions, but I don't recommend them for regular coding projects.  It's always nice to say, "How the hell can I think out of the box and make this as tiny as possible?"  It's just not always nice to try and maintain, build upon, or upkeep any code that was designed with that type of minimalistic logic behind it.

Keep your code simple and organized so you can maintain it.  Shortening it comes with time and practice, and the occasional exercise like this where you try and think outside the box and analyze what you're doing and look for a way to streamline the process.  But if you need to ask "How the hell does this work?", you can bet that other people are going to be asking the same question in the future, if they ever take a fresh look at that code -- yourself included.

Don't get so clever that you can't maintain your own code.  Some days, we're on the top of our game and can write some phenomenally impressive shit.  Other days, we might have the shits and then barely be able to code "Hello World".  Try to keep your stuff simple enough so you can work with it on your *average* days, without needing three cups of coffee and a whole bottle of Aspirin to sort out what the heck you did on your brilliant day. 

LOL!  That's Steve's Advice For the Day(tm)!!
Reply
#5
(08-10-2025, 07:28 AM)SMcNeill Wrote: Sure!  Let's take a moment to compare your two loops.  These are the only things that are different in those loops:


    FOR VERT = 4 TO 20 STEP 4
        FOR HORI = 4 TO 24 STEP 2

    FOR VERT = 4 TO 20 STEP 2
        FOR HORI = 4 TO 24 STEP 4

Everything else you have is exactly the same, except the STEP statements get reversed.  What I did was take a moment and ask myself, "Is there some way I can make this work, and similar results while simplifying it?"

In the end, what we have here is a loop with VERT which counts by two.    Basically it breaks down to this little bit of logic:

Code: (Select All)
FOR VERT = 4 TO 20 STEP 2
   IF VERT MOD 4 = 0 THEN  'IF VERT is 4, 8, 12, 16, 20, 24  (in other words, it is on STEP 4
        FOR HORI = 4 TO 24 STEP 2  'Then we do the HORI counter by counting by 2
   ELSE  'otherwise VERT is 6, 10, 14, 18, 22 (It's the STEP 2 values)
        FOR HORI = 4 TO 24 STEP 4 'THEN we do the HORI counter by counting by 4

This is very similar to the logic which you had originally, and it's now wrapped up in one main VERT loop.

But, let's take that logic a step further and see if we can simplify it even more.

        For HORI = 4 To 24 Step (VERT Mod 4) + 2  <-- Let's look at this line.

VERT is going to be these values, counting by 2:  {4, 6, 8, 10, 12, 14, 16, 18, 20}
If we do the simple math on them, what we get is (VERT MOD 4) + 2
When VERT is 4, VERT mod 4 = 0...  0 + 2 = 2.  The STEP on HORI is 2.
When VERT is 6, VERT mod 4 = 2...  2 + 2 = 4.  The STEP on HORI is 4.
When VERT is 8, VERT mod 4 = 0...  0 + 2 = 2.  The STEP on HORI is 2.
When VERT is 10, VERT mod 4 = 2...  2 + 2 = 4.  The STEP on HORI is 4.

See the same pattern emerging as what I layed out in the IF statements previously?
IF VERT is {4, 8, 12, 16, 20), then we do the HORI step of 2.
IF VERT is {6, 10, 14, 18} then we do the HORI step of 4.

It eliminates the need for any IF statement or repeated logic and calculates the STEP, toggling between 2 and 4. 

In the end, it's my attempt to bring the two routines together and into a single loop to reduce redundant code.

Another easy solution here would be to make this into a SUB and call it, since the process is the same and just the two STEP values are basically different.

Take a look at this alternative:

Code: (Select All)
Dim Shared SCREENRAM(28, 24), SCREENROM(28, 24): For VERT = 1 To 24: For HORI = 1 To 28: Read SCREENROM(HORI, VERT): Next: Next
Randomize Timer
' --------------------------------------------------------------------------------------------------------------------------------
Color 1
For I = 1 To 8 ' COPY ROM TO RAM
    For VERT = 1 To 24
        For HORI = 1 To 28
            SCREENRAM(HORI, VERT) = SCREENROM(HORI, VERT)
    Next HORI, VERT
    Rand 2, 4 'RANDOM VERT
    Rand 4, 2 ' RANDOM HORI
    For VERT = 1 To 24 'DISPLAY MAP
        For HORI = 1 To 28
            Locate HORI, VERT
            If SCREENRAM(HORI, VERT) = 1 Then Print "Û"; Else Print " ";
    Next HORI, VERT
    Sleep 'DO AGAIN
Next I

Sub Rand (Step_Value1, Step_Value2)
    For VERT = 4 To 20 Step Step_Value1
        For HORI = 4 To 24 Step Step_Value2
            If Int(Rnd * 8) = 1 Then
                SCREENRAM(HORI, VERT) = 1: SCREENRAM(HORI + 1, VERT) = 1
                SCREENRAM(HORI, VERT + 1) = 1: SCREENRAM(HORI + 1, VERT + 1) = 1
            End If
    Next HORI, VERT
End Sub

' --------------------------------------------------------------------------------------------------------------------------------

Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

It's a few more lines of code, but I personally like this style of coding better than the previous.  It's not as brief and short, but it's easier logic and not something which would take time and effort to sort out what it's doing.  You don't need to ask, "Can you explain how this works?"

Today, I can explain the logic behind the (VERT MOD 4) + 2.  In a years time, though??  It may be one of those pieces of code where I'd have to sit down and cuss myself for "being too damn clever for my own good", as I end up asking that same question.  It's not IMMEDIATELY obvious what's going on, takes a bit of thinking and logic to sort out the logic behind it, and even though it's LESS code, it's going to be much harder to maintain and share with other people.

Less code is NOT always *BETTER* code.

You need to write code that you understand, can upkeep, and modify/expand if you ever need to.  Exercises like, "How much can I reduce this code by?" are nice as LOGIC exercises to help you run your brain in different directions, but I don't recommend them for regular coding projects.  It's always nice to say, "How the hell can I think out of the box and make this as tiny as possible?"  It's just not always nice to try and maintain, build upon, or upkeep any code that was designed with that type of minimalistic logic behind it.

Keep your code simple and organized so you can maintain it.  Shortening it comes with time and practice, and the occasional exercise like this where you try and think outside the box and analyze what you're doing and look for a way to streamline the process.  But if you need to ask "How the hell does this work?", you can bet that other people are going to be asking the same question in the future, if they ever take a fresh look at that code -- yourself included.

Don't get so clever that you can't maintain your own code.  Some days, we're on the top of our game and can write some phenomenally impressive shit.  Other days, we might have the shits and then barely be able to code "Hello World".  Try to keep your stuff simple enough so you can work with it on your *average* days, without needing three cups of coffee and a whole bottle of Aspirin to sort out what the heck you did on your brilliant day. 

LOL!  That's Steve's Advice For the Day(tm)!!
Thank you Steve.... this has been very interesting for myself...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  More info about Random Access files PhilOfPerth 28 830 02-25-2026, 01:50 PM
Last Post: ahenry3068
  wireframe maze anyone? James D Jarvis 3 457 08-10-2025, 04:52 PM
Last Post: Unseen Machine
  Random Number Generator pmackay 14 1,231 07-30-2025, 12:56 PM
Last Post: SMcNeill
  generating a random number in the full range of that number? (Integer, Long) madscijr 2 641 05-01-2025, 09:11 PM
Last Post: madscijr
  Can't attach random-access file PhilOfPerth 7 1,345 08-29-2024, 10:28 AM
Last Post: PhilOfPerth

Forum Jump:


Users browsing this thread: