Cocos2d simple elimination of game algorithms (I), cocos2d Algorithms
1. Game video demonstration
2. My understanding about three million games
In the game in the video above, I spent two weeks, only a simple Demo, bug, and special effects are almost none. I feel that the three-game relies mainly on grinding, and the more refined the product. There are already more games on the market. The mainstream is map-based, almost endless mode, all kinds of special effects are eliminated, all kinds of customs clearance methods are good to play, that is, it is difficult to encounter the level, you have to try it several times, if you're lucky, it's time to stop. The real extension of this game is the pass mode. It also requires a special map editor to work with the planning and constantly upgrade the game.
3. Eliminate the simple algorithms involved
3.1 generate random MAP algorithm
There are a variety of maps. Here we use the simplest rectangle. Requirement: 1. This algorithm must generate a random map. Three maps must be horizontal or vertical. 2. This map can be removed step by step (not a dead map)
It was quite difficult to see this requirement at first. I thought about the next 2nd requirements. I should leave it alone first. If it was a dead map, I would just generate a new map. After testing, the probability of generating a dead map is very low.
Algorithm Implementation Description: assume that the map (0, 0) is in the upper left corner. Very simple x is generated from the far left to the right, and y is generated from the top to the bottom. Determine whether the two on the left are in the same color and whether the two above are in the same color. If they are in the same color, remove the color. Assume that the generated map is: 2, 3, 3, 4, 1, 3, 21, 2, 3, 4, 3, 31, 2, 4, 2, 2, X because the two on the left of X are both 2, so X can no longer be 2, and the two on it are both 3, so X can no longer be 3. Therefore, the result of X can only be 0, 1, and 4 randomly.
The following is a pseudo-code (the real Code cannot be run ):
Enum MatchItemType {kRedItem = 0, // 0 kGreenItem, // 1 kBlueItem, // 2 kWhiteItem, // 3 kOrangeItem // 4}; MatchItemType equals (const MatchItemType & type) {MatchItemType allType [5] = {kRedItem, kGreenItem, kBlueItem, kWhiteItem, kOrangeItem}; std: vector
RestType; for (int I = 0; I <5; ++ I) {if (allType [I]! = Type) {restType. push_back (allType [I]);} int restSize = restType. size (); int randomIndex = rand () % restSize; return restType [randomIndex];} Array2D <MatchItemType> getOneRandomMapArray () {Array2D <MatchItemType> map = Array2D <MatchItemType> (7, 7); bool findthreesameparameters = false; bool findThreeSameInY = false; for (int y = 0; y <7; ++ y) {for (int x = 0; x <7; ++ x) {MatchItemType randomType = (MatchItemType) (rand () % 5 ); if (x> = 2) {// the two on the left are the same color if (map. get (x-1, y) = map. get (x-2, y) {// need find a new type findthreesame=true;} else {findthreesame== false ;} if (y> = 2) {// the above two are the same color if (map. get (x, y-1) = map. get (x, y-2) {// need find a new type; findThreeSameInY = true;} else {findThreeSameInY = false ;} if (findthreesamething == false & response = false) {// do nothing} else if (findThreeSameInX == true & findThreeSameInY = false) {randomType = map. get (x-1, y);} else if (findthreesame== false & findThreeSameInY = true) {randomType = getOneRandomTypeExceptParameter (map. get (x, y-1);} else {randomType = getOneRandomTypeExceptParameter (map. get (x-1, y), map. get (x, y-1);} map. set (x, y, randomType) ;}} return map ;}
3.2 determine whether a map is a dead Map
If the whole map is moved, it cannot be eliminated in any step, that is, the dead map is generated again.
//case 1/////[x]//////[x]//////////[x][o][x][x][o][x]//////////[x]//////[x]///////////////////////////////////case 2////////[x]///////////////////[x][o][x]///////////////////[x]//////////////////////[x]///////////////////[x][o][x]///////////////////[x]//////////////
Here we use annotations to draw two simple cases. Pay attention to the position of x. Case1 is horizontal with two colors, one move can eliminate only six possibilities, three on the left and three on the right. The following figure shows two identical colors. There are also six situations where one step can be removed. The three types above and the three types below. By knowing this, the code is easy. Remember to find one and return it directly.
vector<MatchItem*> getThreeMatchItemCanRemoveByOneStep(const Array2D<MatchItem*> & map){ vector<MatchItem*> result; int maxX = 7; int maxY = 7; for(int y = 0; y < maxY; ++y){ for(int x = 0; x < maxX; ++x){ if(x + 1 < maxX){ //case 1 if(map.Get(x, y)->getType() == map.Get(x + 1, y)->getType()){ MatchItemType currentType = map.Get(x, y)->getType(); //check 6 item, one move one step can combine three same item if(x - 2 >= 0){ if(map.Get(x - 2, y)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x + 1, y)); result.push_back(map.Get(x - 2, y)); return result; } } if(x - 1 >= 0 && y - 1 >= 0){ if(map.Get(x - 1, y - 1)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x + 1, y)); result.push_back(map.Get(x - 1, y - 1)); return result; } } if(x + 2 < maxX && y - 1 >= 0){ if(map.Get(x + 2, y - 1)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x + 1, y)); result.push_back(map.Get(x + 2, y - 1)); return result; } } if(x + 3 < maxX){ if(map.Get(x + 3, y)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x + 1, y)); result.push_back(map.Get(x + 3, y)); return result; } } if(x + 2 < maxX && y + 1 < maxY){ if(map.Get(x + 2, y + 1)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x + 1, y)); result.push_back(map.Get(x + 2, y + 1)); return result; } } if(x - 1 >= 0 && y + 1 < maxY){ if(map.Get(x - 1, y + 1)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x + 1, y)); result.push_back(map.Get(x - 1, y + 1)); return result; } } } } if(y + 1 < maxY){ MatchItemType currentType = map.Get(x, y)->getType(); //case 2 if(map.Get(x, y)->getType() == map.Get(x, y + 1)->getType()){ if(y - 2 >= 0){ if(map.Get(x, y - 2)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x, y + 1)); result.push_back(map.Get(x, y - 2)); return result; } } if(x + 1 < maxX && y - 1 >= 0){ if(map.Get(x + 1, y - 1)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x, y + 1)); result.push_back(map.Get(x + 1, y - 1)); return result; } } if(x + 1 < maxX && y + 2 < maxY){ if(map.Get(x + 1, y + 2)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x, y + 1)); result.push_back(map.Get(x + 1, y + 2)); return result; } } if(y + 3 < GameGlobal::xMapCount){ if(map.Get(x, y + 3)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x, y + 1)); result.push_back(map.Get(x, y + 3)); return result; } } if(x - 1 >= 0 && y + 2 < maxY){ if(map.Get(x - 1, y + 2)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x, y + 1)); result.push_back(map.Get(x - 1, y + 2)); return result; } } if(x - 1 >= 0 && y - 1 >= 0){ if(map.Get(x - 1, y + 1)->getType() == currentType){ //find one result.push_back(map.Get(x, y)); result.push_back(map.Get(x, y + 1)); result.push_back(map.Get(x - 1, y - 1)); return result; } } } } } } return result;}
It seems a little complicated. in 12 cases, this algorithm should be fast. This algorithm is also used in the elimination of Games. The user has not been eliminated for a long time and should be prompted. You can use this algorithm to find the three ones that can be removed step by step.
The algorithm will be updated first here...
Http://www.waitingfy.com/archives/1335
A simple algorithm question to answer
Separated from inside and out
For I want 1 to n
@ X then x + 1
End
1 + 1 + 1 +... + 1 = n times
For j between 1 to n
For I want 1 to j
@ X then x + 1
End
End
A (n) = n
SA (n) = n * (n + 1)/2
For I want 1 to n
For j branch 1 to I
For k limit 1 to j
@ X then x + 1
End
End
End
B (n) = SA (n)
SB (n) = B (1) + B (2) +... + B (n)
......
This is the way
==========================================
1) n * (n + 1)/2
2) Change to Solution
3) change to Solution
==========================================
2), 3) I cannot give the result, because, obviously, I am not a computer
Want to use cocos2d-x to write a game with checkpoints, help train of thought
A level is a scenario where different classes can be written or the same class can be written. To see the differences between personal programming habits and levels, the pause button should be re-addchild for each scenario, you can write these statements as a function to facilitate the call.