From 97290be5e03ba2299de0e0988404fb4979852b5c Mon Sep 17 00:00:00 2001 From: Kyle K Date: Tue, 1 Feb 2011 17:25:46 -0600 Subject: baby steps --- card.cpp | 66 +++++++++++++++--------------------------------------------- card.h | 1 - cardpile.cpp | 5 +++++ game.cpp | 51 ++++++++++++++++++++++++++++++++++++++++------ game.h | 1 + sorthand.cpp | 38 ++++++++++++---------------------- 6 files changed, 80 insertions(+), 82 deletions(-) diff --git a/card.cpp b/card.cpp index 5bb1a56..19d732b 100644 --- a/card.cpp +++ b/card.cpp @@ -89,46 +89,46 @@ void Card::specialRank(void) switch (c) { case 'A': - rank = -13; + rank = 26; break; case 'K': - rank = -12; + rank = 25; break; case 'Q': - rank = -11; + rank = 24; break; case 'J': - rank = -10; + rank = 23; break; case 'T': - rank = -9; + rank = 22; break; case '9': - rank = -8; + rank = 21; break; case '8': - rank = -7; + rank = 20; break; case '7': - rank = -6; + rank = 19; break; case '6': - rank = -5; + rank = 18; break; case '5': - rank = -4; + rank = 17; break; case '4': - rank = -3; + rank = 16; break; case '3': - rank = -2; + rank = 15; break; case '2': - rank = -1; + rank = 14; break; default: - std::cerr << "something went wrong while special ranking a card" << endl; + std::cerr << "something went wrong while special specialRanking a card" << endl; } } @@ -177,47 +177,13 @@ void Card::sortCards(Card **cards) for (i = 0; i < 5; i++) for (j = 0; j < 5-i-1; j++) + { if (cards[j]->rank < cards[j+1]->rank) { tmp = cards[j]; cards[j] = cards[j+1]; cards[j+1] = tmp; } -} - -char Card::giveRank(char type) -{ - switch (type) - { - case 'A': - return 13; - case 'K': - return 12; - case 'Q': - return 11; - case 'J': - return 10; - case 'T': - return 9; - case '9': - return 8; - case '8': - return 7; - case '7': - return 6; - case '6': - return 5; - case '5': - return 4; - case '4': - return 3; - case '3': - return 2; - case '2': - return 1; - default: - std::cerr << "something went wrong while giving a card" << endl; - return 0; - } + } } diff --git a/card.h b/card.h index 6a7555f..ac0f5c0 100644 --- a/card.h +++ b/card.h @@ -40,7 +40,6 @@ class Card void rankCard(void); void specialRank(void); void sortCards(Card **); - char giveRank(char); }; #endif diff --git a/cardpile.cpp b/cardpile.cpp index 1eb27eb..3f1597b 100644 --- a/cardpile.cpp +++ b/cardpile.cpp @@ -88,5 +88,10 @@ void CardPile::resetDeck(void) { deckCurrIndex = 51; discardPileCurrIndex = 0; + + /* reset cards' rank */ + int i; + for (i = 0; i < 52; i++) + deck[i]->rankCard(); } diff --git a/game.cpp b/game.cpp index c7b78ae..fd9806e 100644 --- a/game.cpp +++ b/game.cpp @@ -212,6 +212,7 @@ int Game::numOfSameCards(Card **cards) return max; } +/* returns the max number of the same suit along with the suit itself */ struct countAndSuit Game::numOfSameSuit(Card **cards) { struct countAndSuit same[5] = @@ -268,7 +269,7 @@ struct countAndSuit Game::numOfSameSuit(Card **cards) return same[whereMaxWasFound]; } -/* returns the max count of a particular card in a hand */ +/* returns the max count of a particular card in a hand */ struct countAndType Game::numOfSameType(Card **cards) { struct countAndType same[14] = @@ -427,6 +428,7 @@ int *Game::whichCardsToDiscard(Card **cards) return howManyOfSameCards; } +/* helper function for opponentAI */ void Game::oppDiscardFromPairOrBetter(Card **cards, int whichOpponent) { if (cards == NULL) { @@ -641,7 +643,17 @@ void Game::userAI(Card **cards) int howManyUserCanDiscard = 0; int i; - /* XXX: handle straight */ + if (isInOrderByOne(cards)) + { + howManyUserCanDiscard = 3; + + cout << "You have a straight, you should definitely\n" + << "keep all of the cards but you can discard " + << howManyUserCanDiscard << " card(s)" << endl; + + userDiscardCards(cards, howManyUserCanDiscard); + return; + } if (pairOrBetter(cards)) { @@ -757,6 +769,34 @@ bool Game::isInOrder(Card **cards) return true; } +bool Game::hasFullHouse(Card **cards, struct countAndType *threeCount) +{ + if (cards == NULL) { + PRINT_COLOR(ANSI_COLOR_RED, "\nhasFullHouse(): cards parameter is NULL!\n"); + return false; + } + + int i; + for (i = 0; i < 5; i++) + if (cards[i]->getType()[0] != threeCount->type) + break; + + int j; + for (j = 0; j < 5; j++) + if ((cards[j]->getType()[0] != threeCount->type) && i != j) + break; + + bool ret = cards[i]->getType()[0] == cards[j]->getType()[0]; + +#ifdef DEBUG + cout << ANSI_COLOR_CYAN << "\ndebug: hasFullHouse(): does " << cards[i]->getType() + << " have the same value as " << cards[j]->getType() << "? " << ret + << endl << ANSI_COLOR_RESET; +#endif + + return ret; +} + bool Game::hasTwoPairs(Card **cards) { if (cards == NULL) { @@ -787,8 +827,6 @@ void Game::evaluateHand(Card **cards, int who) struct countAndSuit tmp = numOfSameSuit(cards); struct countAndType tmp2 = numOfSameType(cards); - /* XXX: Some hands need to be sorted */ - /* royal flush */ if (haveAce && inOrderByOne && (tmp.count == 5)) whatHand = 0; @@ -804,10 +842,11 @@ void Game::evaluateHand(Card **cards, int who) sortFourOfAKind(cards, &tmp2); } - /* XXX: flawed */ /* full house */ - else if ((tmp2.count == 3) && (cards[3]->getType()[0] == cards[4]->getType()[0])) + else if ((tmp2.count == 3) && hasFullHouse(cards, &tmp2)) + { whatHand = 3; + } /* flush */ else if (tmp.count == 5) diff --git a/game.h b/game.h index 298a1a1..0ac8a66 100644 --- a/game.h +++ b/game.h @@ -48,6 +48,7 @@ class Game : virtual public CardPile, public User, public Opponent, public SortH bool isInOrderByOne(Card **); bool isInOrder(Card **); + bool hasFullHouse(Card **, struct countAndType *); bool hasTwoPairs(Card **); void evaluateHand(Card **, int); void showHands(void); diff --git a/sorthand.cpp b/sorthand.cpp index 0b09b7c..e713b7d 100644 --- a/sorthand.cpp +++ b/sorthand.cpp @@ -13,22 +13,25 @@ SortHand::SortHand() void SortHand::sortFourOfAKind(Card **cards, struct countAndType *maxCount) { +#if 0 int i; /* find the card that is different from other four */ for (i = 0; i < 5; i++) if (cards[i]->getType()[0] != maxCount->type) break; +#endif + int k; + for (k = 0; k < 5; k++) + if (cards[k]->getType()[0] == maxCount->type) + { + cards[k]->specialRank(); #ifdef DEBUG - cout << ANSI_COLOR_CYAN << "\ndebug: sortFourOfAKind(): " << cards[i]->getType() - << " is different from other 4 cards" << endl << ANSI_COLOR_RESET; + cout << ANSI_COLOR_CYAN << "\ndebug: sortFourOfAKind(): " + << cards[k]->getType() << " at cards[" << k + << "] gets special rank "<< endl << ANSI_COLOR_RESET; #endif - - /* put that card at the end */ - Card *tmp = cards[4]; - - cards[4] = cards[i]; - cards[i] = tmp; + } } void SortHand::sortThreeOfAKind(Card **cards, struct countAndType *threeCount) @@ -50,22 +53,7 @@ void SortHand::sortThreeOfAKind(Card **cards, struct countAndType *threeCount) << endl << ANSI_COLOR_RESET; #endif - /* put those cards at the end */ - Card *tmp = cards[4]; - Card *tmp2 = cards[3]; - - if (cards[i]->getRank() < cards[j]->getRank()) - { - cards[4] = cards[i]; - cards[3] = cards[j]; - } - else - { - cards[4] = cards[j]; - cards[3] = cards[i]; - } - - cards[i] = tmp; - cards[j] = tmp2; + cards[i]->specialRank(); + cards[j]->specialRank(); } -- cgit v1.2.3