2017-10-28 73 views
1

爲什麼我的檢查沒有檢查重複的結果(指針)

int main() 
    { srand(time(NULL)); 
     Card c; 
     Card *Pc; 
     Pc = new Card [52]; 
     Card ** someHands; 
//  typedef Card * Hand; 
//  Hand *someHands; 
     CardtoArray(c,Pc); 
     printDeckofCards(Pc,c); 

    } 

Card GenerateCards(Card c) 
    { 
     int CardNum; 
     CardNum=rand() % 13 + 1; 
     int colour =rand() % 4 + 1; 

     switch(CardNum) 
      { 
       case 1: c.rank= Ace;       
       break; 

       case 2: c.rank= Two; 
       break; 

       case 3: c.rank= Three; 
       break; 

       case 4: c.rank= Four; 
       break; 

       case 5: c.rank= Five; 
       break; 

       case 6: c.rank= Six; 
       break; 

       case 7: c.rank= Seven; 
       break; 

       case 8: c.rank= Eight; 
       break; 

       case 9: c.rank= Nine; 
       break; 

       case 10: c.rank= Ten; 
       break; 

       case 11: c.rank= Jack; 
       break; 

       case 12: c.rank= Queen; 
       break; 

       case 13: c.rank= King; 
       break; 
      } 
     switch(colour) 
      { 
       case 1:c.color="Diamond"; 
       break; 
       case 2:c.color="Club"; 
       break; 
       case 3:c.color="Heart"; 
       break; 
       case 4:c.color="Spade"; 
       break; 
      }   
      return c; 

    } 

//打印一張卡,此功能是要調入功能// printsdeckofcards工作 無效printCard(卡C) {

 cout<<*c.color; 

     if(c.rank==Ace) 
     cout<<"A"; 
     else if(c.rank==Two) 
     cout<<"2"; 
     else if(c.rank==Three) 
     cout<<"3"; 
     else if(c.rank==Four) 
     cout<<"4"; 
     else if(c.rank==Five) 
     cout<<"5"; 
     else if(c.rank==Six) 
     cout<<"6"; 
     else if(c.rank==Seven) 
     cout<<"7"; 
     else if(c.rank==Eight) 
     cout<<"8"; 
     else if(c.rank==Nine) 
     cout<<"9"; 
     else if(c.rank==Ten) 
     cout<<"10"; 
     else if(c.rank==Jack) 
     cout<<"J"; 
     else if(c.rank==Queen) 
     cout<<"Q"; 
     else if(c.rank==King) 
     cout<<"K"; 
    } 

//生成和插入卡52也調用函數samecard,以檢查是否已經產生 //智能卡被複制,並且如果之前,所以再生一個新//插入到陣列 無效CardtoArray(卡& C,卡* PC) {

 Card *origin; 
     origin=Pc; 
     for(int i=0;i<52;i++) 
     { 
      c=GenerateCards(c); 
      if(SameCard(c,origin)==false) 
      { 
       *Pc=c; 
       Pc++; 
      } 
      else 
       i--;  
     } 
    } 




void printDeckofCards(Card *Pc,Card c) 
    { 
     for(int i=0;i<52;i++) 
     { 
      printCard(*Pc); 
      cout<<" "; 
      Pc++;  
     }    
    } 

bool SameCard(Card c,Card *Pc) 
    { bool check; 

     for(int i=0; i<52; i++) 
     { 
      if(Pc->color==c.color && Pc->rank==c.rank) 
      { 
       check = true; 
      } 
      else 
      { 
       check = false; 
      } 
      Pc++; 
     } 
     return check; 
    } 

塊引用

+0

而且哪裏的東西「不行」? –

+0

我的檢查函數無法檢查我的卡是否在插入指針數組之前被複制過,總是有重複的結果 –

+1

您可能會喜歡閱讀:[如何調試小程序](https://ericlippert.com/2014/ 03/05/how-to-debug-small-programs /) – alk

回答

0

因爲要檢查所有的卡,您的布爾必須在循環之前設置爲false,並設置在循環中爲真;它不應該在循環再次設置爲false:

bool SameCard(Card c,Card *Pc) 
    { bool check= false; 

     for(int i=0; i<52; i++) 
     { 
      if(Pc->color==c.color && Pc->rank==c.rank) 
      { 
       check = true; 
      } 
      //else 
      ///{ 
      // check = false; 
      //} 
      Pc++; 
     } 
     return check; 
    } 

甚至:

bool SameCard(Card c,Card *Pc) 
{ 
    for(int i=0; i<52; i++, pc++) 
     if(Pc->color==c.color && Pc->rank==c.rank) 
      return true; 
    return false; 
} 
+0

OMG不能相信我花了4天對此非常感謝 –

相關問題