Use Python's tkinter module to write a good-looking and powerful Chinese chess

        Following my first version of the "Chinese Chess" program last time, I wrote the second version of the "Chinese Chess" program. Fans who follow me know that I said at the end of the first "Chinese Chess" article , I will publish the second edition. Friends who are interested in the first edition can go and have a look. It is also a very good program, but there are some flaws in some places, but this time I made up for these shortcomings.

This is the link of the first edition of "Chinese Chess" (the source code and program can be downloaded in the article): Tkinter module GUI graphical programming practice (8) - Chinese Chess (including super detailed explanation and complete source code, complete program free download link) _tkinter Chess_Xiaokang 2022's blog - CSDN blog https://xiaokang2022.blog.csdn.net/article/details/125796786 Explanation in advance

In the second edition of "Chinese Chess":

  • No third-party modules are used
  • no image files used
  • The program contains audio files, there are sound effects of moving chess, eating chess, and "general", and the button will also produce sound effects when pressed
  • The chess pieces and chessboard in the program are all drawn by Canvas
  • The animation effects of the movement of the chess pieces and the floating of the prompt box are all made by using my self-made module tkintertools
  • The program has the function of being compatible with different DPIs, and the definition is greatly improved
  • The drawn graphics have no anti-aliasing function for the time being

2023/02/04 update: v1.0 → v1.3

  • Fix known bugs
  • Added chess game library function (in the menu bar), which contains up to 1000 kinds of chess games for users to use
  • The source code file (SourceCode.zip) is supplemented in the download link, and the decompression password is: XiaoKang2022@CSDN

Provide source code for free, I hope everyone can support my tkintertools module: [ tutorial address ] [ warehouse address ]

feature start

Here is the effect of the program (the program is also downloadable at the end of the article):

checkerboard

The chessboard is the same as the classic chess board, there is no difference, but there is a timing display in the center of the board, which will show which side is currently thinking and give the time it takes for the current side to think. When the game mode is not selected, it looks like the one above.

basic pattern

When you open the program, the selection mode window will pop up automatically, or you can manually open it through the "New Game" button in the menu bar of the window, or press the shortcut key "Ctrl+N" to open this window. There are three basic modes displayed in the window, click to select the corresponding mode, and set some game rules, such as handicap, first hand setting, etc. When the mouse stays on the mode selection button, some prompt information will be displayed in the display bar of the information below (the place where "Please select the game mode" is displayed above). When the mode selection button is pressed, there will be a sound effect.

game instructions

In the game description window, you can find the game description option through "Help (H)" in the window menu bar, click to see the above window, or press the shortcut key "Ctrl+H" to quickly open the window. This window will read the help.md file in the same directory of the program, and perform a simple analysis on it. After analysis, the content corresponding to the number of pages will be generated, and the above is 12 pages. Click the page up and down button to switch pages, of course, this button also has sound effects. The game description introduces the basic playing rules of "Chinese Chess" (most of which are copied from Baidu Encyclopedia).

Mode introduction

The last page of the game description introduces all the modes of this program, and it is released here for everyone to take a look [Note, this is not the latest version of the description, there may be some errors with the new version].

Statistical data

Literally, the statistical information of the program, the statistical information is stored in the statistic.json file in the same directory as the program, and the above data are created when I write the program (debugging).

game settings

Game settings can be found in the list of options in the menu bar, and currently there are relatively few configurable items. The window zoom factor determines the size of the window. Of course, the above factor of 0.9 is manually set, because it looks clearer on my computer. You can also set the window auto-scale value to True to let it automatically find the best scaling value. The number of rounds for the draw refers to a situation in the draw, that is, when both sides have no captures within N rounds, the game is considered a draw. The default is 60, and you can change it to other values. I don’t need to say about the maximum search depth of AI. As for what it means when the chess pieces can be moved, please read the following.

Moveable display of chess pieces

When the option mentioned above is set to True, this position display and enemy red mark will appear, of course, this will not affect the green mark when the chess piece is selected. In addition, here is a point, when the mouse stays over the chess piece, the chess piece will be highlighted, and the mouse style will become the one that can be clicked. By the way, the determination of the position of the mouse click is not a rectangle, but a circle!

all options

Opening is set to load the endgame. In fact, it is not necessarily the endgame. As long as it conforms to the FEN chess file format, the program can recognize it. Save as means to save the current chess game in FEN format. About this format, here is a simple For details, you can go to Chess Encyclopedia: Chinese Chess Computer Application Specification (3): FEN File Format (xqbase.com)

The following is a chess file in FEN format (Seven Star Gathering.fen):

4rk3/3P5/4bP3/9/9/8P/9/1p2p2C1/3p1p3/4K1RR1 w

Slashes distinguish each line, the last letter distinguishes red and black squares, numbers represent the number of empty areas, other letters correspond to chess pieces, lowercase is for black squares, and uppercase is for red squares.

The undo and restore functions are the functions of undo and undo, which are supported in the man-machine battle mode and the two-player game mode. The former regrets two moves at a time, and the latter regrets one step at a time. The same is true for the undo regret function.

subsetting

This selection function is supported in man-machine battle mode, two-player game mode, and online mode. I don’t need to say more about the others.

active connection

This is one of the online modes, the active connection mode, and the passive connection mode. The online mode supports LAN connection, requiring the connected party to use a passive connection mode, that is, as a server connection, and the connecting party adopts an active connection mode. Equivalent to the client. Of course, for the convenience of the user, the program will automatically search for recognizable connections without manually inputting the IP address, and the user only needs to select from the list after the search.

passive connection

It is the passive connection method mentioned above, the single waiting time is 60 seconds, and the users who try to connect are detected during this period.

Floating tooltip

In the upper left corner of the picture above is a floating prompt box, which will slide out from the left and play a system prompt, and then retract after staying for 3000 milliseconds. When entering a new game mode, there will be a prompt box, and prompts will also be given when performing these two functions when you cannot undo the game or undo the game.

seven star party

That's all for the presentation, let's explore more functions by yourself, here is a picture of the endgame of "Seven Star Gathering" for everyone. Finally, I would like to talk about the function of AI. My evaluation is that it is not bad. When , a single movement calculation takes up to about 8 seconds, and the effect is barely good.

[Updated on 2023/02/04]

Game library

(Here, let me declare in advance that this chess game library is a resource I obtained from the Internet. It is only for learning and reference, and it will be deleted for infringement)

Five major categories, several subcategories, a total of more than 1,000 chess games are available. Game files are stored in FEN format. There are a maximum of 10 options displayed on this page, but if there are more, you can slide down, and no scroll bar will be displayed here, because I didn't do it...

program files

Not much to say here, you can see the way to chat with me at the bottom of the article, see the file download link:

Chess.zip - Lanzoum File Size: 10.8 M | https://wwc.lanzoum.com/iwgp00mlewpa

— The article is continuously updated —

Guess you like

Origin blog.csdn.net/weixin_62651706/article/details/128852029