提案を学習するための単純なマッチ 3 ゲームを構築しています。今、宝石がある方向 (X または Y) に移動したときの単純なゲームのセクションがあり、周りの宝石が
同じで、1 つ以上連続しているかどうかを調べる必要があります。あまりスマートではないソリューションをプログラムし、それをより一般的にするためのアイデアを探しています。これはコメント付きの私のコードです:
/
*
detect and store the gems that are alike the selected gem
*/
bool GameController::matchDetector(Gem* pSelected)
{
// get the gem that are near the selected gem based on the selected gem movement type
// for example if moved right the movement type is (RightMovment) so the next gem is row,col+1
Gem* pNextSprite = getNextGem(pSelected,pSelected->getGemState());
// array that will store the Gems that are found for each direction array of its own
CCArray * rightGemsToRemove = CCArray::create();
CCArray * leftGemsToRemove = CCArray::create();
CCArray * upperGemsToRemove = CCArray::create();
CCArray * downGemsToRemove = CCArray::create();
if(pNextSprite == NULL)
{
return false;
}
//copy the selected NEXT gem to the selected gem
//so the calculation to find the next gems will be right
pSelected->swap(pNextSprite);
int col = pSelected->getColNum();
int row = pSelected->getRowNum();
/*
its long switch case that on its option doing the same so only the first one commented
*/
switch(pSelected->getGemState())
{
case kMoveRight:
{
// if its right direction i need to run on all the right gems until its NOT the same and stor it
for(int i=pSelected->getColNum()+1;i < maxGemsInCol;i++)
{
std::string nextInnerSpriteId = pUT->setGemId(i,row);
// get the next gem from the container
Gem* pNextInnerSprite = (Gem*)GameSingleTone::getInstance()->getGemsDictionary()->objectForKey(nextInnerSpriteId);
if(pNextInnerSprite == NULL)
{
return false;
}
else
{
// add it to the container
rightGemsToRemove->addObject(pNextInnerSprite);
}
}
break;
}
case kMoveLeft:
{
for(int i=pSelected->getColNum()-1;i < maxGemsInCol;i++)
{
std::string nextInnerSpriteId = pUT->setGemId(i,row);
Gem* pNextInnerSprite = (Gem*)GameSingleTone::getInstance()->getGemsDictionary()->objectForKey(nextInnerSpriteId);
if(pNextInnerSprite == NULL)
{
return false;
}
else
{
leftGemsToRemove->addObject(pNextInnerSprite);
}
}
break;
}
case kMoveUp:
{
for(int i=pSelected->getRowNum()+1;i < maxGemsInRow ;i++)
{
std::string nextInnerSpriteId = pUT->setGemId(col,i);
Gem* pNextInnerSprite = (Gem*)GameSingleTone::getInstance()->getGemsDictionary()->objectForKey(nextInnerSpriteId);
if(pNextInnerSprite == NULL)
{
return false;
}
else
{
upperGemsToRemove->addObject(pNextInnerSprite);
}
}
break;
}
case kMoveDown:
{
for(int i=pSelected->getRowNum()-1;i < maxGemsInRow ;i++)
{
std::string nextInnerSpriteId = pUT->setGemId(col,i);
Gem* pNextInnerSprite = (Gem*)GameSingleTone::getInstance()->getGemsDictionary()->objectForKey(nextInnerSpriteId);
if(pNextInnerSprite == NULL)
{
return false;
}
else
{
downGemsToRemove->addObject(pNextInnerSprite);
}
}
break;
}
}
/*
this function will run on all the arrays and will check which gems needs to be removed from the GRID and all the rest ( fill the grid with new gems and so on .. )
*/
handleGems(downGemsToRemove,upperGemsToRemove,leftGemsToRemove,rightGemsToRemove)
}