[Tutor] Location of object instance in a list
Christian Wyglendowski
cwyglendowski@greenville.edu
Wed Oct 30 23:46:02 2002
Hi everyone. Ok, here it is...my first real question for the list.
I am trying to learn about object oriented design with python. I have =
decided to make a computer based boardgame or something like one. So far, =
I have a class for the board that looks like this (very basic):
class Board:
def __init__(self, width=3D10, height=3D10):
self.width =3D width
self.height =3D height
self.surface =3D []
for eachY in range(self.height):
self.surface.append([])
for eachX in range(self.width):
self.surface[eachY].append([])
def __str__(self):
s =3D ''
for row in range(self.height):
for column in range(self.width):
s =3D s + str(self.surface[row][column])
if len(s) > 0: s =3D s + '\n'
return s
So I have a board that I can access with x/y coordinates...
>>> test =3D Board()
>>> test.surface[0][0].append('Cool object instance')
Now, let's say that I actually did append a 'Cool object instance' to =
test.surface[0][0] and not just a string by the same name. Is there =
anyway the said object instance can have knowledge of its place in the =
list of lists (test.surface[x][y])? If so, I can then give the objects =
the ability to move around the Board.surface[x][y] fairly easily.
I was able to create a method for Board that allowed me to move the =
contents of the [x][y] lists around, but if they are to contain object =
instances - it just seems that the instances themselves should have the =
ability to move, and not have the board pushing them around...
class GamePiece:
pass
Also, if I am heading in the completely wrong direction with all of this, =
let me know!
Thanks,
Christian