Computer Science question (python list is slow with my cruddy algorithm )
Jay O'Connor
joconnor at cybermesa.com
Thu Aug 22 20:35:51 EDT 2002
In article <pan.2002.08.23.06.53.02.409701.2520 at charter.net>, "Mr.
Neutron" <nicktsocanos at charter.net> wrote:
> What I am trying to do is build a game. It is a very simple game it is
> not fancy like Quake or anything like that. I have a World structure
> that contains squares. These squares are just an area of the world (1
> mile x 1 mile) that have interesting features on them. I wanted to make
> a big world that would be interesting. I can play the game in a small
> world ( 64x64 squares ). That is ok and fast. But I wanted the idea of
> huge worlds too (even though it is not necessary for the game). For my
> simple game so far, MyLIst = [ [0] * 64 for i in range(64) ] is fast. Is
> this 64x64x4 = 16384 bytes in Python memory?
>
> Now that that is out of the way, are there any better ways to represent
> the world than a list of lists? I just need to be able to say
> World[Y][X] = ( values ). Or be able to say what is at World[Position].
> Ideally I could say World[ (X,Y) ] = (Values) but I have not tried this.
> If World[ (X,Y) ] is empty, than it does not need to store anything in
> memory at all. I need to go to Idle now and experiment with this.
Would it work to use some sort of Flywheel pattern? Instead of having a
two dimensional list of world coordinates with each space having (or not)
a set of features, have a dictionary where the key is a feature and the
value is a list of tuples representing the world coordinate
worldmap = {
"ore": [(1,2), (1,3)...],
"food": [(10,11), (12,13)...]
...}
then to know what features are in a give location you do a reverse lookup
location = (x,y)
featuresInLocation = []
for each in worldmap.keys():
if location in worldmap (each):
featuresInLocation.append (each)
When this is done, featuresInLocation will hold "ore", "food" etc...for
(x,y)
>From here, it may be possible to use a RunArray of some sort to further
reduce memory for contigious locations that contain a given feature
--
Jay O'Connor
joconnor at cybermesa.com
http://www.r4h.org/r4hsoftware
More information about the Python-list
mailing list