The following content is extracted from Baidu:
2048 is a popular digital game. The original version 2048 was first released on github, and the original author was Gabriel Cirulli. It is developed based on "1024" and "Little 3 Legend ".
A new digital game. Later, various versions were available in 2048, using various platforms. The versions transplanted from Ketchapp to IOS are the most popular, with about 10 million downloads.
The original version is exactly the same. The most famous derivative version is the 2048 hexagonal version, which ranked among the top 200 board games in 81 countries around the world. The Android version is very popular with challenge 2048, and its version 2.0.0 has also joined the dual-player competition. The second most special version is the 2048 Chinese dynasty version. The 2048 custom version allows you to customize text and images. 2048 ~ Yes
A popular one in IOS.
The game rule is simple. You can choose one of the top, bottom, and left to slide. Every time you slide, all the digital blocks will move closer to the sliding direction, and the system will be blank.
A number box appears in a random number. blocks with the same number will be added when they are close together and collided. The number square given by the system is either 2 or 4. players need to find a solution within this small 16-frame range.
"2048. The screen of the game is very simple. In the beginning, most of the 16 squares are gray. When a player's puzzle shows a number, the color and overall style change.
It is very simple. The gameplay rules are also very simple. At the beginning, there will be two or four numbers in the square. Players only need to move the numbers in one of the top, bottom, and left directions.
Some numbers will move closer to the sliding direction, and a random number will appear in the sliding blank square. When the same number is collided, it will move closer together.
Even if the number 2048 is successfully pieced together. If you are a digital enthusiast or a talented mathematical genius, even if you are not a mathematical genius, average players can play around with this
A game. If you are interested, download it and try it out.
CSDN already has a lot of blog posts about 2048, but it is basically based on the cocos2d version. In fact, as a game, cocos2d development is indeed fast. To put it bluntly
It is a replica of a three-class elimination game. However, this article describes Android-based development ideas and is suitable for users who are working on Android projects.
How to implement it? Let's take a look at the 2048:
The core is the middle panel, which is an NXN grid. When moving, it calculates, merges, and adds cells.
1. Determine View implementation
1. GridLayout is preferred for layout. I have to say that this new control of 4.0 is a good news for Android development. If you are not familiar with it, please Google it.
2. FrameLayout is the preferred choice for Cell Layout. A small lattice contains a TextView or ImageView.
Ii. Algorithm Implementation
1. Generate numbers 0, 2, 4, 8 ...... If it is 0, no number is displayed. If it is another number, the base color is determined based on the number.
2. Implement a sliding event between the upper, lower, and left sides. The basic principle is to judge each row (column) first, so that the moving sequence will not have 0 in the middle, the four values are stored in a list,
Based on Game rule calculation, return to this line (column), which is abstract. We can look at it as follows:
2 2 0 4 if this is a row, we move it to the left, first store 2 2 0 4 into a list (remove 0 ), that is, if list is 2 2 4 and then merged according to rules 2 and 2, list is 4 4,
Finally, this row (column) is returned, so you do not know if it is clear.
Why? In fact, there are many 2048 algorithms. You can also think about them. I have also read those cocos2d algorithms and found some problems: too many loops,
There are too many judgments, and the biggest problem that may be caused is the card. Some people may have played 2048 of the Android version. The smoothness is consistent with the reality of iOS.
The difference is more than a little, so we need to improve the efficiency of the algorithm. This algorithm requires a maximum of 16 cycles and 16 reads/writes each time, compared to a certain version in 16 cycles
To improve the efficiency.
3. Add a randomly generated Item, and record a matrix state before sliding during sliding. One is to determine whether the sliding is over or not, and the other is to perform the revocation function,
The program controls a list of record spaces and recalculates existing spaces each time.
4. Judge the end. If the space is not 0, the game will never end. If the space is 0, determine whether each line is adjacent to the same number, and each column is adjacent to the same number.
Iii. Others
1. New Game
2. Score record
3. Undo the last move
4. Customize the 2048 background image
As mentioned above, the basic ideas are well organized. Next we will start to implement them step by step.
The PS source code is being improved. If you need the source code, please leave a message and send it to your friends.