In LittleViking's map generation thread, Life247 thought that it was supposed to generate small maps. So I decided I would take a stab at making a 3x3 map generator. The maps contain a randomly placed ninja, exit, mine, and either a gauss or homing turret. I'm sure that the code could be shorter, but I personally don't know what could be trimmed. The code is largely stolen from LV, so credit to him.
If my math is correct, then there's a 13% chance of instantly dieing, and a 4.542% chance of instantly winning. That means that there is an 82.458% of getting a playable map, leaving 16,491,600,000 different playable maps. Unfortunately, the gameplay is basically the same for all of them.
Code: Select all
import random as r;n,c,x,y,t,z=range,r.choice,n(366,426,6),n(270,330,6),['1' for i in n(713)]+['|5^%s,%s!11^%s,%s,%s,%s!%s^%s,%s!12^%s,%s'%(c(x),c(y),c(x),c(y),c(x),c(y),c([3,10]),c(x),c(y),c(x),c(y))],['0','0','0'];t[332:334]=z;t[355:357]=z;t[378:381]=z;print''.join(t)
BREAKDOWN:
Code: Select all
import random as r
n = range
c = r.choice
This imports the random function, and renames a few functions for brevity. I'll explain these in more detail later.
Remember that the range function was renamed n earlier. What it does is creates a list of numbers between the two numbers given. The six at the end means that it skips to every sixth number. To clarify, "x "is now a list, containing the numbers 366, 372, 378, 384, 390, 396, 402, 408, 414, and 420. These lists will later become the coordinates of the items.
Code: Select all
t = ['1' for i in n(713)] +
['|5^%s,%s!11^%s,%s,%s,%s!%s^%s,%s!12^%s, %s' % (c(x), c(y), c(x), c(y), c(x), c(y), c([3,10]), c(x), c(y), c(x), c(y))]
The first line here adds "1" to the end of t (which starts off as a blank list) 713 times. This creates a tileset made entirely of "E" tiles. The second line is more complicated.
Code: Select all
['|5^%s,%s!11^%s,%s,%s,%s!%s^%s,%s!12^%s, %s']
This bit is n item data, with "%s" instead of coordinates. The five is the ninja, the eleven is the exit, the third one is either a homing or gauss turret (more on that in a second), and twelve is a mine.
Code: Select all
% (c(x), c(y), c(x), c(y), c(x), c(y), c([3,10]), c(x), c(y), c(x), c(y))
Back at the beginning, the function "random.choice" was renamed "c". What it does is randomly selects one item from a list. "x" and "y" are two lists of numbers (made earlier) that are the possible coordinates of the items. The numbers are random chosen from each list and placed into the previous item data. ( "c([3,10])" randomly chooses a three or a ten, which then make the only undefined item ("%s^%s,%s") either a gauss or homing turret (three is gauss, ten is homing).
This makes a list, "z", that consists of three objects, each of them zero.
Code: Select all
t[332:334] = z
t[355:357] = z
t[378:381] = z
This section kills me. I just know that there's a shorter way to do this, but I don't know what it is. This part replaces certain sections of the list "t" (which contains all of the map data), with another list "z" (which contains three zeros). Basically, this makes the three by three area of "D" tiles within which the map will take place.
This last part joins the list "t" (which now contains the completed map) into one long string, and prints it to the screen.
If my explanations are too confusing, try looking at LV's thread for better explanations of most of the concepts used here.