微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

问题

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

问题是当应该有 8 个打印为 pawn 和 8 个打印为 { 时,我的所有 16 个 2 都以某种方式全部打印出 1 {1}}。我相信问题在于它们是具有 16 个不同引用的同一个对象,但我无法弄清楚如何修复它,并且我尝试了许多不同的东西(包括 2)但我一直得到相同的结果.

到目前为止,我有三个文件deepcopypiece.pypawn.py(由一个名为 board.py 的类的二维列表组成):>

piece.py

square

pawn.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

board.py

from piece import piece

class pawn(piece):

    symbol = 'pn'
    legalmoves = [[0,1],[0,2]]

输出

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('    -----------------------------------------------------------------')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。