Automatic removal of cocos2d-x3.6 connections
I have already completed the main logic of the continuous view. In this section, I will explain how to automatically remove it. What should I do if the board becomes dead.
As mentioned in the previous chapter, the automatic elimination algorithm is also based on the connection algorithm.
The principle is very simple: Two-layer for loop, scan the Board to determine whether there is a connection. If there is a connection path, add a connection path. If there are no two points that can be connected to the whole board, it is dead.
The solution here is to randomly disrupt the current Board and re-layout it.
First look at the dead board algorithm:
bool GameScene::die(){ for (int y = 1; y < yCount; y++) { for (int x = 1; x < xCount; x++) { if (mMap[x][y] != 0) { for (int j = y; j < yCount - 1; j++) { if (j == y) { for (int i = x + 1; i < xCount - 1; i++) { if (mMap[i][j] == mMap[x][y] && link(Vec2(x, y), Vec2(i, j))) { return false; } } }else{ for (int i = 1; i < xCount - 1; i++) { if (mMap[i][j] == mMap[x][y] && link(Vec2(x, y), Vec2(i, j))) { return false; } } } } } } } return true;}
In fact, it is just a few traversal. Each vertex on the board is connected to the rest for judgment and scanning by row.
Because the link function adds the connected vertex to the path container, once the die function returns false, the connected path vertex is saved in the path container.
If the die is used, the Board must be randomly changed.
if (die()) { changeMap(); }
Let's see changeMap ():
Void GameScene: changeMap () {// Random Seed srand (unsigned int) time (NULL); // a temporary variable used to exchange data in two locations: int tempX, tempY, tempM; // traverses the map array and randomly exchanges positions for (int x = 1; x <xCount-1; x ++) for (int y = 1; y <yCount-1; y ++) {tempX = 1 + (int) (CCRANDOM_0_1 () * (xCount-2); tempY = 1 + (int) (CCRANDOM_0_1 () * (yCount-2); tempM = mMap [x] [y]; mMap [x] [y] = mMap [tempX] [tempY]; mMap [tempX] [tempY] = tempM; // switch the location of the genie, switch the tag value int tag1 = (yCount-2) * (x-1) + y; int tag2 = (yCount-2) * (tempX-1) + tempY; auto pos1 = indextoScreen (x, y); auto pos2 = indextoScreen (tempX, tempY ); auto pic1 = getChildByTag (tag1); auto pic2 = getChildByTag (tag2); if (pic1) {pic1-> setPosition (pos2); pic1-> setTag (tag2 );} else {// if it is null, set the value of the map array to 0 mMap [tempX] [tempY] = 0;} if (pic2) {pic2-> setPosition (pos1 ); pic2-> setTag (tag1);} else {mMap [x] [y] = 0 ;}}}
I put a button on the UI and click the button to call the autoClear () function, which is the automatic clearing mechanism.
auto tip = Sprite::createWithTexture(textureCache->getTextureForKey(s_game_leisure)); auto menuItemSprite = MenuItemSprite::create(tip, tip, CC_CALLBACK_1(GameScene::autoClear, this)); auto menu = Menu::create(menuItemSprite, nullptr); menu->setAnchorPoint(Vec2(1, 1)); menu->setPosition(wSize.width - 100, wSize.height - 100); addChild(menu);
Here is simple. There is nothing to say. Let's look at the autoClear function:
Void GameScene: autoClear (Ref * runner der) {// animated auto rote = RotateBy: create (0.05, 20); auto seq = Sequence: create (rote, rote-> reverse (), rote-> reverse (), rote-> clone (), nullptr); (Sprite *) vertex der)-> runAction (seq ); if (die () {CCLOG (die -----); changeMap () ;}else {drawLine ();}}
Well, it's that simple. If the die is used, the Board will be changed. If there is no die, the line will be drawn.
Let's talk about this die (). if the die is true when the if (die () is executed, changeMap has nothing to say. if the die returns false, at this time, the Board has actually been traversed and the accessible path is put in the member variable mPath, so you can directly drawLine.
Okay. Now, the connection is complete.