連連看演算法--cocos2d-x 環境下開發

你的財神爺發表於2018-01-16

先上一張圖給大家看看

當大家看到這張圖片的時候會看到周圍一圈對號,這個是我做標記用的,當然 真正的開發遊戲過程中 這些對號是不存在的。我這裡想給大家說明的是 假如我們的 地圖是14*8的 那麼其實真正的地圖佈局應該是16*10 為什麼要這麼做呢 那是因為我們要讓我們所有的路線都從我們規定的方格內經過,不能越界。如果你是14*8 的那麼當四邊上有可以連通的時候 就要做大量的判斷來處理。那麼為什麼我們不避免這些問題呢。這樣我們在規劃路線的時候 就不存在了特殊的情況。
   不知道大家是否明白我說的什麼意思。如果不懂的話可以給我留言我會給大家回覆 解答。
   關於圖片的生成我這裡說一下 如何保證所有的圖片 都是成對出現呢 這裡有個前提條件就是我們的地圖佈局一定要成偶數。那麼我們在生出圖片的 我們每一次取出一張圖片生成兩個一模一樣的 這樣就保證地圖裡面所有的圖片肯定是成對出現的。
  主要程式碼是這樣的

[cpp] view plain copy
  1. int totalcout=this->row*this->cloum;  
  2.               CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("point.plist","point.png");  
  3.               CCArray* temparry=CCArray::create();  
  4.               // 一次生成兩張一樣的  
  5.               for (int i=0;i<totalcout/2;i++){  
  6.                       int num=CCRANDOM_0_1()*35;//因為是圖片的張數是從0到36張 所以隨機從0 到35  
  7.                       CustomSprite*  phit1=  CustomSprite::createWithSpritePicIndex(num,CCSize(picWidth,picHeight));                  
  8.                       CustomSprite*  phit2=  CustomSprite::createWithSpritePicIndex(num,CCSize(picWidth,picHeight));                  
  9.                       temparry->addObject(phit1);  
  10.                       temparry->addObject(phit2);  
  11.               }  

 是不是很簡單啊
   至於如何把生成的東西給打亂。這個cocos2d-x 提供了很好用的方法那就是從陣列中取出的時候 可以隨機去取出。
  下面把整個地圖佈局的方法給貼出來
[cpp] view plain copy
  1. bool LLKmapLayer::setUpdateView(){  
  2.         bool isRet=false;  
  3.         do {  
  4.                 //得到需要的總張數  // 一定要是偶數  
  5.                 int totalcout=this->row*this->cloum;  
  6.                 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("point.plist","point.png");  
  7.                 CCArray* temparry=CCArray::create();  
  8.                 // 一次生成兩張一樣的  
  9.                 for (int i=0;i<totalcout/2;i++){  
  10.                         int num=CCRANDOM_0_1()*35;//因為是圖片的張數是從0到36張 所以隨機從0 到35  
  11.                         CustomSprite*  phit1=  CustomSprite::createWithSpritePicIndex(num,CCSize(picWidth,picHeight));                  
  12.                         CustomSprite*  phit2=  CustomSprite::createWithSpritePicIndex(num,CCSize(picWidth,picHeight));                  
  13.                         temparry->addObject(phit1);  
  14.                         temparry->addObject(phit2);  
  15.                 }  
  16.   
  17.                 for(int i=0;i<row+2;i++){  
  18.                         for(int j=0;j<this->cloum+2;j++){  
  19.   
  20.                                 if ((i==0)||(j==0)||(j==cloum+1)||(i==row+1)){  
  21.                                         CustomSprite*  temp=  CustomSprite::createWithSpritePicIndex(0,CCSize(picWidth,picHeight));  
  22.                                         temp->displayCustome();  
  23.                                         temp->setX(j);  
  24.                                         temp->setY(i);  
  25.   
  26.                                         temp->setAnchorPoint(ccp(0,0));  
  27.                                         temp->setPosition(ccp(45*(j-1),51*(i-1)));  
  28.                                                 this->addChild(temp);  
  29.                                         this->totalArray->addObject(temp);  
  30.   
  31.                                 }else {  
  32.                                         CustomSprite* phit=(CustomSprite*)temparry->randomObject();  
  33.                                         temparry->removeObject(phit,false);  
  34.                                         phit->setAnchorPoint(ccp(0,0));  
  35.                                         phit->setPosition(ccp(45*(j-1),51*(i-1)));  
  36.                                         this->addChild(phit);  
  37.                                         llkArray->addObject(phit);  
  38.                                         phit->setX(j);  
  39.                                         phit->setY(i);  
  40.                                         this->totalArray->addObject(phit);  
  41.                                 }  
  42.                         }  
  43.                 }  
  44.                 isRet=true;          
  45.         } while (0);  
  46.         return isRet;  
  47. }  

是不是很少的程式碼就實現了這個功能了。
下面就開始我們今天的重頭戲 那就是連連看的主要演算法
首先先說明最簡單 那就是一線貫通。


大家看到我畫的紅線   是連連看中最簡單的 兩種連線方式 
   只要兩張圖片 處於同一水平線或者同一垂直線  如果兩張圖片中沒有別的圖片間隔就代表可以連通消除了
  主要程式碼

[cpp] view plain copy
  1. // 這個是橫向檢測  
  2. CCArray* LLKAlgorithm::horizon(cocos2d::CCArray* llkArray,cocos2d::CCArray* hitArray){  
  3.         CustomSprite* hit1=(CustomSprite*)hitArray->objectAtIndex(0);// 取出第一個  
  4.         CustomSprite* hit2=(CustomSprite*)hitArray->objectAtIndex(1);// 取出第二個  
  5.         int x1=hit1->getX(); // 得到第一個X 座標  
  6.         int x2=hit2->getX(); // 得到第二個X 座標  
  7.         bool isda=x1>x2?true:false;// 得出那個座標肯後  
  8.         int temp=isda?x1-x2:x2-x1; // 獲取兩個兩個點之間的 間隔  
  9.         CCArray* temparray=CCArray::create();// 記錄兩個點之間 所經過的點  
  10.   
  11.         if (temp==1){// 表示兩個是相鄰的兩個  
  12.                 temparray->addObject(hit1);  
  13.                 temparray->addObject(hit2);  
  14.         }else {      
  15.   
  16.                 if (isda){  
  17.                         temparray->addObject(hit1);  
  18.                         temparray->addObject(hit2);  
  19.                         for(int i=1;i<temp;i++){  
  20.                                 CustomSprite* hitteim=LLKAlgorithm::getCustByXandY(llkArray,x1-i,hit2->getY());  
  21.                                 // 如果是已經消除的那麼 就可以繼續查詢  
  22.                                 if (!hitteim->getIsEliminate()){  
  23.                                         LLKAlgorithm::removeAllArray(temparray,false);  
  24.                                         break;  
  25.                                 }else {  
  26.                                         temparray->addObject(hitteim);  
  27.                                 }  
  28.                         }  
  29.   
  30.                 }else {  
  31.                         temparray->addObject(hit2);  
  32.                         temparray->addObject(hit1);  
  33.                         for(int i=1;i<temp;i++){  
  34.                                 CustomSprite* hitteim=LLKAlgorithm::getCustByXandY(llkArray,x2-i,hit2->getY());  
  35.                                 // 如果是已經消除的那麼 就可以繼續查詢  
  36.                                 if (!hitteim->getIsEliminate()){  
  37.                                         LLKAlgorithm::removeAllArray(temparray,false);  
  38.                                         break;  
  39.                                 }else {  
  40.                                         temparray->addObject(hitteim);  
  41.                                 }  
  42.                         }  
  43.                 }  
  44.   
  45.         }  
  46.   
  47.   
  48.         return temparray;  
  49.   
  50. }  
  51. //這個是縱向檢測  
  52. CCArray*  LLKAlgorithm::vertical(cocos2d::CCArray* llkArray,cocos2d::CCArray* hitArray){  
  53.         CustomSprite* hit1=(CustomSprite*)hitArray->objectAtIndex(0);// 取出第一個  
  54.         CustomSprite* hit2=(CustomSprite*)hitArray->objectAtIndex(1);// 取出第二個  
  55.         int y1=hit1->getY(); // 得到第一個Y 座標  
  56.         int y2=hit2->getY(); // 得到第二個Y 座標  
  57.         bool isda=y1>y2?true:false;// 得出那個座標肯後  
  58.         int temp=isda?y1-y2:y2-y1; // 獲取兩個兩個點之間的 間隔  
  59.         CCArray* temparray=CCArray::create();// 記錄兩個點之間 所經過的點  
  60.   
  61.         if (temp==1){// 表示兩個是相鄰的兩個  
  62.                 temparray->addObject(hit1);  
  63.                 temparray->addObject(hit2);  
  64.         }else {      
  65.   
  66.                 if (isda){  
  67.                         temparray->addObject(hit1);  
  68.                         temparray->addObject(hit2);  
  69.                         for(int i=1;i<temp;i++){  
  70.                                 CustomSprite* hitteim=LLKAlgorithm::getCustByXandY(llkArray,hit2->getX(),y1-i);  
  71.                                 // 如果是已經消除的那麼 就可以繼續查詢  
  72.                                 if (!hitteim->getIsEliminate()){  
  73.                                         LLKAlgorithm::removeAllArray(temparray,false);  
  74.                                         break;  
  75.                                 }else {  
  76.                                         temparray->addObject(hitteim);  
  77.                                 }  
  78.                         }  
  79.   
  80.                 }else {  
  81.                         temparray->addObject(hit2);  
  82.                         temparray->addObject(hit1);  
  83.                         for(int i=1;i<temp;i++){  
  84.                                 CustomSprite* hitteim=LLKAlgorithm::getCustByXandY(llkArray,hit2->getX(),y2-i);  
  85.                                 // 如果是已經消除的那麼 就可以繼續查詢  
  86.                                 if (!hitteim->getIsEliminate()){  
  87.                                         LLKAlgorithm::removeAllArray(temparray,false);  
  88.                                         break;  
  89.                                 }else {  
  90.                                         temparray->addObject(hitteim);  
  91.                                 }  
  92.                         }  
  93.                 }  
  94.   
  95.         }  
  96.   
  97.   
  98.   
  99.         return temparray;  
  100. }  

其中返回的 array 就是兩個圖片的之間的路徑  包含兩張圖片在內。

對於兩張圖片是否可以垂直相連或者橫向相連這個我不用多講大家都應該明白怎麼去做。

下面講解一個拐角相連的情況
首先我先上張圖


      看到這張圖片的時候大家先看藍色矩形框裡面 是兩張可以經過一個拐角消除的圖片  但是怎麼確定兩張圖片可以經過一個拐角就可以連線呢 
   大家有沒有發現 這兩個張圖片剛好是矩形的兩個對角頂點 。也就是說圍繞著兩張圖片我們可以畫出一個最小的矩形,而矩形的其中兩個頂點就是這兩張圖片。
   好大家如果理解了這裡 我們就說下面的,如果不理解好好的想一下。既然已知矩形的 其中兩個頂點,那麼另外兩個頂點的座標也就輕鬆的計算出來。就是我們看到的兩個綠色的框。
    我們先拿出最上面的那個綠色的矩形框,我們發現他不是一個空白或者說已經消除的一個圖片 。那麼這個頂點是不符合規則的。我們看下面的那個綠色矩形框。 我們驚奇的發現 他竟然和我們選擇的那兩張(藍色框)圖片能直接連通 也就是通過這個綠色的矩形框(已經消除掉的) 我們可以把他轉化成 一個橫向可以連通和一個縱向可以連通的。
    看到這裡有的人或許已經恍然大悟了。其實我們要向知道兩個點是否可以經過一個拐角相連,我們首先要確定兩個圖片的座標點是可以畫出一個矩形的並且這個矩形的兩個對角頂點就是你選中的兩張圖片。(必須是對角)然後我們找出這兩個矩形另外兩個頂點的座標,找到時候首先要確定找到這個頂點必須是已經消除的掉的一個空白在哪裡,或者說沒有阻礙物。如果找到一個這樣的頂點,那麼只要這個頂點能和我們剛開始選擇的那兩張圖片 形成一個縱向的連通和一個橫向的連通 那麼就說明這兩個點可以經過一個拐角進行相連。
  簡單一點說就是 兩個拐角的==想辦法轉成===》一個縱向連通+一個橫向連通。
  說到這裡不知道大家是否已經明白我所表達的意思。
主要程式碼實現

[cpp] view plain copy
  1. CCArray*  LLKAlgorithm::oneCorner(cocos2d::CCArray* llkArray,cocos2d::CCArray* hitArray){  
  2.         CustomSprite* hit1=(CustomSprite*)hitArray->objectAtIndex(0);// 取出第一個  
  3.         CustomSprite* hit2=(CustomSprite*)hitArray->objectAtIndex(1);// 取出第二個  
  4.         int x1=hit1->getX();  
  5.         int x2=hit2->getX();  
  6.         int y1=hit1->getY();  
  7.         int y2=hit2->getY();  
  8.         bool isXg=x1>x2?true:false;//如果是true 表示hit1 在hit2 的後面  
  9.         bool isYg=y1>y2?true:false;//如果是true 表示hit1 在hit2 的上面  
  10.         // 下面我們計算出矩形的另外的兩個重點  
  11.         // 先計算出  在矩形上面所對應的   
  12.         int temx=0;  
  13.         int temy=0;          
  14.   
  15.         // 求取最上面的哪一個對應的座標點  
  16.   
  17.         if(isYg){  
  18.                 temy=hit1->getY();  
  19.                 temx=hit2->getX();  
  20.         }else {  
  21.                 temy=hit2->getY();  
  22.                 temx=hit1->getX();  
  23.         }  
  24.         CCArray* temparray=CCArray::create();// 記錄兩個點之間 所經過的點  
  25.   
  26.         CustomSprite* hity=LLKAlgorithm::getCustByXandY(llkArray,temx,temy);// 得到最上面的那個對應的矩形頂點的 精靈圖片  
  27.         CCArray* tempHit=CCArray::create();  
  28.         //頂點必須是已經消除的 要不肯定不能貫通  
  29.         if (hity->getIsEliminate()){  
  30.                 tempHit->addObject(hity);  
  31.                 tempHit->addObject(isYg?hit1:hit2);  
  32.                 CCArray* temx=LLKAlgorithm::horizon(llkArray,tempHit);// 獲取X 方向上是否可以連通  
  33.                 if(temx->count()>0){// 表示可以連通  
  34.                         temparray->addObjectsFromArray(temx);  
  35.                         LLKAlgorithm::removeAllArray(tempHit,false);  
  36.                         tempHit->addObject(hity);  
  37.                         tempHit->addObject(isYg?hit2:hit1);  
  38.                         CCArray* temy=LLKAlgorithm::vertical(llkArray,tempHit);  
  39.                         if (temy->count()>0){  
  40.                                 temparray->removeObject(hity,false);  
  41.                                 temparray->addObjectsFromArray(temy);  
  42.                         }else {  
  43.                                 LLKAlgorithm::removeAllArray(temparray,false);  
  44.                         }  
  45.                 }else {  
  46.                         LLKAlgorithm::removeAllArray(tempHit,false);  
  47.                 }  
  48.         }  
  49.         // 表示上面的路走不通  
  50.         if (temparray->count()==0){  
  51.                 // 獲取X 方向矩形所對應的的點                  
  52.                 //temx=isXg?hit1->getX():hit2->getX();  
  53.                 //temy=isXg?hit2->getY():hit1->getY();  
  54.   
  55.                 if (isYg){  
  56.                         temy=hit2->getY();  
  57.                         temx=hit1->getX();  
  58.                 }else {  
  59.                         temy=hit1->getY();  
  60.                         temx=hit2->getX();                  
  61.                 }  
  62.   
  63.                 CustomSprite* hitx=LLKAlgorithm::getCustByXandY(llkArray,temx,temy);// 得到最上面的那個對應的矩形頂點的 精靈圖片  
  64.                 if (hitx->getIsEliminate()){  
  65.                         tempHit->addObject(hitx);  
  66.                         tempHit->addObject(isYg?hit2:hit1);  
  67.                         CCArray* temx=LLKAlgorithm::horizon(llkArray,tempHit);// 獲取X 方向上是否可以連通  
  68.                         if(temx->count()>0){// 表示可以連通  
  69.                                 temparray->addObjectsFromArray(temx);  
  70.                                 LLKAlgorithm::removeAllArray(tempHit,false);  
  71.                                 tempHit->addObject(hitx);  
  72.                                 tempHit->addObject(isYg?hit1:hit2);  
  73.                                 CCArray* temy=LLKAlgorithm::vertical(llkArray,tempHit);  
  74.                                 if (temy->count()>0){  
  75.                                         temparray->removeObject(hitx,false);  
  76.                                         temparray->addObjectsFromArray(temy);  
  77.                                 }else {  
  78.                                         LLKAlgorithm::removeAllArray(temparray,false);  
  79.                                 }  
  80.                         }else {  
  81.                                 LLKAlgorithm::removeAllArray(tempHit,false);  
  82.                         }  
  83.                 }  
  84.   
  85.         }  
  86.   
  87.         return temparray;  
  88.   
  89. }  

下面我們說下拐兩角 可以相連的。


大家看看藍色區域內的是我選擇的兩張圖片。從圖中我們看出這兩張圖片是可以經過兩個拐角相連的。
那麼如何知道可以經過兩個拐角可以 可以相連的 我相信通過上面那一個拐角的演算法 大家或許就可以猜出來了。這裡我詳細說明一下 首先我們拿出一個點  那麼就像這個點的上下左右去找,找到一個 可以經過一個拐角和另外一個點相連的。經過一個拐角的演算法請看上面那個講解。首先找到的這個點可以和我們拿出的這個點縱向相連或者橫向相連 。(但是必須保證這個點已經沒有別的圖片佔位置或者說是一個空白)。如果能找到這樣的一個點那麼就可以說明我們開始選擇的兩個圖片是可以經過兩個拐角進行相連的。
總體來說 可以這樣理解  兩個拐角==轉化成===》(一個橫向相連/縱向相連)+ 一個拐角可以相連的
一個拐角的看上面的解釋。
主要實現程式碼

[cpp] view plain copy
  1. CCArray*  LLKAlgorithm::twoCorner(cocos2d::CCArray* llkArray,cocos2d::CCArray* hitArray){  
  2.   
  3.         bool isjiance=false// 這個主要來表示是否走了 一個拐角的判斷  
  4.         // 先獲取 當前X 的最大是多少  y 的最大是多少  
  5.         CustomSprite* lastCutom=(CustomSprite*)llkArray->lastObject();  
  6.         int max=lastCutom->getX();  
  7.         int may=lastCutom->getY();  
  8.         CustomSprite* hit1=(CustomSprite*)hitArray->objectAtIndex(0);// 取出第一個  
  9.         CustomSprite* hit2=(CustomSprite*)hitArray->objectAtIndex(1);// 取出第二個  
  10.   
  11.         // 隨便拿出一個點  向他的四個方向出發 看看能否和另外一個變成一個拐角相連  
  12.         // 這裡我們就拿出取出的第一個點吧 hit1  
  13.   
  14.         CCArray* temparray=CCArray::create();// 記錄兩個點之間 所經過的點  
  15.         int x1=hit1->getX(); // 得到第一個X 座標  
  16.         int y1=hit1->getY(); // 得到第1個Y座標  
  17.         // 首先向左檢測  
  18.         CCArray* temphit=CCArray::create();   
  19.         temphit->addObject(hit2);  
  20.         for(int i=(x1-1);i>=0;i--){  
  21.                 CustomSprite* tepcou =LLKAlgorithm::getCustByXandY(llkArray, i, y1);  
  22.                 if (tepcou->getIsEliminate()){  
  23.                         // 判斷看能否組成一個矩形  
  24.                         temparray->addObject(tepcou);  
  25.                         if(tepcou->getX()==hit2->getX()||hit2->getY()==tepcou->getY()){  
  26.   
  27.                                 continue;  
  28.                         }  
  29.                         isjiance=true;  
  30.                         temphit->addObject(tepcou);  
  31.                         CCArray* temparr=LLKAlgorithm::oneCorner(llkArray,temphit);  
  32.                         if (temparr->count()>0){  
  33.                                 temparray->addObjectsFromArray(temparr);  
  34.                                 break;  
  35.                         }else {  
  36.                                 if(i==0){  
  37.                                         LLKAlgorithm::removeAllArray(temparray,false);  
  38.                                 }  
  39.   
  40.                                 temphit->removeLastObject(false);  
  41.                         }  
  42.                 }else{  
  43.                         LLKAlgorithm::removeAllArray(temparray,false);  
  44.   
  45.                         break;  
  46.                 }  
  47.   
  48.         }  
  49.   
  50.         if (isjiance==false){  
  51.                 LLKAlgorithm::removeAllArray(temparray,false);                          
  52.         }  
  53.   
  54.         if(temparray->count()==0){  
  55.                 bool isjiance=false;   
  56.                 // 向右檢測  
  57.                 for(int i=(x1+1);i<=max;i++){  
  58.                         CustomSprite* tepcou =LLKAlgorithm::getCustByXandY(llkArray, i, y1);  
  59.                         if (tepcou->getIsEliminate()){  
  60.                                 // 判斷看能否組成一個矩形  
  61.                                 temparray->addObject(tepcou);  
  62.                                 if(tepcou->getX()==hit2->getX()||hit2->getY()==tepcou->getY()){  
  63.                                         continue;  
  64.                                 }  
  65.                                 isjiance=true;   
  66.                                 temphit->addObject(tepcou);  
  67.                                 CCArray* temparr=LLKAlgorithm::oneCorner(llkArray,temphit);  
  68.                                 if (temparr->count()>0){  
  69.                                         temparray->addObjectsFromArray(temparr);  
  70.                                         break;  
  71.                                 }else {  
  72.                                         if(i==max){  
  73.                                                 LLKAlgorithm::removeAllArray(temparray,false);  
  74.                                         }                                  
  75.                                         temphit->removeLastObject(false);  
  76.                                 }  
  77.                         }else{  
  78.                                 LLKAlgorithm::removeAllArray(temparray,false);                          
  79.                                 break;  
  80.                         }  
  81.                 }  
  82.   
  83.                 if (isjiance==false){  
  84.                         LLKAlgorithm::removeAllArray(temparray,false);                          
  85.                 }  
  86.         }  
  87.   
  88.         if(temparray->count()==0){  
  89.                 bool isjiance=false;   
  90.                 // 向下檢測                  
  91.                 for(int i=(y1-1);i>=0;i--){  
  92.                         CustomSprite* tepcou =LLKAlgorithm::getCustByXandY(llkArray, x1, i);  
  93.                         if (tepcou->getIsEliminate()){  
  94.                                 temparray->addObject(tepcou);  
  95.                                 // 判斷看能否組成一個矩形  
  96.                                 if(tepcou->getX()==hit2->getX()||hit2->getY()==tepcou->getY()){                                  
  97.                                         continue;  
  98.                                 }  
  99.                                 isjiance=true;   
  100.                                 temphit->addObject(tepcou);  
  101.                                 CCArray* temparr=LLKAlgorithm::oneCorner(llkArray,temphit);  
  102.                                 if (temparr->count()>0){  
  103.                                         temparray->addObjectsFromArray(temparr);  
  104.                                         break;  
  105.                                 }else {  
  106.                                         if (i==0){  
  107.                                                 LLKAlgorithm::removeAllArray(temparray,false);  
  108.                                         }                                          
  109.                                         temphit->removeLastObject(false);  
  110.                                 }  
  111.                         }else{  
  112.                                 LLKAlgorithm::removeAllArray(temparray,false);  
  113.                                 break;  
  114.                         }  
  115.                 }  
  116.                 if (isjiance==false){  
  117.                         LLKAlgorithm::removeAllArray(temparray,false);                          
  118.                 }  
  119.         }  
  120.   
  121.   
  122.         if(temparray->count()==0){  
  123.                 bool isjiance=false;   
  124.                 // 向上檢測                  
  125.                 for(int i=(y1+1);i<=may;i++){  
  126.                         CustomSprite* tepcou =LLKAlgorithm::getCustByXandY(llkArray, x1, i);  
  127.                         if (tepcou->getIsEliminate()){  
  128.                                 temparray->addObject(tepcou);  
  129.                                 // 判斷看能否組成一個矩形  
  130.                                 if(tepcou->getX()==hit2->getX()||hit2->getY()==tepcou->getY()){  
  131.                                         continue;  
  132.                                 }  
  133.                                 isjiance=true;   
  134.                                 temphit->addObject(tepcou);  
  135.                                 CCArray* temparr=LLKAlgorithm::oneCorner(llkArray,temphit);  
  136.                                 if (temparr->count()>0){  
  137.                                         temparray->addObjectsFromArray(temparr);          
  138.                                         break;  
  139.                                 }else {  
  140.                                         if(i==may){  
  141.                                                 LLKAlgorithm::removeAllArray(temparray,false);  
  142.                                         }  
  143.   
  144.                                         temphit->removeLastObject(false);  
  145.                                 }  
  146.                         }else{  
  147.                                 LLKAlgorithm::removeAllArray(temparray,false);  
  148.   
  149.                                 break;  
  150.                         }  
  151.                 }  
  152.                 if (isjiance==false){  
  153.                         LLKAlgorithm::removeAllArray(temparray,false);                          
  154.                 }  
  155.         }  
  156.   
  157.         if (temparray->count()>0){  
  158.                 temparray->addObject(hit1);  
  159.         }  
  160.   
  161.         return temparray;  

寫到這裡連連看的演算法基本算是已經完成了
     我在網上看了很多方法感覺不太容易理解,這個是我自己想出來的。感覺很通俗易懂,其實就是化繁為簡。我最麻煩的變成最簡單。不過連連看的演算法有很多種。不如著名的A* 尋路演算法就可以。不過我感覺那個給大家講解其實會很麻煩,有希望瞭解瞭解的同學可以在百度上上搜尋一下A* 尋路演算法。
     其實我子所以寫這個練練看看的演算法是交大家以後學習中的一種思路,有些同學一開始把問題弄得很複雜導致一時半會想不通,我建議剛開始考慮情況的時候從最簡單的開始考慮。到繁瑣的階段的時候看看能不能轉化成 簡單的方式。中國人其實都喜歡大事化小 小事化了  哈哈。 本章講解結束。 至於重新佈局+自動查詢 等演算法我會慢慢加進去 希望大家耐心等待。

    另外給大家說下以後我所有的部落格首發地址將會是天地會的論壇,由於我是他們哪裡的原創管理。所以我以後在釋出部落格首發地址將會天地會感興趣的同學可以關注一下

  http://bbs.9ria.com/forum.php?mod=forumdisplay&fid=318



     我給本演算法起名字交錯化繁為簡 演算法。

       如果沒有看懂的同學可給我留言,我給給大家仔細講解一下

原始碼下載

相關文章