Random maze generator.

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
Shaun_B
Posts: 451
Joined: Wed Apr 22, 2009 10:22 am

Random maze generator.

Post by Shaun_B » Sun Jun 08, 2014 4:02 pm

I was thinking about a random maze generator which on the ZX Spectrum can be achieved by:

Code: Select all

10 PRINT "\/"(INT(RND*2)+1);:GO TO 10
I came up with this - more of a random pattern generator and ends up looking like a QR code actually, but anyway, here's the listing:

Code: Select all

 1 LET M$="{GRAPHICS}{SHIFT}12345678QWERTY {SHIFT OFF}{GRAPHICS OFF} "
 2 LET Z=NOT PI
 3 LET W=CODE "3"
 4 LET H=CODE "*"
 5 LET X=Z
 6 LET Y=Z
 7 POKE 16418,Z
 8 LET L=LEN M$
 9 LET G=CODE "{GRAPHICS}{SHIFT}S{SHIFT OFF}{GRAPHICS OFF}"
10 FOR X=Z TO W
11 FOR Y=Z TO H
12 PRINT M$(INT(RND*L)+SGN PI);
13 NEXT Y
14 NEXT X
15 PRINT AT Z,Z;
16 GOTO G
It opens up the bottom two rows of the screen so it will not work on a 1K machine. If you're using 1K, this version works:

Code: Select all

 1 LET M$="{GRAPHICS}{SHIFT}12345678QWERTY {SHIFT OFF}{GRAPHICS OFF} "
 2 LET Z=NOT PI
 3 LET W=CODE "4"
 4 LET H=CODE "("
 5 LET L=LEN M$
 6 LET O=SGN PI
 7 LET G=CODE "{GRAPHICS}{SHIFT}A{SHIFT OFF}{GRAPHICS OFF}"
 8 FOR X=O TO W*H
 9 PRINT M$(INT (RND*L)+O);
10 NEXT X
11 PRINT AT Z,Z;
12 GOTO G
Obviously, these listings could be further minimized, and a SCROLL could be added to the version that'll work with expanded memory.

By the way, does anyone know of a safe way to POKE to the screen, say POKE DFILE+X+Y*32,CHAR ? Things don't go well when I've tried it so far.

Regards,

Shaun.

gozzo
Posts: 448
Joined: Fri Jul 08, 2011 8:52 pm

Re: Random maze generator.

Post by gozzo » Sun Jun 08, 2014 8:34 pm

You have to be careful poking the DFILE as if you alter the 118 NEWLINE character, it could cause a crash! The first location of the DFILE is a 118 and each 33rd character after this first 118, is also, so for selecting a location you need DFILE+1+X+33*Y. And this will not work on a 1k/2k machine as the DFILE is not filled out as with a 16k and above, also SCROLL upsets this by making a collapsed bottom line, BUT I think a PRINT AT 21,31;" ";AT 21,0; after the SCROLL might 're-inflate' it, but haven't yet tested it...

Edit - can confirm that the PRINT AT line I described above DOES re-inflate the DFILE after a SCROLL correctly !
Last edited by gozzo on Sat Jun 14, 2014 6:37 pm, edited 1 time in total.

poglad
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

Re: Random maze generator.

Post by poglad » Sun Jun 08, 2014 8:40 pm

Yes, basically if you want a simple life poking the display file, make sure you're running 4K or more so that the display file is a known size.

gozzo
Posts: 448
Joined: Fri Jul 08, 2011 8:52 pm

Re: Random maze generator.

Post by gozzo » Mon Jun 09, 2014 1:03 am

If yo have a 2k machine, you can 'fill out' the display file by using...

POKE 16389,128
CLS
POKE 16389,72

and, if you want to, for 4k and higher, to collapse the DFILE

RT=PEEK 16389
POKE 16389,68
CLS
POKE 16389,RT

poglad
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

Re: Random maze generator.

Post by poglad » Mon Jun 09, 2014 11:26 am

...or execute 24 SCROLLs one after the other. Doing that before a SAVE will knock a few seconds off the loading time.

I wonder why Vickers 'cheated' with the scroll routine and made it generate short lines? It's annoying because you end up with all sorts of screen flickering next time you do a CLS, and it takes an age to do it. Same question (in a different form), I wonder why he switched to SLOW mode on start-up before generating the display file? If he had stayed in FAST a bit longer, the system would have booted almost instantly.

poglad
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

Re: Random maze generator.

Post by poglad » Mon Jun 09, 2014 11:31 am

Back on topic, the Maze Generator... Here is a Maze Generator for the ZX80, written by... Nigel Searle. Remember him?

10 PRINT CHR$(RND(3));
20 GO TO 10

It appeared in Sync magazine back in 1980:
https://archive.org/stream/syncmagazine ... 2/mode/1up

Shaun_B
Posts: 451
Joined: Wed Apr 22, 2009 10:22 am

Re: Random maze generator.

Post by Shaun_B » Tue Jun 10, 2014 12:24 am

Good one poglad - here's a ZX81 listing that does almost the same as my examples above:

Code: Select all

1 PRINT CHR$ (RND*CODE"{GRAPHICS}{SHIFT}E{SHIFT OFF}{GRAPHICS OFF}");
2 GOTO SGN PI
Regards,

Shaun.

Shaun_B
Posts: 451
Joined: Wed Apr 22, 2009 10:22 am

Re: Random maze generator.

Post by Shaun_B » Mon Jan 07, 2019 9:52 pm

Here's an infinitely scrolling random maze, requires 2K or more
random-maze.png
random-maze.png (1.87 KiB) Viewed 1074 times
For a 1K ZX81, change line 4 to:

Code: Select all

   4 FOR N=SGN PI TO CODE "3"
Regards,

Shaun.
Attachments
m.p
For those who don't want to type
(213 Bytes) Downloaded 22 times

dr beep
Posts: 1156
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Random maze generator.

Post by dr beep » Tue Jan 08, 2019 5:42 pm

With the display method of HIDDEN PICTURE I think I can code a hidden 2D maze on the screen in the same size that will show fields you visited like a map and you must find a random exit.

This would fit 1K in lowres.

Shaun_B
Posts: 451
Joined: Wed Apr 22, 2009 10:22 am

Re: Random maze generator.

Post by Shaun_B » Thu Jan 10, 2019 10:08 pm

You can save more BASIC bytes like this:

Change line 7 to:

Code: Select all

   7 CONT
Then add in the following line:

Code: Select all

   2 STOP
Now RUN the program, remove line 2 and then enter the CONT command. Hey presto, another BASIC token saved!

Regards,

Shaun.

Post Reply