我想访问和更改我的 2D 字符列表中的单个字符。但是,当我更改一个时,它会更改整个列。为什么是这样?我究竟做错了什么?在第 31 行:我正在修改数组中的一个元素,但得到的结果与我想要和期望的结果不同。
下面是我的代码:
from random import *
class chessBoard:
BOARD = None
BOARD_WIDTH = 8
def __init__(self,W = 8):
self.BOARD = []
self.BOARD_WIDTH = W
aRow = ['.'] * self.BOARD_WIDTH
for curRow in range(self.BOARD_WIDTH):
self.BOARD.append(aRow)
def printBoard(self):
for curRow in self.BOARD:
print(curRow)
def initialiseRandomQ(self,N):
cords = []
for ii in range(self.BOARD_WIDTH):
for jj in range(self.BOARD_WIDTH):
cords.append( [ii,jj] )
shuffle(cords)
self.BOARD[0][0] = '1'
# for curQ in range(N):
# xx = cords[curQ][0]
# yy = cords[curQ][1]
# print(xx,yy)
# print(cords[curQ])
# self.BOARD[xx][yy] = 'Q'
#PROGRAM START#
N_QUEENS = 2
myChessBoard = chessBoard()
myChessBoard.initialiseRandomQ(2)
myChessBoard.printBoard()
白猪掌柜的
相关分类