2011-04-10 37 views
1

這是由我的老師提供的驅動程序代碼,它並不意味着由我編輯。我需要幫助檢查我的代碼。任何審查大大優於

PlayingCardTest.cpp

#include <iostream> 
#include "PlayingCard.h" 

PlayingCard makeValidCard(int value, int suit); 

int main() 
{ 
    // Create a playing card 
    PlayingCard card1; 

    // Test the default constructor and GetCardCode 
    std::cout << "Testing default constructor. Expect card code to be 00\n card code is :"; 
    std::cout << card1.getCardCode() << std::endl << std::endl; 

    // Test the setter and getter 
    std::cout << "Seting card to 'AH' using SetValue and SetSuit" << std::endl; 
    card1.setCard('A', 'H'); 
    std::cout << "GetValue returns :" << card1.getValue() << std::endl; 
    std::cout << "GetSuit returns :" << card1.getSuit() << std::endl << std::endl; 

    // Test overloaded constructor 
    PlayingCard tenOfSpades('T', 'S'); 
    std::cout << "Testing overloaded constructor. Expect card code to be TS\n card code is :"; 
    std::cout << tenOfSpades.getCardCode() << std::endl << std::endl; 

    // Test IsValid with valid cards 
    std::cout << "Testing valid card codes.\n" 
     << "Expect isValid to return true for all (except perhaps Jokers.)" 
     << std::endl; 
    // Create and test valid cards 
    int validCards = 0;  // cards that return true for IsValid 
    int invalidCards = 0; // cards that return false for IsValid 

    // Create and test four suits plus the jokers 
    for(int suit = 1; suit <= 5; suit++) 
    { 
     // Create and test ace, 2 - 9, Jack, Queen, and King 
     for(int value = 1; value <= 13; value++) 
     { 
      PlayingCard aCard = makeValidCard(value, suit); 
      std::cout << "Card Code: " << aCard.getCardCode() << " IsValid :"; 
      if (aCard.isValid()) 
      { 
       validCards++; 
       std::cout << "true" << std::endl; 
      } 
      else 
      { 
       invalidCards++; 
       std::cout << "false" << std::endl; 
      } 
      // suit 5 is just for creating the two Jokers 
      if (suit == 5 && value >= 2) 
       break; 
     } 
    } 
    std::cout << "IsValid returned false for " << invalidCards << " card codes" << std::endl; 
    std::cout << "IsValid returned true for " << validCards << " card codes" << std::endl; 
    std::cout << std::endl; 

    // Test IsValid with invalid cards 
    // Create and test invalid cards 
    std::cout << "Testing invalid card codes; isValid should return false for all." << std::endl; 
    validCards = 0; 
    invalidCards = 0; 
    // Loop through all possible ASCII character codes for card codes 
    for(int suit = 0; suit <= 255; suit++) 
     for(int value = 0; value <= 255; value++) 
     { 
      // Only check card codes that are not valid 
      PlayingCard aCard = makeValidCard(value, suit); 
      if (aCard.getCardCode() == "00") 
      { 
       if (aCard.isValid()) 
       { 
        std::cout << "value :" << value << " suit :" <<suit << " IsValid :"; 
        std::cout << "true" << std::endl; 
        validCards++; 
       } 
       else 
       { 
        invalidCards++; 
       } 
      } 
     } 
     std::cout << "IsValid returned false for " << invalidCards << " card codes" << std::endl; 
     std::cout << "IsValid returned true for " << validCards << " card codes" << std::endl; 

    return 0; 
} 

/******************************************************/ 
/* Test Functions          */ 
/******************************************************/ 

PlayingCard makeValidCard(int iValue, int iSuit) 
{ 
    char value = '0'; 
    char suit = '0'; 

    switch (iValue) 
    { 
    case 1: 
     value = 'A'; 
     break; 
    case 10: 
     value = 'T'; 
     break; 
    case 11: 
     value = 'J'; 
     break; 
    case 12: 
     value = 'Q'; 
     break; 
    case 13: 
     value = 'K'; 
     break; 
    default: 
     if ((iValue >= 2) && (iValue <= 9)) 
      value = '0' + iValue; 
     break; 
    } 

    switch (iSuit) 
    { 
    case 1: 
     suit = 'D'; 
     break; 
    case 2: 
     suit = 'S'; 
     break; 
    case 3: 
     suit = 'C'; 
     break; 
    case 4: 
     suit = 'H'; 
     break; 
    // Special case for the Joker 
    case 5: 
     if(iValue == 1) 
     { 
      value = 'Z'; 
      suit = 'B'; 
     } 
     else if(iValue == 2) 
     { 
      value = 'Z'; 
      suit = 'R'; 
     } 
     else 
     { 
      value = '0'; 
      suit = '0'; 
     } 
     break; 
    } 

    PlayingCard testCard(value, suit); 
    return testCard; 
} 

這是我的頭文件,PlayingCard.h

#ifndef PLAYINGCARD_H_INCLUDED 
#define PLAYINGCARD_H_INCLUDED 

class PlayingCard 
{ 
private: 
    char suit, value; 

public: 
    PlayingCard(){suit = '0'; value = '0';} 
    PlayingCard(char myValue, char mySuit); 

    char getValue() {return value;} 
    char getSuit() {return suit;} 

    std::string getCardCode(); 
    bool setCard(char myValue, char mySuit); 
    bool isValid(); 

#endif // PLAYINGCARD_H_INCLUDED 

這是我的類實現文件,PlayingCard.cpp

#include "PlayingCard.h" 

PlayingCard::PlayingCard (char myValue, char mySuit) 
{ 
    char aValue[13] ('2','3','4','5','6','7','8','9','T','J','Q','K','A')) 
    char aSuit[4] {'D','H','C','S'] 

    for(count = 0; count <= 12; count++) 
    { 
     if (myValue = aValue[count]) 
     { 
      for (count2 = 0; count2 <= 3; count2++) 
      { 
       if (mySuit = aSuit[count2++]) 
       { 
        suit = mySuit; 
        value = myValue; 
       } 
      } 
     } 
    } 
} 

bool PlayingCard::setCard(char myValue, char mySuit) 
{ 
    char aValue[13] ('2','3','4','5','6','7','8','9','T','J','Q','K','A')) 
    char aSuit[4] {'D','H','C','S'] 

    for(count = 0; count <= 12; count++) 
    { 
     if (myValue = aValue[count]) 
     { 
      for (count2 = 0; count2 <= 3; count2++) 
      { 
       if (mySuit = aSuit[count2++]) 
       { 
        suit = mySuit; 
        value = myValue; 
       } 
       else 
       { 
        return false; 
       } 
      } 
     } 
    } 
} 

string PlayingCard::getCardCode() 
{ 
    return suit + value; 
} 

bool PlayingCard::isValid() 
{ 
    char aValue[13] ('2','3','4','5','6','7','8','9','T','J','Q','K','A')) 
    char aSuit[4] {'D','H','C','S'] 

    for(count = 0; count <= 12; count++) 
    { 
     if (myValue = aValue[count]) 
     { 
      for (count2 = 0; count2 <= 3; count2++) 
      { 
       if (mySuit = aSuit[count2++]) 
       { 
        return true; 
       } 
       else 
       { 
        return false; 
       } 
      } 
     } 
    } 
} 

這是我收到的編譯器錯誤。我不知道該怎麼做,它看起來像在我不應該編輯的文件中。我會很感激你能給的幫助。

PlayingCardTest.cpp|103|error: 'PlayingCard PlayingCard::makeValidCard(int, int)' cannot be overloaded|

PlayingCardTest.cpp|5|error: with 'PlayingCard PlayingCard::makeValidCard(int, int)'|

PlayingCardTest.cpp|169|error: expected '}' at end of input|

PlayingCardTest.cpp|169|error: expected unqualified-id at end of input|

||=== Build finished: 4 errors, 0 warnings ===|

+2

您可能想嘗試在http://codereview.stackexchange.com上提問。 – 2011-04-10 04:10:59

+3

@GregHewgill:實際上,這是代碼審查的主題。代碼審查旨在改進現有的工作代碼。他的代碼不是編譯的,編譯錯誤的幫助只在這裏討論。 – Will 2011-05-11 15:46:58

+0

@威爾:謝謝,我會牢記這一點。 – 2011-05-11 19:16:17

回答

1

第一輪徵求意見:

  1. 風格尼特:爲了節 「公」, 「保護」,然後選擇 「私人」。私人部分不應該公開。這在技術上不是必需的,但是相當標準的做法。
  2. 風格nit:使用單獨的語句聲明每個變量,每個語句都在自己的行上。使用逗號是惹麻煩的好方法(例如,當聲明指針類型時)並且風格很差。
  3. 在構造函數中使用初始化列表,而不是使用賦值運算符。
  4. 你應該在你的頭文件中包含「<字符串>」以使用std :: string。

第二輪的評論:

  1. 你古怪初始化您的陣列;你應該使用{}作爲括號。
  2. 您不需要在初始化中指定數組的大小。
  3. 風格nit:不要在代碼中使用像「12」這樣的魔術常量。相反,將它們分配給諸如value_length或value_count之類的變量,並使用指定的變量。
  4. 你的意思是在你的if語句中做一個等於比較(「==」)還是一個賦值(「=」)?如果你打算做一個任務,你應該把它移到if之外。

第三輪的評論:

  1. 您不必要地重複你的非默認構造函數和你setCard功能的代碼。你應該能夠在這兩個函數之間共享代碼。由於setCard不是一個虛函數,你應該可以簡單地從你的構造函數中調用它。
  2. 你的setCard邏輯看起來相當複雜。大多數「設置」功能比這更微不足道。您應該考慮添加說明它正在嘗試執行的邏輯的文檔。
  3. 應將「getValue()」,「getCardCode()」,「getSuit()」和「isValid()」函數聲明爲「const」。

第四輪的評論:

  1. 由於你的教授做「遊戲牌卡= makeValidCard(......)」,很顯然,他希望你的卡類,以支持任務。既然你的「setCard()」函數和你的非默認構造函數做的不是簡單的設置屬性,而是提供一個「PlayingCard & operator =(const PlayingCard &);」賦值操作符以及「PlayingCard :: PlayingCard(const PlayingCard &)」複製構造函數。如果您沒有提供這些信息,最好添加一條評論,以表明已故意允許使用默認分配/複製進行復制。
+0

非常感謝你,現在正在工作。 – 2011-04-10 07:24:36

2

您在頭文件末尾缺少};