Random maze generator.

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

Random maze generator.

Post by Shaun_B »

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: 452
Joined: Fri Jul 08, 2011 8:52 pm

Re: Random maze generator.

Post by gozzo »

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 »

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: 452
Joined: Fri Jul 08, 2011 8:52 pm

Re: Random maze generator.

Post by gozzo »

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 »

...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 »

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: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: Random maze generator.

Post by Shaun_B »

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: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: Random maze generator.

Post by Shaun_B »

Here's an infinitely scrolling random maze, requires 2K or more
random-maze.png
random-maze.png (1.87 KiB) Viewed 7325 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 276 times
dr beep
Posts: 2060
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Random maze generator.

Post by dr beep »

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: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: Random maze generator.

Post by Shaun_B »

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