3

that's my problem:

self.gameField = [
    ['-', '-', '-', '-', '-', '-', '-']
    ['-', '-', '-', '-', '-', '-', '-']
    ['-', '-', '-', '-', '-', '-', '-']
    ['-', '-', '-', '-', '-', '-', '-']
    ['-', '-', '-', '-', '-', '-', '-']
    ['-', '-', '-', '-', '-', '-', '-']
]

As you can see, I've created a matrix using list, I need to change a single element of a column selected by user, so I've tried with this code:

for z in self.gameField:
    if z[num] == "-":
        z[num] = "X"
        print "Done"

Where 'num' is an integer from 0 to 6 to indicate the sub-list. I want to select a colum, and set the lowest ' - ' available (like if there's gravity), so for example, if I select '0'

        ['-', '-', '-', '-', '-', '-', '-']
        ['-', '-', '-', '-', '-', '-', '-']
        ['-', '-', '-', '-', '-', '-', '-']
        ['-', '-', '-', '-', '-', '-', '-']
        ['-', '-', '-', '-', '-', '-', '-']
        ['X', '-', '-', '-', '-', '-', '-']

But actually my program change the value of all the column

        ['X', '-', '-', '-', '-', '-', '-']
        ['X', '-', '-', '-', '-', '-', '-']
        ['X', '-', '-', '-', '-', '-', '-']
        ['X', '-', '-', '-', '-', '-', '-']
        ['X', '-', '-', '-', '-', '-', '-']
        ['X', '-', '-', '-', '-', '-', '-']

How can I fix it? Have you got any ideas to do that?

  • Create your 2d list using something like this: http://stackoverflow.com/questions/13447882/python-2-7-creating-a-multidimensional-list. This should fix your referencing issue. – Red Shift Nov 12 '15 at 22:04
  • how are you initializing your matrix? because this could be a classic example of [multiple list references](http://stackoverflow.com/questions/9969609/two-separate-python-lists-acting-as-one) – R Nar Nov 12 '15 at 22:05
  • 2
    `for z in self.gameField` will iterate over every 'row' and set the 'cell' to `X` if it is `-` in the column referenced by `num`. – Richard Kenneth Niescior Nov 12 '15 at 22:06
  • @RedShift, a sublist is not changing so it cannot be a referencing issue – Padraic Cunningham Nov 12 '15 at 22:11
  • @Marco, you are changing all the first elements of each sublist so obviously they all look the same, `self.gameField [0][0]` would just get the first element of the first row, `self.gameField [5][0]` would get the first element of the last row – Padraic Cunningham Nov 12 '15 at 22:12

2 Answers2

0

Putting previous comments together:

Assuming your grid is created well (see possible issue with multiple sub-lists references - basically if you created your grid with something like self.gameField = [[['_']*7]*6], which seems short and clean, it is actually bad in your case), you just need to break out of the for loop as soon as you place an "X" (otherwise you just keep filling the whole column!):

for z in self.gameField:
    if z[num] == "-":
        z[num] = "X"
        print "Done"
        break

Note that you might want to iterate your rows (your for loop) in reverse order so that gravity is downwards when you print your grid (or print your grid in reverse)...

Julien
  • 13,986
  • 5
  • 29
  • 53
0

First of all defining lists in lists, you still need to seperate the lists with ,. So the following.

[['-','-']['-', '-']]

Would become.

[['-','-'],['-', '-']]

Secondly instead of looping through the lists, you are able to access list indexes in this fashion.

gameField[3][3] = 'X'

This access has a runtime of 1, if considering the algorytmic factor, not relevant now. But scaling up could become a problem. Putting it all togheter i created the following running code.

gameField = [['-', '-', '-', '-', '-', '-', '-'],['-', '-', '-', '-', '-', '-', '-'],['-', '-', '-', '-', '-', '-', '-'],['-', '-', '-', '-', '-', '-', '-'],['-', '-', '-', '-', '-', '-', '-'],['-', '-', '-', '-', '-', '-', '-']]

gameField[3][3] = 'X'

print(gameField)
mrhn
  • 17,961
  • 4
  • 27
  • 46