提问人:spiff 提问时间:2/13/2021 更新时间:2/13/2021 访问量:37
使用抽象类构建的 Python 二维列表在不同位置产生相同的值
Python two dimensional list built using abstract classes is yielding the same value in different locations
问:
问题
这是我第一次真正尝试 Python。我正在制作一个简单的国际象棋游戏,这样我就可以学习这门语言,但我还没有走得很远。如果这个问题的答案对除了我之外的每个人都是显而易见的,请原谅我。我今天花了一整天的时间试图弄清楚这一点,在网上搜索无济于事(除了发现我在 Python 用户中使用的这个 2d 列表设置有点有争议的主题)。
问题是我的所有 16 个都以某种方式全部打印出来(如在玩家 2 中),而应该有 8 个打印为 和 8 个打印为 .我相信问题是它们是具有 16 个不同引用的同一个对象,但我不太清楚如何解决它,并且我尝试了许多不同的东西(包括 ),但我一直得到相同的结果。pawn
2
1
2
deepcopy
到目前为止,我有三个文件:、 和 (它由一个名为 的类的二维列表组成):piece.py
pawn.py
board.py
square
piece.py
from abc import ABC, ABCMeta, abstractmethod
class piece(ABC):
@property
@abstractmethod
def symbol(self) -> str:
raise NotImplementedError
@property
@abstractmethod
def legalmoves(self) -> list:
raise NotImplementedError
@classmethod
def __init__(self, side: int):
if (side == 1) or (side == 2):
self.side = side
else:
raise ValueError('Side must be 1 or 2')
@classmethod
def checkmove(self, move: list) -> bool:
legal = False
for legalmove in self.legalmoves:
if (move[0] == legalmove[0]) and (move[1] == legalmove[1]):
legal = True
return legal
@classmethod
def getsymbol(self) -> str:
return str(self.side) + self.symbol
pawn.py
from piece import piece
class pawn(piece):
symbol = 'pn'
legalmoves = [[0,1],[0,2]]
board.py
from piece import piece
from pawn import pawn
class square(object):
def __init__(self, row: int, column: int):
self.row = row
self.column = column
self.occupied = False
self.piece = None
def setrow(self, row: int):
self.row = row
def setcol(self, col: int):
self.column = col
def setpiece(self, piece: piece):
if self.occupied:
raise ValueError("Square is occupied")
else:
self.piece = piece
self.occupied = True
def getrow(self) -> int:
return self.row
def getcol(self) -> int:
return self.column
def getpiece(self) -> piece:
return self.piece
# Set up board
num_rows = 8
num_cols = 8
board = []
for r in range(num_rows):
row = []
for c in range(num_cols):
row.append(square(r,c))
board.append(row)
row = None
# Fill with pieces
for c in range(num_cols):
board[1][c].setpiece(pawn(1))
board[6][c].setpiece(pawn(2))
# Draw board
print(' -----------------------------------------------------------------')
print(' | |///////| |///////| |///////| |///////|')
print(' 8 | |//---//| --- |//---//| --- |//---//| --- |//---//|')
print(' | |///////| |///////| |///////| |///////|')
print(' -----------------------------------------------------------------')
print(' |///////| |///////| |///////| |///////| |')
print(' 7 |//' + board[6][0].getpiece().getsymbol() + '//| ' + board[6][1].getpiece().getsymbol() +
' |//' + board[6][2].getpiece().getsymbol() + '//| ' + board[6][3].getpiece().getsymbol() +
' |//' + board[6][4].getpiece().getsymbol() + '//| ' + board[6][5].getpiece().getsymbol() +
' |//' + board[6][6].getpiece().getsymbol() + '//| ' + board[6][7].getpiece().getsymbol() + ' |')
print(' |///////| |///////| |///////| |///////| |')
print(' -----------------------------------------------------------------')
print(' | |///////| |///////| |///////| |///////|')
print(' 6 | |//---//| --- |//---//| --- |//---//| --- |//---//|')
print(' | |///////| |///////| |///////| |///////|')
print(' -----------------------------------------------------------------')
print(' |///////| |///////| |///////| |///////| |')
print(' 5 |//---//| --- |//---//| --- |//---//| --- |//---//| --- |')
print(' |///////| |///////| |///////| |///////| |')
print(' -----------------------------------------------------------------')
print(' | |///////| |///////| |///////| |///////|')
print(' 4 | |//---//| --- |//---//| --- |//---//| --- |//---//|')
print(' | |///////| |///////| |///////| |///////|')
print(' -----------------------------------------------------------------')
print(' |///////| |///////| |///////| |///////| |')
print(' 3 |//---//| --- |//---//| --- |//---//| --- |//---//| --- |')
print(' |///////| |///////| |///////| |///////| |')
print(' -----------------------------------------------------------------')
print(' | |///////| |///////| |///////| |///////|')
print(' 2 | ' + board[1][0].getpiece().getsymbol() + ' |//' + board[1][1].getpiece().getsymbol() +
'//| ' + board[1][2].getpiece().getsymbol() + ' |//' + board[1][3].getpiece().getsymbol() +
'//| ' + board[1][4].getpiece().getsymbol() + ' |//' + board[1][5].getpiece().getsymbol() +
'//| ' + board[1][6].getpiece().getsymbol() + ' |//' + board[1][7].getpiece().getsymbol() + '//|')
print(' | |///////| |///////| |///////| |///////|')
print(' -----------------------------------------------------------------')
print(' |///////| |///////| |///////| |///////| |')
print(' 1 |//---//| --- |//---//| --- |//---//| --- |//---//| --- |')
print(' |///////| |///////| |///////| |///////| |')
print(' -----------------------------------------------------------------')
输出:
-----------------------------------------------------------------
| |///////| |///////| |///////| |///////|
8 | |//---//| --- |//---//| --- |//---//| --- |//---//|
| |///////| |///////| |///////| |///////|
-----------------------------------------------------------------
|///////| |///////| |///////| |///////| |
7 |//2pn//| 2pn |//2pn//| 2pn |//2pn//| 2pn |//2pn//| 2pn |
|///////| |///////| |///////| |///////| |
-----------------------------------------------------------------
| |///////| |///////| |///////| |///////|
6 | |//---//| --- |//---//| --- |//---//| --- |//---//|
| |///////| |///////| |///////| |///////|
-----------------------------------------------------------------
|///////| |///////| |///////| |///////| |
5 |//---//| --- |//---//| --- |//---//| --- |//---//| --- |
|///////| |///////| |///////| |///////| |
-----------------------------------------------------------------
| |///////| |///////| |///////| |///////|
4 | |//---//| --- |//---//| --- |//---//| --- |//---//|
| |///////| |///////| |///////| |///////|
-----------------------------------------------------------------
|///////| |///////| |///////| |///////| |
3 |//---//| --- |//---//| --- |//---//| --- |//---//| --- |
|///////| |///////| |///////| |///////| |
-----------------------------------------------------------------
| |///////| |///////| |///////| |///////|
2 | 2pn |//2pn//| 2pn |//2pn//| 2pn |//2pn//| 2pn |//2pn//|
| |///////| |///////| |///////| |///////|
-----------------------------------------------------------------
|///////| |///////| |///////| |///////| |
1 |//---//| --- |//---//| --- |//---//| --- |//---//| --- |
|///////| |///////| |///////| |///////| |
-----------------------------------------------------------------
答: 暂无答案
评论
@classmethod
@classmethod
@classmethod