{"assets":[],"author_link":"author\/zanzlanz\/","author_name":"Zanzlanz","cat":"LD #31","categories":["LD #31"],"comments":[],"epoch":1418550660,"event":"LD31","likes":13,"metadata":{"p_key":"83233","p_author":"Zanzlanz","p_authorkey":"0","p_urlkey":"293971","p_title":"Randomized puzzle generator \u2013 How it works!","p_cat":"LD #31","p_event":"LD31","p_time":"1418550660","p_likes":"13","p_comments":"0","p_status":"WAYBACK","us_key":null,"us_name":null,"us_username":null,"event_start":"1417737600","event_key":"26","event_name":"LD31"},"source_url":"2014\/12\/14\/puzzle-2\/","text":"<p>Hey again!<\/p>\n<p>Recently I\u2019ve made a post \u2013 <a href=\"http:\/\/ludumdare.com\/compo\/2014\/12\/06\/puzzle\" target=\"_blank\" title=\"previous post about my generator\">http:\/\/ludumdare.com\/compo\/2014\/12\/06\/puzzle<\/a> \u2013 where I show off progress on the random puzzle generator I created for LD31. A few of you were interested in how I created it, so here I am to explain it!<\/p>\n<p>First of all, here\u2019s the newest gif of me playing the first couple randomly generated levels, and an image of one of the higher levels:<\/p>\n<p>\u00a0<\/p>\n<p><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/12\/Sometimes-things-get-complicated-dundundun.png\"><img alt=\"Puzzle generator with coins!\" class=\"wp-image-427231\" height=\"258\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/12\/puzzle-gen.gif\" width=\"308\"\/><\/a><a href=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/12\/Sometimes-things-get-complicated-dundundun.png\"><img alt=\"Sometimes things get complicated dundundun\" class=\"alignnone wp-image-427234\" height=\"262\" sizes=\"(max-width: 302px) 100vw, 302px\" src=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/12\/Sometimes-things-get-complicated-dundundun.png\" srcset=\"http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/12\/Sometimes-things-get-complicated-dundundun-300x260.png 300w, http:\/\/ludumdare.com\/compo\/wp-content\/uploads\/2014\/12\/Sometimes-things-get-complicated-dundundun.png 536w\" width=\"302\"\/><\/a><\/p>\n<p>\u00a0<\/p>\n<p>If you want to try it out yourself, here\u2019s my submission:<br\/>\n<a href=\"http:\/\/ludumdare.com\/compo\/ludum-dare-31\/?action=preview&amp;uid=11407\" target=\"_blank\" title=\"Play Snowman Slide\">http:\/\/ludumdare.com\/compo\/ludum-dare-31\/?action=preview&amp;uid=11407<\/a><\/p>\n<p>\u00a0<\/p>\n<p>The idea is simple:<\/p>\n<p><em>Create a random level generator that provides the player with a challenging, yet always solvable, sliding puzzle.<\/em><\/p>\n<p>\u00a0<\/p>\n<p><strong>Here\u2019s the trick!<\/strong><\/p>\n<p>I programmed a simple noise generator (loops through all tiles on the screen and sets them to a random tile). Then I made a pathfinding algorithm that starts at the spawn point and finds all possible moves. It\u2019s kind of like A*, where if a move gets you to a spot quicker than a different move, it over-writes it. Then I simply determine which spot took the highest number of moves to get to, and place the end there! If the spot with the highest number of moves isn\u2019t \u201chard\u201d enough, I generate another level from scratch and try again, until the level meets a certain difficulty.<\/p>\n<p><strong>The water.<\/strong><\/p>\n<p>You die when you touch the water. That was pretty easy to put into the generator. Basically I tell it the same thing as I do to the snowman: if there is water along the path, you can\u2019t go down that path (restart the player). The generator has no problem dealing with that.<\/p>\n<p><strong>The coins\u2026 oh the coins.<\/strong><\/p>\n<p>I really wanted to give the player something to collect as an added challenge, as they solve the puzzle. The goal here is not to place coins in the middle of the path to the end. That would be way too easy for the player. So basically I created another algorithm that tells me if Point A can lead to Point B (or pass through it, even). I found the best way to make coins was to check these:<\/p>\n<p>\u2013 You can pass through the coin safely from the start.<br\/>\n\u2013 Wherever you land next, you can pass through the start, AND reach the end (if you can\u2019t reach the end, it\u2019s in an unsolvable position!)<br\/>\n\u2013 If you can get from wherever you land after you pass through the start BACK to the coin (basically, if it\u2019s possible to get other coins).<\/p>\n<p>I\u2019m kind of worried that there are very specific \u201ccorner cases\u201d where this generator doesn\u2019t make every coin collectable. But I\u2019ve never seen it happen, so maybe it <em>is<\/em> flawless! <img alt=\":)\" class=\"wp-smiley\" src=\"http:\/\/ludumdare.com\/compo\/wp-includes\/images\/smilies\/simple-smile.png\" style=\"height: 1em; max-height: 1em;\"\/><\/p>\n<p><strong>How fast does it generate?<\/strong><\/p>\n<p>Fast for the small levels, and quite slow for the end levels (30 seconds some times).\u00a0 So what I did is make the generator create the rest of the levels while the player is solving the first ones! It can create all 20 of them by the time you reach level 5. Also, I did a lot of fooling around with the density of water and blocks, and found some densities are better for the generator to quickly create harder levels, quicker.<\/p>\n<p>\u00a0<\/p>\n<p>Alright! Well I hope you enjoyed the post! I also hope this inspires you to try out random generation \u2013 it\u2019s not all that hard once you get the concept down. \ud83d\ude00<\/p>\n<p>Thanks for reading,<br\/>\n\u2013 Zanz<\/p>\n<p>\u00a0<\/p>","time":"December 14th, 2014 9:51 am","title":"Randomized puzzle generator \u2013 How it works!","title_was_empty":false}