This interestingC ++The series will show that using C ++ to write code can be as efficient and interesting as other mainstream languages. In part 2, I will show you how to create a well-known game from scratch using C ++. This article and the entire series are intended for developers who want to learn C ++ or are curious about the language's performance.
Many young people want to learn programming to write games. C ++ is the most widely used language for game writing, although it requires a lot of programming experience before writing the next Angry Bird. A game is a good choice to start learning. In fact, after I started learning C ++ many years ago, he was a game I wrote. I hope this article will help beginners and experienced developers who are not familiar with C ++.
I am using Visual Studio 2012 to write the source code of this article.
Game Introduction
If you have never played a word game or are not familiar with it, the following is a description from Wikipedia.
| Word game (or "circle and cross", Xs and OS) is a two-person pen and paper game. Two people draw circles and forks in a 3x3 grid in turn. when a player places a logo horizontally, a line vertical or diagonal line is formed to win. |
This game can also be used for man-machine combat, and the first hand is not fixed.
When creating this program, there are two key things: The program logic and the program UI interface. there are many methods for creating user UI in windows, including Win32 API, MFC, ATL, GDI +, DirectX, etc. in this article, I will show how to use multiple technologies to implement the same program logic. we will create two new applications, one using Win32 API and the other using C ++/CX.
Game Logic
If a player follows a few simple rules when placing a tag on the grid, then he can play a perfect game that means a win or a draw ). With these rules written on Wikipedia, you can also find the optimal strategy for first-hand players.
On xkcd drawing, there is an optimal strategy for first-hand and later-hand players. Although there are several errors that fail to take a winning step in several cases, at least one X mark is lost in one case ), I will use this version as a game policy to fix the errors I can find ). Remember that computers always play a perfect game. If you implement such a game, you may also want users to win. In this case, you need a different method. This policy should be sufficient for the purpose of this Article.
The first question raised is what data structure is used in the C ++ program to represent the image model. This can have different options, such as trees, graphs, arrays, or bit fields. If someone really cares about memory consumption ). The grid has nine units. The simplest use I have selected is to use an array containing nine integers for each unit: 0 indicates an empty unit, and 1 indicates that the Unit is marked as X, 2 indicates that the Unit is marked as O. Let's see how it will be encoded.
This figure can be understood as follows:
- Place X in the unit 0, 0. The grid can be encoded as:, 0, 0, 0, 0
- If the opponent places O in unit 0, 1), X in Unit 1, 1. The current mesh encoding is:, 0, 0, 0
- If the opponent places O in the unit 0, 2), then X in the unit 2, 2. The current mesh encoding is: 1, 2, 0, 1, 0, 0, 1
- If the opponent places O in Unit 2, 2), X in Unit 2, 0. The current mesh encoding is:, 2. At this time, no matter what the opponent does, X will win the game.
- If the opponent places O in the unit 0, 2), then X in the Unit 1, 0. The current mesh encoding is: 1, 2, 1, 1, 0, 1, 0, 2. This indicates a step to win the game.
Remember this, we can start coding it in the program. We will use a std: array to represent a 9-pane. This is a fixed-size container that is known during compilation and stores elements in contiguous memory areas. To avoid using the same array type over and over again, I will define an alias to simplify it.
- #include <array>
-
- typedef std::array<char, 9> tictactoe_status;
The optimal policy described above is represented by another array of such an array Queue.
- tictactoe_status const strategy_x[] =
- {
- {1,0,0,0,0,0,0,0,0},
- {1,2,0,0,1,0,0,0,0},
- {1,2,2,0,1,0,0,0,1},
- {1,2,0,2,1,0,0,0,1},
- // ...
- };
-
- tictactoe_status const strategy_o[] =
- {
- {2,0,0,0,1,0,0,0,0},
- {2,2,1,0,1,0,0,0,0},
- {2,2,1,2,1,0,1,0,0},
- {2,2,1,0,1,2,1,0,0},
- // ...
- };