diff options
Diffstat (limited to 'game.cpp')
| -rw-r--r-- | game.cpp | 156 | 
1 files changed, 120 insertions, 36 deletions
| @@ -119,8 +119,7 @@ void Game::repeatGame(bool *quit)  int Game::howManyCardsOfSameSuit(Card **cards)  { -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\nhowManyCardsOfSameSuit(): cards parameter is NULL!\n");          return -1;      } @@ -141,8 +140,7 @@ int Game::howManyCardsOfSameSuit(Card **cards)  bool Game::pairOrBetter(Card **cards)  { -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\npairOrBetter(): cards parameter is NULL!\n");          return false;      } @@ -172,8 +170,7 @@ bool Game::pairOrBetter(Card **cards)  /* returns max number of same cards, returns 0 when all cards are different */  int Game::numOfSameCards(Card **cards)  { -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\nnumOfSameCards(): cards parameter is NULL!\n");          return -1;      } @@ -220,8 +217,7 @@ struct countAndSuit Game::numOfSameSuit(Card **cards)          { Undef, -1 },      }; -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\nnumOfSameSuit(): cards parameter is NULL!\n");          return same[4];      } @@ -250,7 +246,7 @@ struct countAndSuit Game::numOfSameSuit(Card **cards)              same[3].count += 1;              break;          default: -            cout << "could not determine the suit of the card" << endl; +            std::cerr << "could not determine the suit of the card" << endl;          }      } @@ -266,10 +262,103 @@ struct countAndSuit Game::numOfSameSuit(Card **cards)      return same[whereMaxWasFound];  } -bool Game::doWeHaveAnAce(Card **cards) +/* returns the max count of a particular card in a  hand */ +struct countAndType Game::numOfSameType(Card **cards)  { -    if (cards == NULL) +    struct countAndType same[14] = +    { +        { 'A', 0 }, +        { 'K', 0 }, +        { 'Q', 0 }, +        { 'J', 0 }, +        { 'T', 0 }, +        { '9', 0 }, +        { '8', 0 }, +        { '7', 0 }, +        { '6', 0 }, +        { '5', 0 }, +        { '4', 0 }, +        { '3', 0 }, +        { '2', 0 }, +        { '0',-1 }  /* undefined */ +    }; + +    if (cards == NULL) { +        PRINT_COLOR(ANSI_COLOR_RED, "\nnumOfSameTypes(): cards parameter is NULL!\n"); +        return same[13]; +    } + +    int max = 0; +    char type = '0'; + +    /* go through all cards and count how many there is of each suit */ +    int i; +    for (i = 0; i < 5; i++)      { +        type = cards[i]->getType()[0]; + +        switch (type) +        { +        case 'A': +            same[0].count += 1; +            break; +        case 'K': +            same[1].count += 1; +            break; +        case 'Q': +            same[2].count += 1; +            break; +        case 'J': +            same[3].count += 1; +            break; +        case 'T': +            same[4].count += 1; +            break; +        case '9': +            same[5].count += 1; +            break; +        case '8': +            same[6].count += 1; +            break; +        case '7': +            same[7].count += 1; +            break; +        case '6': +            same[8].count += 1; +            break; +        case '5': +            same[9].count += 1; +            break; +        case '4': +            same[10].count += 1; +            break; +        case '3': +            same[11].count += 1; +            break; +        case '2': +            same[12].count += 1; +            break; +        default: +            std::cerr << "could not determine the type of the card" << endl; +            break; +        } +    } + +    int whereMaxWasFound = 0; +    /* look for the max number of cards of the same type */ +    for (i = 0; i < 13; i++) +        if (same[i].count > max) +        { +            max = same[i].count; +            whereMaxWasFound = i; +        } + +    return same[whereMaxWasFound]; +} + +bool Game::doWeHaveAnAce(Card **cards) +{ +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\ndoWeHaveAnAce(): cards parameter is NULL!\n");          return false;      } @@ -292,8 +381,7 @@ bool Game::doWeHaveAnAce(Card **cards)   * since it's a unique card */  int *Game::whichCardsToDiscard(Card **cards)  { -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\nwhichCardsToDiscard(): cards parameter is NULL!\n");          return NULL;      } @@ -306,7 +394,7 @@ int *Game::whichCardsToDiscard(Card **cards)      howManyOfSameCards[4] = 1;      char c1, c2; -    /* goal is to look later at indices that contain zero */ +    /* goal is to look later at indices that contain a 1 */      int i, j;      for (i = 0; i < 5; i++)      { @@ -329,8 +417,7 @@ int *Game::whichCardsToDiscard(Card **cards)  void Game::oppDiscardFromPairOrBetter(Card **cards, int whichOpponent)  { -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\ndiscardFromPairOrBetter(): cards parameter is NULL!\n");          return;      } @@ -339,7 +426,7 @@ void Game::oppDiscardFromPairOrBetter(Card **cards, int whichOpponent)      int *howManyOfSameCards = whichCardsToDiscard(cards);      int i; -    /* indices that contain a one should be discarded */ +    /* indices that contain a 1 should be discarded */      for (i = 0; i < 5; i++)      {          if (howManyOfSameCards[i] == 1) @@ -356,8 +443,7 @@ void Game::oppDiscardFromPairOrBetter(Card **cards, int whichOpponent)  /* make sure cards are in decreasing rank */  bool Game::sanityCheck(Card **cards)  { -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\nsanityCheck(): cards parameter is NULL!\n");          return false;      } @@ -384,8 +470,8 @@ bool Game::sanityCheck(Card **cards)  } -/*============================================================================== - * Discard and Draw +/*============================================================================* + * Discard and Draw                                                           *   *============================================================================*/  void Game::discardAndDraw(Card **cards, int amount, int offset)  { @@ -423,8 +509,8 @@ void Game::discardAndDraw(Card **cards, int amount, int offset)  } -/*============================================================================== - * Computer AI +/*============================================================================* + * Computer AI                                                                *   *============================================================================*/  void Game::opponentAI(int whichOpponent)  { @@ -518,8 +604,8 @@ void Game::opponentAI(int whichOpponent)  } -/*============================================================================== - * User AI +/*============================================================================* + * User AI                                                                    *   *============================================================================*/  void Game::userDiscardCards(Card **cards, int howManyUserCanDiscard)  { @@ -613,8 +699,7 @@ void Game::userAI(Card **cards)  bool Game::isInOrderByOne(Card **cards)  { -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\nisInOrderByOne(): cards parameter is NULL!\n");          return false;      } @@ -637,8 +722,7 @@ bool Game::isInOrderByOne(Card **cards)  bool Game::isInOrder(Card **cards)  { -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\nisInOrder(): cards parameter is NULL!\n");          return false;      } @@ -661,8 +745,7 @@ bool Game::isInOrder(Card **cards)  bool Game::hasTwoPairs(Card **cards)  { -    if (cards == NULL) -    { +    if (cards == NULL) {          PRINT_COLOR(ANSI_COLOR_RED, "\nhasTwoPairs(): cards parameter is NULL!\n");          return false;      } @@ -687,6 +770,7 @@ void Game::evaluateHand(Card **cards, int who)      bool haveAce = doWeHaveAnAce(cards);      bool inOrderByOne = isInOrderByOne(cards);      struct countAndSuit tmp = numOfSameSuit(cards); +    struct countAndType tmp2 = numOfSameType(cards);      /* royal flush */      if (haveAce && inOrderByOne && (tmp.count == 5)) @@ -697,11 +781,11 @@ void Game::evaluateHand(Card **cards, int who)          whatHand = 1;      /* four of a kind */ -    if (tmp.count == 4) +    if (tmp2.count == 4)          whatHand = 2;      /* full house */ -    if ((tmp.count == 3) && (cards[3]->getType()[0] == cards[4]->getType()[0])) +    if ((tmp2.count == 3) && (cards[3]->getType()[0] == cards[4]->getType()[0]))          whatHand = 3;      /* flush */ @@ -713,7 +797,7 @@ void Game::evaluateHand(Card **cards, int who)          whatHand = 5;      /* three of a kind */ -    if (tmp.count == 3) +    if (tmp2.count == 3)          whatHand = 6;      /* two pair */ @@ -721,7 +805,7 @@ void Game::evaluateHand(Card **cards, int who)          whatHand = 7;      /* one pair */ -    if (tmp.count == 2) +    if (tmp2.count == 2)          whatHand = 8;      else @@ -742,7 +826,7 @@ void Game::evaluateHand(Card **cards, int who)          setOpponentHand(whatHand, 3);          break;      default: -        cout << "something went wrong in evaluation" << endl; +        std::cerr << "something went wrong in evaluation" << endl;      }  } | 
