Reputation: 11
As part of a University exercise, we are being taken thru Python as in introductory programming language. I already know how to program to a reasonable standard, so I have been told that if I can present something of good quality, I can skip a bit of the class.
To that extent, I have taken a C version of the text game Star Trek (http://www.dunnington.u-net.com/public/startrek/startrek.c) and rewritten it in Python, which went well enough, when I got used to the specific requirements of Python.
My problem is something that seems to be a common one on Stackoverflow, which is the creation and use of a usable multidimensional array. After much searching, I have gotten the hang of creating multidimensional lists of integers or strings, etc, but when it comes to a multidimensional array (or list of lists, to be precise) of classes, all hell breaks loose.
Here is my class:
class quadrant:
klingonCount = 0
baseHere = 0
starCount = 0
And here is one of many attempts at creating a list of lists of classes:
quadrants = [quadrant] * 9
for y1 in range(1,9):
quadrants[y1] = [quadrant] * 9
This was copied from http://docs.python.org/2/faq/programming#how-do-i-create-a-multidimensional-list
As soon as I substituted a class for 0, printing the array showed that the entire array was referencing one class, not many different ones, as I had hoped.
The problem is that the array only holds the values entered during the last iteration of the loop filling it out, not the individual ones.
I'm not 100% familiar with the use of classes, so it may be that I'm just trying to reference one class many times, and I actually need many classes. Do I? Help!
I'm using Python 2.7, which is the recommended language.
For references sake, this is the code that attempts to populate the multidimensional list:
for x in range(1,9):
for y in range(1,9):
j = randint(1,99)
if (j < 5):
numBases += 1
baseHere = 1
temp = getKlingons(difficulty)
klingonsHere += temp
quadrants[x][y].baseHere = baseHere
quadrants[x][y].klingonCount = temp
s = randint(1,9)
quadrants[x][y].starCount = s
Upvotes: 0
Views: 7903
Reputation: 14901
Your mistake is in referring to a class, instead of an instance of that class.
x = quadrant
print x
...will return something like <class '__main__.quadrant'>
, because it refers to the class, not an object of that class.
This is how you make an object of that class:
x = quadrant()
print x
.. which will print something like <__main__.quadrant object at 0x0000000003263828>
Try this:
quadrants = [[quadrant() for i in range(0,9)] for j in range(0,9)]
Also, maybe consider a simpler program? Cymons's Games has a bunch of much simpler programs to demonstrate your understanding of Python:
http://cymonsgames.com/category/programs/
Upvotes: 0
Reputation: 35891
The problems comes from the fact, that you're created a class with class attributes, not with instance attributes. Later, during your iteration you create instance attributes not really having anything to do with the class attributes. Which doesn't make sense really.
Your class should look like this:
class Quadrant(object):
def __init__(self):
self.klingonCount = 0
self.baseHere = 0
self.starCount = 0
And you can create the array like this:
quadrants = [[Quadrant() for i in range(8)] for j in range(8)]
In your solution all lists in one row are containing the same object. So in each iteration you are modifying the same 3 properties.
Also note, that lists have 0-based index, so you should iterate like this:
for x in range(9):
for y in range(9):
#...
Upvotes: 2