我正在嘗試實現一個簡單的TicTacToe遊戲,但我無法制作某個列表的淺表副本。雖然我給你新的價值只體現在SIM卡中,變化體現在「主」板無法在Python3中創建列表的淺表副本
SIZE = 3 # size of game board
PLAYERS = (PLAYER, COMPUTER) = "X", "O"
class TicTacToe(list):
""" Class handling game board """
def __init__(self, size=SIZE, players=PLAYERS):
""" Board size defaults to 3 """
super().__init__([[" " for idx in range(size)] for jdx in range(size)])
self._size = size
def winner_move(self, letter, range_):
""" Determine winner move - range_ members are tuples """
for tile in range_:
sim = self[:] # shallow copy of game board for simulation
sim[tile[0]][tile[1]] = letter
print("Current board:")
print(self)
# code makes some decisions here and continues
def get_empty(self):
""" Return empty places on board as a set of (row, col) """
return {(row, col) for row in range(self._size)\
for col in range(self._size) if self[row][col] == " "}
board = TicTacToe()
board.winner_move(PLAYER, board.get_empty())
: 下面是我的腳本最小提取物,呈現我的問題。我也試過
sim = list(self)
和
sim = [item for item in self]
但都給予同樣的結果。
任何建議我做錯了什麼?
TicTacToe是一個2D結構,你真的需要淺而不深的複製? – Arpegius
@Arpegius是賴特,你需要深刻的複製 –