I need to have a 2 radius map drawn off the player's current room in a MUD I'm building in python (or more, if possible). Rooms are set up as containers with a self.exits = {'west':1, 'north':2}
where the key is the direction that the value (UID of the adjacent room) is located. Rooms are linked only in this way. A player with a self.location of 0 could type 'n' and their location, based on the above variable, would then be 2 and that room's contents would have the player's UID appended to its contents.
So, I would like to have a map displayed that looks like the following, based on the above variable, where 'u' is the player's current location..
[ ]
|
[ ]-[u]
I've achieved this portion, as this is just a radius of 1. Here is a small (heavily modified for posting here) snippet of how I did this, and you'll see why I'm posting, as it's poor code.
mloc = '[u]'
mn = ' '
mw = ' '
spn= ' '
spw= ' '
for Exit in room.exits.keys():
if Exit == 'north':
mn = '[ ]'
spn = '|'
if Exit == 'west':
mw = '[ ]-'
# player.hear() is our function for printing a line to the player's screen
player.hear(' '+mn)
player.hear(' '+sp)
player.hear(mw+mloc)
In my insanity, I managed to make this work with all 8 different directions (diagonals, and not including up or down). But I then have to for loop the rooms I just parsed with my first for loop, and then draw those, and then space it all out, and then take into account the overlap of the (sp)aces such as '\' or '|' if there are paths that cross each other. This small task turned nightmarish immediately, and well into 200 lines before I was done.
Another hurdle is that I can only print line by line. So if the map is 50 characters high, I have to have player.hear()
on 50 lines, which I'm not opposed to. Just keep that in mind before posting an answer.
I'm also not picky about formatting. I just simply want a 'map at a glance' to aid players while traveling around the world.
Thanks guys. I hope I provided enough info. Let me know, if not. (Here is a link to the entire (unfinished and HORRIBLE) module I'm referencing. Map.py
This code is in serious trouble. Let's start designing from scratch. This will hopefully serve as a good lesson in how to design and build classes and data structures.
To start with, you should organize your code around a
Map
class, which then represents your rooms as a grid. You shouldn't think about "room 1", "room 2", etc (which is very hard to keep track of on a map), and rather think of rooms in terms of coordinates.Now, there are a few possible features we are ignoring at the beginning, including the player seeing only rooms he's been to, the player remaining at the center of the map, and diagonal paths. If you want them you can put them in later, once the basic functionality works. For now, we are aiming for something that looks a little like this:
That is, we're representing it as a grid where some rooms are connected and others are not. Let's have each room have a coordinate pair, a little like this:
Let x be along the top and y be along the side. The top left is (0, 0), the one with
[u]
in it is (0, 1).Now, what are the components of our
Map
class?map height: integer
map width: integer)
player_x, player_y: coordinates of player
possible paths: a list of pairs of rooms that we can move between. The above map would be represented as:
Notice that I ordered each pair such that the larger tuple goes first (that's important later).
So now that we have our design, let's write that
Map
class!I can think of four methods we want:
print_map
,move
, and an initializer. Intitialization is simple: just set the four attributes we listed above:Now,
move
is quite simple. Given a direction n/e/s/w:The
move
function for "north" just checks if there is a path to the room above the one we are in.Now for the most interesting part: printing the map. You do this by looping over the rows (0 to
self.height
) and over the columns (0 toself.width
). (Note: you can't useprint
in this situation since it automatically puts a newline or space after the string. Instead we use sys.stdout.write.Now, let's put it all together and try it out. Here's the code:
Notice that I added a section on the bottom that creates a map and allows the player to move around it. Here's how it looks when it runs:
There are many improvements that can be made to this code (in particular, the
move
method is repetitive), but it's a good start. Try making the map 20x20 and you'll see it expands just fine.ETA: I should note that
print_map
could be rewritten in a much shorter form as something like:But this is a bit more intense.