2011-04-10 38 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

您在头文件末尾缺少};