标签:style blog http color io os ar for sp
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)); 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; }
算法先到这里... 后续有时间再更新...
http://www.waitingfy.com/archives/1335
标签:style blog http color io os ar for sp
原文地址:http://blog.csdn.net/fox64194167/article/details/40213103