0

I'm new to python and am trying to modify a single value in a list of lists. I come from java experience. I am writing code for a chess application to familiarize myself with python. board is a list of lists that contains all the pieces that exist on the board.

def updateMoves(self):
    for i in range(self.getY()+1,col,1):
        if board[self.getX()][i] != None:
            self.canMove[self.getX()][i]=False
            break;
        else:
            self.canMove[self.getX()][i]=True

def printMoves(self):
    s=""
    for i in range(row):
        for k in range(col):
            if self.canMove[i][k]==True:
                s=s+"T,"
            else:
                s=s+"x,"
        print(s)
        s=""

GetY and X return a value stored on the piece of it's current position on the board(in this case the piece is in 2,2). The values in my list are started out as all false this code results in the canMove list(local to the class) looking like this: (x is false)

x,x,x,T,T,T,T,T,
x,x,x,T,T,T,T,T,
x,x,x,T,T,T,T,T,
x,x,x,T,T,T,T,T,
x,x,x,T,T,T,T,T,
x,x,x,T,T,T,T,T,
x,x,x,T,T,T,T,T,
x,x,x,T,T,T,T,T,

I decided to put breakpoints in, and on each loop of the array it changes the entire column to True. Any idea why this might be? I'm assuming its a syntax issue, but can't find anything in my google searches.

  • 1
    The problem is with how you created the `self.canMove` list. All the elements are the same list. – Barmar Jan 05 '20 at 18:35
  • 1
    What is the `board` you refer to? – quamrana Jan 05 '20 at 18:36
  • Off-topic: If ``board`` already holds a list of lists (i guess with the chess pieces) then why update a second list of lists with boolean values? This information is redundant. And consider to get rid of getter / setter methods. Either use properties or simply use attributes. Python is not Java. – Mike Scotty Jan 05 '20 at 18:37
  • board is a global variable defined at the beginning of the file: row, col = (8,8) board = [[None]*row]*col canMove is defined on the piece class with: canMove = [[False]*row]*col board is to contain the pieces, the canMove is stored on each piece to tell where the pieces can move to. – yuripulzeff Jan 05 '20 at 18:39
  • Please update the question with this information. – quamrana Jan 05 '20 at 18:40
  • 2
    `[[None] * row] * col` does _shallow_ copies. It makes one list containing `None`, then makes `row` copies of that reference. Then `* col` makes `col` copies of _that_ reference. So you really have only two lists here - one list of `[None]`, and a list of multiple references to that list. See the linked duplicate question for more details and options to change this. – Peter DeGlopper Jan 05 '20 at 18:40
  • Thank you guys, seems kind of funky, but makes sense. Revised the creation of the list to: row, col = (8,8) board = [[None]*row for _ in range(col)] and canMove = [[False]*row for _ in range(col)] – yuripulzeff Jan 05 '20 at 18:53
  • Sorry, the above is slightly off - `[None] * row` does a shallow copy of `None` to fill out a new list. Three identical references to `None` is fine - `None` is a singleton anyway. It's specifically the `* col` part that's introducing problems for you - that's doing the shallow copy of the referenced list. There are still only two lists, but one is `[None, None, None]` (or however many) and the other is repeated references to the first one. – Peter DeGlopper Jan 05 '20 at 19:00

0 Answers0