python 写一个象棋游戏

board = [
    ['車', '馬', '象', '士', '將', '士', '象', '馬', '車'],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', '炮', ' ', ' ', ' ', ' ', ' ', '炮', ' '],
    ['卒', ' ', '卒', ' ', '卒', ' ', '卒', ' ', '卒'],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    ['兵', ' ', '兵', ' ', '兵', ' ', '兵', ' ', '兵'],
    [' ', '砲', ' ', ' ', ' ', ' ', ' ', '砲', ' '],
    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    ['車', '馬', '相', '仕', '帥', '仕', '相', '馬', '車']
]

# 打印棋盘
def print_board(board):
    for row in board:
        print(' '.join(row))

# 获取用户输入
def get_input():
    src = input("请输入要移动的棋子位置(格式:行 列):")
    dst = input("请输入目标位置(格式:行 列):")
    return src, dst

# 移动棋子
def move_piece(board, src, dst):
    src_row, src_col = map(int, src.split())
    dst_row, dst_col = map(int, dst.split())
    piece = board[src_row][src_col]
    board[src_row][src_col] = ' '
    board[dst_row][dst_col] = piece

# 主游戏循环
def main():
    while True:
        print_board(board)
        src, dst = get_input()
        move_piece(board, src, dst)

# 运行游戏
if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/ducanwang/article/details/131456671