Friday, 6 September 2013

Two-dimensional list wrongly assigning values in python

Two-dimensional list wrongly assigning values in python

class Board:
def __init__(self):
self.board = self.createBoard()
def createBoard(self):
line = []
for i in range(7):
line.append(' ')
board = []
for i in range(7):
board.append(line)
return board
def showBoard(self):
line = "| "
for x in range(len(self.board)):
for y in range(len(self.board)):
line += self.board[x][y] + " | "
print("-" * 29)
print(line)
line = "| "
print("-" * 29)
if __name__ == '__main__':
board = Board()
board.showBoard()
board.board[1][1] = "O"
board.showBoard()
I was working on a connect-4 python console demo/game when I got stuck on
this really weird issue.
The output of the code above is as follows:
-----------------------------
| | O | | | | | |
-----------------------------
| | O | | | | | |
-----------------------------
| | O | | | | | |
-----------------------------
| | O | | | | | |
-----------------------------
| | O | | | | | |
-----------------------------
| | O | | | | | |
-----------------------------
| | O | | | | | |
-----------------------------
The weird thing is, I never assigned O to all those positions, I only
assigned it to the position [1][1].
I had expected the output to be:
-----------------------------
| | | | | | | |
-----------------------------
| | O | | | | | |
-----------------------------
| | | | | | | |
-----------------------------
| | | | | | | |
-----------------------------
| | | | | | | |
-----------------------------
| | | | | | | |
-----------------------------
| | | | | | | |
-----------------------------
It is extremely likely that I'm missing something obvious and small but
I've been looking and trying for over an hour and I really can't find the
problem.
It's not like my board.board list is any more odd than any other
two-dimensional list.
[[' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ']]
(It's what I get when I print(board.board))
Copying and pasting that into IDLE I get the following:
>>> a = [[' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', '
', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', '
', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', '
', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ']]
>>> a[1][1] = "O"
[[' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', 'O', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ']]
Which gets me the proper board value.
What is so obviously wrong in my code that I'm missing it? I'm pretty sure
that when any of you find an answer that I'll shake my head in shame, it's
likely that bad.
Enough self-shaming, so why does my code board.board[1][1] = "O" assign
the value "O" to the every single row in board.board?
Changing the first 1 to any other number from 0-6 doesn't change anything
either. It's all the same.

No comments:

Post a Comment