In the previous article on the basis of a simple digital board game learning, address: http://www.cnblogs.com/fwst/p/3706483.html
Running effect:
(4x4)
(7x7)
(1) the basic functions of 2048 games have been completed. The key issues to be solved are:
A. The first is the data structure. First, define the rectangle class, and then define the two-dimensional array of the rectangle class object. The length is defined by a macro and can be modified to customize the game to N * n. In this way, the game is composed of N * n rectangular objects.
B. Game logic processing is the most important part. Direction key response. The logic of the four direction keys on the keyboard is the same, and the code is only slightly modified. This part of the logic is a bit tangled. There should be multiple methods. Here I will introduce my processing. You can share different methods. Take the left button as an example. The processing steps of this logic for each row are as follows:
I. Clear spaces and move all numbers to the leftmost order. Each rectangle has a value. When the value is 0, it is not displayed. These rectangles are spaces. For example, if it is 0 2 0 2 at the beginning, it should be
2 2 0 0;
Ii. from the left, the values of the rectangle that are equal to the adjacent values on the right are doubled, and the adjacent values are set to 0. For example, 2 2 0 0, after processing, it should be 4 0 0 0; then, for example, 2 2 2 2, after processing, it should be 4 0 4 0; for example, after 4 2 2 8 processing, the value is 4 4 8 0;
Iii. Perform the first step. This step is to deal with the new space after the second step. For example, step 2, step 2, step 2, is 4 0 4 0. After this step, the result is 4 4 0 0.
C. generate a new number. After each action, a new number is generated. Originally, 2 or 4 was generated in the game. I used 2 directly here, which is less difficult than the original game, it is easy to generate 4. Just add a probability to generate it randomly. When the value of the generated position is not 0, it is re-generated randomly until the value of the random position is 0.
In addition, a judgment must be added before generation, that is, if the recent buttons do not cause changes on the game panel, new numbers cannot be generated.
D. Determine the end of the game. With a global function, the condition for the game to end is that there are spaces on the game disk or no spaces and the values of any two adjacent numbers are different, adjacent here refers to the adjacent rows and columns. Here we use three loops. The first loop determines whether there is space. If there is space, the game is definitely not over, and the function returns false directly. The second loop is to judge two adjacent values from the row direction. Similarly, the third loop is determined from the column direction. When the game ends, the MessageBox dialog box is displayed.
(2) what has not been done
A. You can use a rounded rectangle.
B. The color of the interface is too colorful and eye-catching. It can be converted into numbers from small to large, and the color ranges from light to deep.
C. No scoring function.
D. Do not start again. No further steps are taken.
E. You can drag the box size on the interface...
Source code:
2048 games based on the MFC dialog box