While writing a code snippet for a Leetcode question, I noticed that my Python3 arrays were not getting updated as expected.
Basically, I have a 2D array listing players moves for a tic-tac-toe game. Player A plays first, Player B next, Player A next, and so on. My first move is to represent these moves on a 2D array. The array moves
listed below mentions the (x,y) co-ordinates of the 3x3 TicTacToe grid that is to be updated.
(0,0) | (0,1) | (0,2)
_______|_________|________
(1,0) | (1,1) | (1,2)
| |
_______|_________|________
(2,0) | (2,1) | (2,2)
| |
moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
arr=[['']*3]*3
#print(arr)
for i in range(len(moves)):
xC=moves[i][0]
yC=moves[i][1]
if(i%2==0):
#playerA
arr[xC][yC]='X'
else:
#playerB
arr[xC][yC]='O'
print(arr)
The variable 'moves' lists the population of only 5 boxes out of 9 boxes in the tic tac toe game, but the output array 'arr' is populated as such:
[['O', 'O', 'X'], ['O', 'O', 'X'], ['O', 'O', 'X']]
Just fyi: I've been using Py3 for a few years now, and I'm comfortable with it (not a newbie). What's your thoughts on the issue? A bug in Py3's array module? Let me know. (btw, my first StackOverflow Question!)
The question is at this address: https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/