简体   繁体   English

从库 python-chess on line 命令可视化完整的棋盘

[英]Visualize the full chess board from the library python-chess on line command

I am working a chess game based on this library: https://pypi.org/project/python-chess/ or https://github.com/niklasf/python-chess我正在开发基于此库的国际象棋游戏: https : //pypi.org/project/python-chess/https://github.com/niklasf/python-chess

On Jupyter Notebook, if I run this code:在 Jupyter Notebook 上,如果我运行此代码:

import chess
board = chess.Board()
board

It will display a nice board (ie with colors, shape, looking like a chess board).它将显示一个漂亮的棋盘(即具有颜色、形状、看起来像棋盘)。 If I run like this:如果我像这样运行:

import chess
board = chess.Board()
print(board)

It will display the board in a much more rudimental way with letters.它将以更基本的方式用字母显示板。

The problem is that the only way of seeing the nice board, using the "board" command, is if I am using Jupyter Notebook.问题是,使用“board”命令查看漂亮板子的唯一方法是,如果我使用的是 Jupyter Notebook。 If I try to run on Visual Studio or line command the command "board" nothing will happen.如果我尝试在 Visual Studio 或 line 命令上运行命令“board”,则不会发生任何事情。 It seems that the line command will not support the use of "board" (from their website: Supports Python 3.6+ and PyPy3.IPython/Jupyter Notebook integration).似乎 line 命令不支持使用“board”(来自他们的网站:Supports Python 3.6+ and PyPy3.IPython/Jupyter Notebook integration)。

Is there a way around this?有没有解决的办法? In other words, can I still run "board" on command line and visualize the nice chess board?换句话说,我仍然可以在命令行上运行“board”并可视化漂亮的国际象棋棋盘吗?

If all you want to do is simply graphically display a position, you can install and use the chess-board package.如果您只想以图形方式显示位置,则可以安装和使用chess-board包。 I use it in tandem with the one you are talking about in the following example:我将它与您在以下示例中谈论的那个一起使用:

import chess

from chessboard import display
from time import sleep

board = chess.Board()

move_list = [
    'e4', 'e5',
    'Qh5', 'Nc6',
    'Bc4', 'Nf6',
    'Qxf7'
]

display.start(board.fen())
while not display.checkForQuit():
    if move_list:
        board.push_san(move_list.pop(0))
        display.update(board.fen())
    sleep(1)
display.terminate()

This package however currently feels quite lacking when it comes to flexibility and well... sanity.然而,这个包目前在灵活性和......理智方面感觉相当缺乏。 You would have to at least open the packages display.py file and add one line at the beginning of the start() function.您至少必须打开包 display.py 文件并在 start() 函数的开头添加一行。 Otherwise, you will not be able to use display.update().否则,您将无法使用 display.update()。

def start(fen=''):
    global gameboard

I see you posted this question a long time ago.我看到你很久以前发布了这个问题。 Nonetheless, I hope you find this useful.尽管如此,我希望你觉得这很有用。

you can use chess.svg你可以使用 chess.svg

import chess.svg
board = chess.Board()
chess.svg.board(board, size=350)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM