Development of black and white games in C language in windows SDK

Source: Internet
Author: User

Recently, I was designing a Data Structure course and chose the most difficult development of black and white games, because I had decided to use pure C for development. So before that, my friends said that using MFC for development would definitely not do it. There are a lot of black and white game source code on the Internet, but most of the C language source code is not called graphic library development, or C ++ is used for MFC development, there are very few examples of using pure C to call Windows API for development, so since I have been suffering for so long, if someone wants to suffer as much as I do in the future, I would like to share my experience here. Even if I cannot help him technically, I can also comfort him psychologically. Let's talk about my design.

Implementation Method:

Because I used the Graphics library to implement the dos graphical interface in the last C language course, I want to use different implementation methods in this data structure experiment, I used pure C to call SDK functions to design windows programs, but I have not yet formed a complete object-oriented idea, so I gave up using MFC to write programs at the beginning. In fact, MFC encapsulates windows APIs with classes for reuse. on the Internet, we can find many black and white games written using MFC, however, there are very few programs written using pure C to directly call windows APIs. I think this should be a breakthrough. Of course, I am not saying that using MFC to write programs is not as good as SDK programming, but to better learn MFC in the future, it is necessary for me to master SDK programming. This will help me better understand the structure of MFC Classes, however, MFC does not provide the API function and can call the API for implementation. When the MFC Classes does not meet my needs, I can easily change its Class. Finally, I decided to adopt SDK programming.

Knowledge reserve:

Because my previous windows programming experience only had to write the Russian box of a dialog box program, and that logic is much simpler than the black and white games, I have to learn to write windows programs from the beginning. I need to understand the event-driven process, some basic API functions of windows, the Message Processing Mechanism of windows, and the logic of my program, of course, most of this knowledge can be found in MSDN, so I have been reading MSDN For a long time this summer.

Difficult questions:

Debugging in a windows program compiler environment is more difficult than debugging a general program. Although the breakpoint can be played at will, it is not so easy to intercept messages, and due to lack of windows programming experience, as a result, many detours were taken during programming. I also explicitly called the API to send messages, for example, to draw the main window, when the window is moved or blocked, the program will automatically send the WM_PAINT message to the window handle, because you do not know this before, you can display the message sent by calling the function during the program design, as a result, the interface will flash continuously. There are many such problems. I believe the teacher understands what I mean. In comparison, the design of the black and white games logic is simple, because there is a lot of information available on the Internet, there are a lot of sub-algorithms of the light black and white games, what basic search algorithms, alpha-beta pruning algorithms, the main algorithms include the Zobrist hashing algorithm, MTD (f) algorithm, and iterative deepening search algorithm. You can refer to many materials in this area, but I also saw my shortcomings. Although I understand these algorithms, I am afraid I cannot design these clever algorithms by myself, there are still some algorithms that I cannot even understand, such as simulated annealing Algorithms and neural network algorithms. Data structure is the foundation of algorithms. It can be seen that I still have a lot to learn.

Resource design:

This program is a program in windows, and it is necessary to find resources, movies, sounds, and so on. This time I downloaded a large number of black and white pictures and sound resources on the Internet, finally adopted a group of black and white games resources, and edited them into the project using the resource editor of VC ++:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image002 "border =" 0 "alt =" clip_image002 "height =" 244 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR42608-0.jpg "/>

Then, I created multiple dialogs and menus and edited them as resources:

1. dialog box about black and white games:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image004 "border =" 0 "alt =" clip_image004 "height =" 288 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR430G-1.jpg "/>

2. Opening option dialog box:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image006 "border =" 0 "alt =" clip_image006 "height =" 361 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR44443-2.jpg "/>

3. Go to the hero List dialog box:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image008 "border =" 0 "alt =" clip_image008 "height =" 254 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR43633-3.jpg "/>

4. Help dialog box:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image010 "border =" 0 "alt =" clip_image010 "height =" 320 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR41229-4.jpg "/>

5. Heroes dialog box:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image012 "border =" 0 "alt =" clip_image012 "height =" 331 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR4HQ-5.jpg "/>

6. Setting Dialog Box:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image014 "border =" 0 "alt =" clip_image014 "height =" 233 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR45021-6.jpg "/>

7. Game Play dialog box:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image016 "border =" 0 "alt =" clip_image016 "height =" 220 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR42M6-7.jpg "/>

8. Win dialog box:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image018 "border =" 0 "alt =" clip_image018 "height =" 204 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR4IL-8.jpg "/>

9. fl dialog box:

 

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image002 [4] "border =" 0 "alt =" clip_image002 [4] "height =" 167 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR43O0-9.jpg "/>

10. Menu:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image004 [4] "border =" 0 "alt =" clip_image004 [4] "height =" 166 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR435C-10.jpg "/>

Module Design:

I have done two module designs for this black and white game program. For the first time, I divided four modules: Main module, drawing module, logic control module, and end processing module, later, I found that such a design was too rigid in the software molding process, and my program was not a very large project, and the whole program had only about 2000 lines of code, therefore, the module design in the early stage is saved, and the code is directly carried out. In the end, there is only one main. c main module program, all other logic functions are implemented by main. h. h defines the global variable, resource. the H file is a file automatically generated by the resource editor and records the ID macro definitions of various resources.

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image006 [4] "border =" 0 "alt =" clip_image006 [4] "height =" 319 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR425Q-11.jpg "/>

AI Design:

The core part of computer black and white games is the search algorithm, which actually reflects the calculation process of the black and white Games program. The quality of search algorithms directly affects the speed and playing power of the program. Next we will start with the most basic search algorithm.

For the situation shown in figure 1, there are three moves for white play: D6, F4, and F6. How can the black and white play program find the best moves?

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image007 "border =" 0 "alt =" clip_image007 "height =" 322 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR4F14-12.jpg "/>
Figure 1 white first, three-step move

In fact, the process of playing chess in a program is very similar to that of a human player. It first searches for all the chess steps in the current situation, computes the situation after each chess step changes, and based on the results of various situations, find the best game for you. Let's take a look at how the program calculates a game. In Figure 1, if Bai Qi goes to D6, F4, and F6 respectively, three situations will be formed: Figure 2, Figure 3, and figure 4.

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image008 [4] "border =" 0 "alt =" clip_image008 [4] "height =" 328 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR42222-13.jpg "/>
Figure 2 blackfirst, with a valuation of 0

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image009 "border =" 0 "alt =" clip_image009 "height =" 337 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR46306-14.jpg "/>
Figure 3 blackfirst, estimated value + 6

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image010 [4] "border =" 0 "alt =" clip_image010 [4] "height =" 336 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR4F01-15.jpg "/>
Figure 4 blackfirst, with a valuation of 0

The program will evaluate the three situations separately to see if the situation is advantageous or inferior? The evaluation process is generally completed by the valuation function. The valuation function will give an evaluation value indicating the merits and demerits of the situation based on the distribution of the pieces on the disk. This is called the valuation. In general, when the valuation is positive, it indicates that the situation is dominant. The larger the positive value, the greater the advantage. Otherwise, a negative value indicates a disadvantage. For example, the situation in Figure 3 is slightly dominant for black games, and the program may provide a valuation of + 6. For Figure 2 and Figure 4, the valuation of both sides is 0 due to the balance of the situation. Because the advantages and disadvantages of the situation are relative, it is advantageous for one party. From the other side, it will be the opposite result.

Valuation can have different value ranges. The Valuation of many programs is [-64, + 64], so as to intuitively express the evaluation of the final score, for example, "a value of + 6" indicates that the current situation has the advantage of winning 6.

By evaluating the situation formed by these three moves, the program can obtain the valuation of each step, as shown in Figure 5 ). At this time, the player is playing white games, and the valuation of F4 has become a negative value. In other words, F4.

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image011 "border =" 0 "alt =" clip_image011 "height =" 341 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR45c5-16.jpg "/>
Figure 5 valuation of the three-step game

Obviously, in the face of various possible chess moves and their valuation, the chess player will eventually choose the game with the largest valuation, that is, the game that is most advantageous to him. Therefore, white chess chooses D6 or F6 instead of F4. The search process of the program is to maximize the valuation to find the best game.

I designed three levels of AI for this program. The first level of AI uses the simplest valuation function, which uses template valuation and the template is fixed, the values in various places on the board are definite, so there is no randomness in the AI of this level. With a definite way, people can easily win it; the second level of AI uses the step in which the flip component can win the most pieces. If there are as many pieces as the flip component, set the random quantity and choose one of them. This design is also very low, because those who know about black and white know that the key to playing black and white games is who can seize four corners first, because once a corner is snatched, the color will not change, therefore, the value of the corner should be the highest, and the value of the side should also be very high, and the value of the side should be different from each other, which is reflected in my valuation template. The third level of AI was originally intended to use deep search in multiple steps to achieve more accurate valuation, and the α-β pruning algorithm was used for optimization, but the time was insufficient, so it is not perfect.

Function call relationship diagram:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image002 [6] "border =" 0 "alt =" clip_image002 [6] "height =" 576 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR4D62-17.jpg "/>

Main Interface:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'style = "border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clip_image002 [8] "border =" 0 "alt =" clip_image002 [8] "height =" 267 "src =" http://www.bkjia.com/uploads/allimg/131228/1JR45058-18.jpg "/>

 

Because the code is too long, I will not post it here, and friends who need it can leave their contact information. If you have a friend who has worked on the design of the black and white games, you can talk to me. I also want to try the MFC design in a short time.

This article from the "cainiao surfaced" blog, please be sure to keep this source http://rangercyh.blog.51cto.com/1444712/394825

Related Article

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: info-contact@alibabacloud.com 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.