int Swap(int i, int j, List L) { Position P1 = PositionOfIndex(i, L); Position P2 = PositionOfIndex(j, L); if(!P1 || !P2) return 0; SwapPosition( P1, P2, L); return 1; }
bool HelloWorld::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event) { auto touchPoint = touch->getLocation(); log("onTouchBegan id =%d, x = %f, y = %f", touch->getID(), touchPoint.x, touchPoint.y); for (int i = 0; i < 8; i++) { bool bTouch = sildePuzzle[i]->getBoundingBox().containsPoint(touchPoint); if (bTouch) { int tagNum = sildePuzzle[i]->getTag(); SwapPosition(tagNum, i); log("sprite touch %d",i); log("null Position %d", nullPosition); log("%d", sildePuzzle[i]->getTag()); } } return true; }
void HelloWorld::initSuffle() { Vec2 randVec[9] = { puzzlePosition[8],puzzlePosition[0], puzzlePosition[1],puzzlePosition[2],puzzlePosition[3], puzzlePosition[4],puzzlePosition[5],puzzlePosition[6],puzzlePosition[7] }; Vec2 emptyCoord; int n = 0; srand((int)time(NULL)); while (n < 5000) { n++; auto randTouch = emptyCoord + randVec[rand() % 8]; log("%d (%f, %f)", n, randTouch.x, randTouch.y); for (int i = 0; i < 8; i++) { if (sildePuzzle[i]->getBoundingBox().containsPoint(randTouch)) { int tagNum = sildePuzzle[i]->getTag(); SwapPosition(tagNum, i); } } } }