Small Generator of Small n Levels

Talk about computers, hardware, applications, and consumer electronics.
User avatar
Albany, New York
Posts: 519
Joined: 2008.09.27 (21:14)
NUMA Profile: http://nmaps.net/user/bufar
MBTI Type: ISTP

Postby bufar » 2010.01.19 (08:36)

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.

Code: Select all

x = n(366,426,6)
y = n(270,330,6)
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).

Code: Select all

z = ['0','0','0']
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.

Code: Select all

print '' .join(t)
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.

Raigan and the Horse-Woman
Raigan and the Horse-Woman
Posts: 182
Joined: 2008.09.27 (02:14)
NUMA Profile: www.nmaps.net/user/sidke
Steam: www.steamcommunity.com/id/shagdish
Location: ⑨ 
Contact:

Postby sidke » 2010.01.19 (10:09)

I tried coming up with a clever way of putting that 3x3 block in the middle, but the best I could come up with was "t='1'*332+q+'1'*20;t+=q+t[::-1]", which is 1 character longer than your specific assignment. Your one-lined code errored because you were referencing variables that wouldn't be defined until the statement was over, so you needed to split those out a bit. I didn't try messing with your object stuff ;-;

['1' for i in n(713)] can be changed to '1'*713

Full of what I got, including my 1-char over t-setter:

Code: Select all

import random as r;n,c=range,r.choice;x,y=n(366,426,6),n(270,330,6);q='0'*3;t='1'*332+q+'1'*20;t+=q+t[::-1]+'|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));print t
spoiler

辻菜摘が好きじゃー ヽ(´ー`)ノ sig by peking duck


User avatar
Albany, New York
Posts: 519
Joined: 2008.09.27 (21:14)
NUMA Profile: http://nmaps.net/user/bufar
MBTI Type: ISTP

Postby bufar » 2010.01.19 (18:08)

The one-line code doesn't error for me, but I now see why it should be.
Your tileset generator is shorter than mine, I think. "t[332:334]=z;t[355:357]=z;t[378:381]=z" is 38 characters, while "t='1'*332+q+'1'*20;t+=q+t[::-1]" is only 31.


Who is online

Users browsing this forum: No registered users and 13 guests