Maze Generation code

Monkey Forums/Monkey Code/Maze Generation code

Grey Alien(Posted 2013) [#1]
I added maze generation to my latest minigame and have posted up all the code in my blog: http://greyaliengames.com/blog/maze-generation-code-in-monkey/


Paul - Taiphoz(Posted 2013) [#2]
Left you a comment Jake.


therevills(Posted 2013) [#3]
Nice - I think it would be better to post the code here ;)

Also when posting code on your site, can't you use the <code> tags?

(PS The language is now called Monkey X and I think BRL wants people to use the www.monkey-x.com domain from now on... ;))


AdamRedwoods(Posted 2013) [#4]
nice!
here was mine:
http://monkeycoder.co.nz/Community/posts.php?topic=4536


impixi(Posted 2013) [#5]
Well, a quick glance at Jake's code tells me it wouldn't be runnable as is: ccRand isn't a Monkey (or a Jake-provided) function - erm, is it? EDIT: Which is probably why he did not paste the code in this thread.

PS: My own maze generation code is in the archives too. Inside a function tucked away here: http://www.monkeycoder.co.nz/Community/posts.php?topic=374&post=49039


SLotman(Posted 2013) [#6]
Why do walls are 'double sided'? (at least on the image posted, they all look like '##')
Isn't a waste of space/memory?

Also, I wouldn't use an 'ArrayUtil' - just array[x+y*width] to access the data ^_^


impixi(Posted 2013) [#7]

Also, I wouldn't use an 'ArrayUtil' - just array[x+y*width] to access the data ^_^


He's using arrays of arrays to store the maze rooms, not a single dimensional array.


impixi(Posted 2013) [#8]
Okay, here's Jake's code in a runnable example (a rough hack with fixed errors. Only works in RELEASE mode, otherwise my browser throws a syntax error on the generated JS code - that I don't have time to investigate right now):




therevills(Posted 2013) [#9]
Looks like Jake and Impixi have found a little Monkey bug. The Debug code generates the following line:
				dbg_array(t_rows,)[dbg_index]=dbg_array(t_rows,)[dbg_index]+"###"

Which is incorrect (there is a comma then a bracket). I'll post it in the Bugs forum.


SLotman(Posted 2013) [#10]
He's using arrays of arrays to store the maze rooms, not a single dimensional array.

That's the point I'm making - I'd use a 1D array, which is faster than 'array of arrays', hence using array[x+y*width] instead of 'ArrayUtils' (which I understand is only there to correctly initialize the fake 2D array).


AdamRedwoods(Posted 2013) [#11]
I'd use a 1D array, which is faster than 'array of arrays'

depends. there is almost no notable difference in most platforms, and i would not waste time on this type of optimization.
http://stackoverflow.com/questions/2512082/java-multi-dimensional-array-vs-one-dimensional
http://stackoverflow.com/questions/5476000/c-multi-dimensional-array-vs-one-dimensional


impixi(Posted 2013) [#12]
@Slotman: Personally, in Monkey, I use single dimensional arrays as you suggest, if only to eliminate the additional initialization procedure. As for the performance aspect, that's something I'm not sure about. Though it would not be too difficult to write a comparative benchmark (perhaps by modifying Jake's maze gen code?)...


Grey Alien(Posted 2014) [#13]
@therevills yeah I wasn't sure about posting it here as it wasn't runnable, just potentially usefully snippets. OK I'll try to use the new domain now (I searched for it but somehow it came up with the old co.nz domain). Is "Monkey X" the official new name for the language too? Should I use that when talking about it. Oh I didn't know <code> works in wordpress. Guess I should try that!

@SLotman The walls are only double sided on the printout as it was easier to quickly write the printing code like that. Well I guess each room has 4 walls and I found it easier to remove those as needed instead of having a separate array of just walls as well as the rooms array.

Yes I could have made the array 1D, in fact back in my assembly days 2D arrays were always 1D and using y*width+x But I like the ease of use/comprehendabililty (that's not a real word) of 2D arrays.

@impixi Wow thanks for turning it into a working example, that's great!

Also GREAT find with the bug (and thanks to @therevills for posting in bugs). I actually saw something like that when I tried to compile it as Debug for Flash (worked fine as Debug for GLFW btw). Got some kind of error regarding the 2D array but I was in a rush last night to get the game onto #1gameamonth and so just compiled as release in Flash (which worked).

Also thanks @adam and @impixi for posting your own maze gen examples too!


Jesse(Posted 2014) [#14]
here is my contribution:
http://www.monkeycoder.co.nz/Community/posts.php?topic=7723#75089


Gerry Quinn(Posted 2014) [#15]
If you're doing complicated stuff with mazes involving solving them repeatedly, in my experience the single-dimensional array will be faster, but by a linear factor. I.e. something will take twice as long, maybe even in some cases three times as long, but most of the time that will be 2 ms vs 1 ms.

In other words it's unlikely to matter.


SLotman(Posted 2014) [#16]
Well, call me insane, but I really prefer (even for readability) x+y*width :)
When a programmer uses 2D arrays, I never now if it's been used as array[x][y] or array[y][x] ^_^


Paul - Taiphoz(Posted 2014) [#17]
Call me crazy but I have an idea with all this maze talk, what if we setup a testing framework with two exposed functions, one for creating a maze and one for solving it on a frame by frame basis.

Imagine if you will a setup where each forum poster submitted his own include which had these two functions, that all these submitted includes were pulled into this little test framework and run, I have this image of tons of little coloured AI roaming around trying to solve the maze, could be really cool .

I think the only rule might be that your solver function can only use a few grid area's in diameter around its current position, possibly 5 or 6 squares around, if it could see the full grid you could just use A* or some other path finding to solve it.


just a thought.


computercoder(Posted 2014) [#18]
@Paul

That actually sounds kinda fun :) I've been dorking about remaking simple games from the old Commodore VIC-20 days and was thinking about something else to mess with. Just building upon my abilities with Monkey X and game dev in general.


Paul - Taiphoz(Posted 2014) [#19]
OK, finally had some spare time, so here is my effort, I am a very visual person so love to see things happening so the generation of the maze is timed so you can sit and watch it work itself out rather than just blanking for a second as it works in the background.

at the moment it's a straight up maze, but I have some lose code in the project folder locally for creating rooms which I am still playing with, but its looking cool so far.

There might be the odd bit of code here or there that's not being called , just ignore it its stuff I am playing with atm.

See it run Here



Paul - Taiphoz(Posted 2014) [#20]
Oh yeah ignore all that header footer crap, I have a tool that creates new projects from a template..