使用抽象类构建的 Python 二维列表在不同位置产生相同的值

Python two dimensional list built using abstract classes is yielding the same value in different locations

提问人:spiff 提问时间:2/13/2021 更新时间:2/13/2021 访问量:37

问:

问题

这是我第一次真正尝试 Python。我正在制作一个简单的国际象棋游戏,这样我就可以学习这门语言,但我还没有走得很远。如果这个问题的答案对除了我之外的每个人都是显而易见的,请原谅我。我今天花了一整天的时间试图弄清楚这一点,在网上搜索无济于事(除了发现我在 Python 用户中使用的这个 2d 列表设置有点有争议的主题)。

问题是我的所有 16 个都以某种方式全部打印出来(如在玩家 2 中),而应该有 8 个打印为 和 8 个打印为 .我相信问题是它们是具有 16 个不同引用的同一个对象,但我不太清楚如何解决它,并且我尝试了许多不同的东西(包括 ),但我一直得到相同的结果。pawn212deepcopy

到目前为止,我有三个文件:、 和 (它由一个名为 的类的二维列表组成):piece.pypawn.pyboard.pysquare

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  |//---//|  ---  |//---//|  ---  |//---//|  ---  |//---//|  ---  |
    |///////|       |///////|       |///////|       |///////|       |
    -----------------------------------------------------------------
Python 列表 维数组 抽象类 传递引用

评论

1赞 user2357112 2/13/2021
这些是怎么回事?你是否觉得你应该用它来标记正常的方法?你不是。@classmethod
0赞 spiff 2/13/2021
我的印象是,您标记了要按原样继承的方法@classmethod
0赞 user2357112 2/13/2021
不。它做了一些完全不同的事情。
0赞 user2357112 2/13/2021
您无需执行任何特殊操作即可继承方法。这是默认设置,没有内置方法可以将其关闭。
0赞 spiff 2/13/2021
哦,好吧,我以为这意味着它们无法更改,但我刚刚删除了所有语句,现在它可以工作了,所以谢谢@classmethod

答: 暂无答案