大佬教程收集整理的这篇文章主要介绍了cocos2d 简单消除游戏算法 (一),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
enum MatchItemType{ kRedItem = 0,//0 kGreenItem,//1 kBlueItem,//2 kWhiteItem,//3 kOrangeItem //4 }; MatchItemType getOneRandomTypeExceptParameter(const MatchItemType& typE){ MatchItemType allType[5] = {kRedItem,kGreenItem,kBlueItem,kWhiteItem,kOrangeItem}; std::vectorrestType; 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 findThreeSameInX = 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){ //左边两个是同色 if( map.Get(x - 1,y) == map.Get(x - 2,y)){ //need find a new type findThreeSameInX = true; }else{ findThreeSameInX = false; } }else{ findThreeSameInX = false; } if(y >= 2){ //上面两个是同色 if(map.Get(x,y - 1) == map.Get(x,y -2)){ //need find a new type; findThreeSameInY = true; }else{ findThreeSameInY = false; } }else{ findThreeSameInY = false; } if(findThreeSameInX == false && findThreeSameInY == falsE){ //do nothing }else if(findThreeSameInX == true && findThreeSameInY == falsE){ randomType = getOneRandomTypeExceptParameter(map.Get(x - 1,y)); }else if(findThreeSameInX == 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; }
//case 1 /////[x]//////[x]//////// //[x][o][x][x][o][x]///// /////[x]//////[x]//////// ///////////////////////// //case 2 ////////[x]////////////// /////[x][o][x]/////////// ////////[x]////////////// ////////[x]////////////// /////[x][o][x]/////////// ////////[x]//////////////
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 - 1)); return result; } } if(x + 2 < maxX && y - 1 >= 0){ if(map.Get(x + 2,y)); result.push_BACk(map.Get(x + 2,y - 1)); return result; } } if(x + 3 < maxX){ if(map.Get(x + 3,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 + 1)); return result; } } if(x - 1 >= 0 && y + 1 < maxY){ if(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)); 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 + 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 + 3)); return result; } } if(x - 1 >= 0 && y + 2 < maxY){ if(map.Get(x - 1,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)); return result; } } } } } } return result; }
算法先到这里... 后续有时间再更新...
提交给苹果,审核居然能通过
以上是大佬教程为你收集整理的cocos2d 简单消除游戏算法 (一)全部内容,希望文章能够帮你解决cocos2d 简单消除游戏算法 (一)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。