[Tutor] Re: matrixes

Lee Harr missive at hotmail.com
Wed Apr 28 17:59:06 EDT 2004


>I have to recreate a text-based game 'visually' for school and I was
>hoping you could help me with things I've run into.
>
>One of the first things I've run into is how to deal with matrixes in
>python. If I have the following matrix with 'glade' being location
>(0,0):
>
>forest        river   desert
>
>woods creak   dunes
>
>glade stream  mountains
>
>I was thinking of representing like this:
>
>matrix = (('glade', 'stream', 'mountains'),('woods', 'creak', 'dunes'),
>('forest', 'river', 'desert'))
>
>Is this the correct way and 'pythonic' way of doing it, or am I missing
>a module or something.
>


That seems reasonable. Though I might suggest formatting your tuple
like this ...

matrix = (('glade', 'stream', 'mountains'),
                ('woods', 'creak', 'dunes'),
                ('forest', 'river', 'desert'),
                )


Just so that it looks more like your sketch, and is easier to add on to.


>location = [0, 0]
>
>and then north would be:
>
>north = [0, 1]
>
>and then if the input would be north. I would do something like this:
>
>location =  [location[0] + north[0], location[1] + location[1]]
>
>The problem is if you went south for your first 'move' how would i make
>it wrap and end up at forest?
>


I'd like to see what code you are actually using, but how about
something like ...

if location[0] < 0:
    location[0] = 2

You will probably want to take in to account the fact that your
matrix of locations could change size in the future.


>This seems like a real roundabout way of doing things, is there a
>better/faster way of doing this?
>

I would probably opt for an object-oriented approach myself.
(each location would be an object instead of just a string...
they could have links to the locations around them instead of
putting them in to a matrix.)


>As for my visual part, I was thinking of having a command line prompt to
>take input and a seperate window which shows a picture depending on
>which place you would be. This picture would have to dynamically change
>and pretty rapidly. I'll do a little research before asking about it.
>

Sounds like fun. Let us know what you come up with.

_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*  
http://join.msn.com/?page=features/junkmail




More information about the Tutor mailing list