Chinese chess engine beginners

Source: Internet
Author: User

I used to write a chess game with AI.ProgramBut when I was a sophomore, I wroteCodeIt was so messy that I recently used the time to rewrite one. I thought it was too troublesome to write the interface. I wrote an engine directly according to the ucci protocol, and then I went online to find an interface, simple, crude, and effective. At present, my program can reach 7-8 layers within 3 seconds when opening a middle office, and 9-10 layers within 5 seconds when launching a middle office, the disadvantage is that the evaluation function is too crude.

I wrote it in C #, and it seems that I only use C #.. During countless times of reference to the Encyclopedia of chess network, web site, interested can go to see, a very good site.

To put it bluntly, an engine is a console program that provides corresponding feedback based on input information and other information.

The engine accepts the message [console input] sent from the interface. According to what the message processing engine needs to do internally, the engine then outputs feedback through the console. I only know that the interface will intercept the standard input and output stream of the engine and then interact with the engine. I have not studied how to implement it ..

An engine can take the following steps:

1. Listener Input

2. parsing commands

3. Run the corresponding computing

4. Output corresponding feedback

In my own program, it is divided into the following parts:

1. Listener Input

2. parsing commands

3. Execute computing

4. Output Feedback

5. log recording


First, we need to parse the ucci Command sent from the interface. This requires a class to parse the command, which is the uccihelper class in my program. Then we know what the interface will let us do, however, if we haven't done it yet, we need to make corresponding actions based on the instructions and command parameters. In my program, only the following commands are supported:

1. ucci:

This command was sent to the engine after the engine was started on the interface. It is a guiding period. He told the engine that the current protocol is ucci, at this time, the engine needs to initialize some data settings of the engine, such as some parameters, and finally feed back the engine name, version, copyright, and other information. Finally, it gives feedback to ucciok to inform the interface of preparation, after that, the engine will be idle [in the pilot state when the engine is started ].

2. Position Fen <Fen> moves <...>

This command tells the engine a situation and what steps are taken after this situation, and the engine needs to set the array value of the board inside the engine based on this information, as well as the current sub-party, record the past situation, and so on,

Since then, the engine does not provide any feedback, waiting for other commands, and the engine is still idle.

3. Go [ponder] [depth] [time] [] movestogo]

This command tells the engine to start computing. the engine will set a value based on the parameter of this command and start computing. Then, the engine enters the thinking state. After the calculation is complete or times out, the engine outputs bestmove XX ponder XX as feedback, and then returns idle status.

For example: Go depth 7, the engine starts to calculate the depth of 7 based on the currently set checkboard value and related data. After the calculation is complete, it returns bestmove b2e2 ponder b9c7, this feedback tells the interface that the best way to do the current situation is to crack the gun, and guesses that the opponent may jump the horse.

4. ponderhit

This command tells the engine that the opponent guessed a hit. This command is usually used by the engine in the background. After receiving this command, the engine's clock is enabled.

5. Stop

This command requires the engine to immediately stop computing and give feedback on the best practice.

My program only supports these five commands, but it has already interacted well with the interface. [For Me,]


These are the interactions with the interface and so on. They mainly record the design of the core part and the problems encountered:

1. Data Representation

Data Representation is nothing more than a chess piece and a chess board. In my program, a chess piece uses byte 1 to 7 to represent seven different types of chess pieces, the checker uses a one-dimensional array with a size of 256.

2. Generate steps

This part is also a considerable part of the entire engine. It is worth studying to design a good way to generate a function, because this part takes the most time for the program to run. First, we need to find a piece of chess on the board, and then generate all the moves he can go according to his walking rules, finally, sum up the walk of all the pawns on the board to a collection, and return this set for other parts.

3. Search

Searching means that you take turns step by step and deduct all possible situations one by one.

4. Assess the situation

The purpose of the search is to deduct the possible situations one by one, but all of them are deducted. To compare which one is better for you, you need to evaluate the situation, give a score on the quality of a situation.


For now, all game software cannot escape this architecture, but each game software is different in every step.

In my program, some arrays are used to generate routes to avoid unnecessary loops. However, the routes of vehicles and guns do not know how to avoid loops, the search uses α-β pruning.AlgorithmThe node sequence for searching is quite sensitive. There are more than N sorts in the formal software, but there are only two sorts in my program. The first is to search and take sub-steps first, sort by the most valuable sub-items, and sort by the historical table by the remaining sub-steps. Various sorting strategies are called inspiration, evaluate the sub-force value table of the open-source chess engine elephanteye directly copied as the core of the evaluation function. When evaluating this part, you must use static search to overcome the horizontal effect, however, the search speed of my program is slow. It is useless because I cannot afford the resource overhead brought about by static search. One way to speed up the search is to crop the search in blank steps, this may increase by 2-3 layers, but in the case of the final fault and some special circumstances, it may cause a big problem to crop the blank step forward. For example, it is better to do nothing than to take any step, for example, in my program, the temptation to use this technology is not enough to improve 2-3 layers, however, his negative effects only prevent me from using the empty-step forward pruning policy when the People's Republic of China is working. For example, when I started the game, my program was hacked, when he does not use the empty step to crop forward, 3- The search results on the 9-layer are all right horses, but the 3-7 layers are right horses after the empty step is used to crop the forward. From the eighth layer, he thinks that the right horse is better than the right horse, this is unreasonable because the specific cause is not found. Another problem is that the search returns a positive and negative value for the odd and even layers in the same situation. The values are also different. In principle, in the same situation, the odd and even layers should not be at the point of changing the symbol. At most, the numbers are different.

Now I have pasted the Source Code address with more comments than this one.ArticleIf you are interested, you can download it and check it out. If you are interested, you can add QQ friends And hey.

Source codeAddress:



Reprinted please indicate the source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.