Page 1 of 1
Random maze generator.
Posted: Sun Jun 08, 2014 4:02 pm
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.
Re: Random maze generator.
Posted: Sun Jun 08, 2014 8:34 pm
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 !
Re: Random maze generator.
Posted: Sun Jun 08, 2014 8:40 pm
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.
Re: Random maze generator.
Posted: Mon Jun 09, 2014 1:03 am
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
Re: Random maze generator.
Posted: Mon Jun 09, 2014 11:26 am
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.
Re: Random maze generator.
Posted: Mon Jun 09, 2014 11:31 am
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
Re: Random maze generator.
Posted: Tue Jun 10, 2014 12:24 am
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.
Re: Random maze generator.
Posted: Mon Jan 07, 2019 9:52 pm
by Shaun_B
Here's an infinitely scrolling random maze, requires 2K or more
- random-maze.png (1.87 KiB) Viewed 8257 times
For a 1K ZX81, change line 4 to:
Regards,
Shaun.
Re: Random maze generator.
Posted: Tue Jan 08, 2019 5:42 pm
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.
Re: Random maze generator.
Posted: Thu Jan 10, 2019 10:08 pm
by Shaun_B
You can save more BASIC bytes like this:
Change line 7 to:
Then add in the following line:
Now RUN the program, remove line 2 and then enter the CONT command. Hey presto, another BASIC token saved!
Regards,
Shaun.