当用户输入“a1”这样的坐标时,我想找到该方块上的棋子,并将其图标替换为“#”字符并渲染新棋盘。所以新棋盘中的车应该是“#”符号。
#IMPORTS
import ctypes
from ctypes import *
##END
# IMPORT C/C++ MODULE FOR GAME TITLE
lib = ctypes.CDLL('./c_title_module.so')
lib.c_title_module.restype = ctypes.c_char_p
title = lib.c_title_module()
title_str = title.decode('utf-8')
##END
#THE TITLE IMPORTED FROM C FILE.
print(title_str, "\n\n")
#PIECES FOR BLACK
piecesB = ["♜", "♞", "♝", "♛", "♚", "♝", "♞", "♜"]
pawnsB = ["♟︎", "♟︎", "♟︎", "♟︎", "♟︎", "♟︎", "♟︎", "♟︎"]
##END
#PIECES FOR WHITE
piecesW = ["♖", "♘", "♗", "♕", "♔", "♗", "♘", "♖"]
pawnsW = ["♙", "♙", "♙", "♙", "♙", "♙", "♙", "♙"]
##END
#RAW CHESS BOARD
r1 = ["a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"]
r2 = ["a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2"]
r3 = ["a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3"]
r4 = ["a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4"]
r5 = ["a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5"]
r6 = ["a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6"]
r7 = ["a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"]
r8 = ["a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8"]
##END
#FIND THE PIECE ON THE GIVEN SQUARE.
def find_piece(square, board):
for idx, row in enumerate(board):
if square in row:
piece_idx = row.index(square)
if idx < 2: # If it's a black piece
print(board[idx][piece_idx]) # Print the piece
board[idx][piece_idx] = "#" # Replace with "#"
return board[idx][piece_idx]
elif idx > 5: # If it's a white piece
print(board[idx][piece_idx]) # Print the piece
return board[idx][piece_idx]
else:
return "" # Empty square
return ""
def board_display(board):
for row in board:
if "#" in row:
# Replace "#" with the original pieces before displaying
row = ["#" if square == "#" else square for square in row]
print(" ".join(row))
def move(board):
board_display(board)
piece_initial = input("Move Piece On Square: ")
piece = find_piece(piece_initial, board)
if piece:
print("Piece on square", piece_initial, ":", piece)
piece_final = input(f"Move piece on square '{piece_initial}', to square: ")
piece_2 = find_piece(piece_final, board)
return board # Return the updated board
# Define the initial state of the board
board = [
piecesW,
pawnsW,
["#" for _ in range(8)],
["#" for _ in range(8)],
["#" for _ in range(8)],
["#" for _ in range(8)],
pawnsB,
piecesB
]
while True:
print("Current Board Setup: ")
board = move(board) # Update the board with the move
期望下次显示棋盘时,输入的方格上的棋子会被替换为“#”字符。现在什么也没有发生。
可能是由于下面的
if square in row
行:
def find_piece(square, board):
for idx, row in enumerate(board):
if square in row: # Square == "b2", but row is filled with "#" or piece symbols
pass
假设我为
"b2"
输入了 square
值。由于您枚举的是仅包含 board
或 #
的 piece symbols
,因此 square in row
条件将始终为 false。