我有以下任務在螢屏上列印奶酪板的碎片,它們必須采用這種格式?,但這是最不重要的問題。
如果我回圈遍歷我得到這個 < main .Rook object at 0x7fc8fa510790>,我該如何編碼 --> 如果 main.Rook == ?,
代碼完成后的輸出應該是這樣的:
?????
?????
?????
?????
?????
目前我像那樣建造董事會
class Piece:
pos_x : int
pos_y : int
side_ : bool #True for White and False for Black
def __init__(self, pos_X : int, pos_Y : int, side_ : bool):
'''sets initial values'''
self.pos_x=pos_X
self.pos_y=pos_Y
self.side_=side_ #maybe set it to True?
def __str__(self):#
return f"{self.pos_x} {self.pos_y} {self.side_}"
def get_side(self):
return side_
class Rook(Piece):
def __init__(self, pos_X : int, pos_Y : int, side_ : bool):
'''sets initial values by calling the constructor of Piece'''
super().__init__(pos_X, pos_Y, side_)
wr1 = Rook(1,2,True)
br1 = Rook(4,3,False)
br2 = Rook(2,4,False)
br3 = Rook(5,4, False)
wr2 = Rook(1,5, True)
B1 = (10, [wr1,br1, br2, br3, wr2])
所以現在我正在研究以下功能,我沒有走遠,我可以得到棋子的位置,側面等等,但是我怎么知道它們是 ROOK 還是其他東西,如果我有一個變數,我可以使用它們例如名稱,但在這里我有一個物件,有人可以說明如何解決這個問題的方法嗎?
def conf2unicode(B: Board) -> str:
'''converts board cofiguration B to unicode format string (see section Unicode board configurations)'''
for piece in B[1]:
print (piece)
uj5u.com熱心網友回復:
為什么不將 Rook 設定__repr__為回傳類似 "?" 的內容:
def __repr__(self):
return "?"
然后您可以列印出 B1 并使您的圖示美觀整潔。
uj5u.com熱心網友回復:
一種方法是將較大的任務分解為幾個較小的步驟,即:
- 回圈遍歷每一行
- 找到該行中的所有部分并構建一個字串來表示它
- 列印該行
部分解決方案可能是列印沒有碎片的電路板,然后從那里擴展。
要獲得代表車的符號,您可以向類添加一個方法,該方法回傳正確的符號。
請注意,模運算子非常適合創建交替的黑白方塊。
uj5u.com熱心網友回復:
我會為板定義一個類,以便您可以將這些部件保存在二維網格中。然后,您還可以與字典進行一些交叉參考,這樣您就可以通過顏色和型別(即類別)快速找到板上的一塊。
這里有一些想法:
WHITE = True
BLACK = False
class Piece:
pos_x: int
pos_y: int
side: bool # BLACK or WHITE
def __init__(self, side: bool, symbol: str):
self.side = side
self.symbol = symbol
self.pos_x = self.pos_y = None
def set_position(self, pos_x: int, pos_y: int):
self.pos_x = pos_x
self.pos_y = pos_y
def clear_position(self):
self.pos_x = self.pos_y = None
def __repr__(self) -> str:
return self.symbol
class Rook(Piece):
def __init__(self, side: bool):
super().__init__(side, "??"[int(side)])
class King(Piece):
def __init__(self, side: bool):
super().__init__(side, "??"[int(side)])
class Bisshop(Piece):
def __init__(self, side: bool):
super().__init__(side, "??"[int(side)])
class Board:
def __init__(self, row_count: int = 8, col_count: int = 8):
self.rows = [[None] * col_count for _ in range(row_count)]
self.pieces = [{ # BLACK pieces by type
King: [],
Rook: [],
Bisshop: []
}, { # WHITE pieces by type
King: [],
Rook: [],
Bisshop: []
}]
def add(self, piece: Piece, pos_x: int, pos_y: int):
piece.set_position(pos_x, pos_y)
self.rows[pos_y][pos_x] = piece
self.pieces[piece.side][type(piece)].append(piece)
def remove(self, piece: Piece):
self.rows[piece.pos_y][piece.pos_x] = None
piece.clear_position()
self.pieces[piece.side][type(piece)].remove(piece)
def move(self, piece: Piece, to_x: int, to_y: int):
self.remove(piece)
self.add(piece, to_x, to_y)
def __repr__(self):
return "\n".join(
"".join(repr(piece) if piece else "." for piece in row)
for row in self.rows
)
board = Board(5, 5)
# coordinates are zero-index based:
board.add(Rook(WHITE), 0, 0)
board.add(King(WHITE), 2, 0)
board.add(Rook(BLACK), 1, 1)
board.add(Rook(BLACK), 4, 1)
board.add(King(BLACK), 1, 2)
board.add(Rook(BLACK), 3, 2)
board.add(Rook(WHITE), 0, 3)
board.add(Bisshop(WHITE), 4, 3)
board.add(Bisshop(WHITE), 0, 4)
print(board)
如果你想移動一個已經在板上的棋子,你可以這樣做:
board.move(board.pieces[WHITE][Bisshop][1], 1, 4)
這將移動第二個白人主教。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/372584.html
上一篇:當請求沒有“內容型別”HTTP請求標頭時,嘗試讓SpringBoot應用程式發送錯誤回應訊息
下一篇:處理金錢投入的最佳方式
