From 56c6c8d0a485768e591a48a18414305ac3452edb Mon Sep 17 00:00:00 2001 From: Kyle K Date: Tue, 1 Feb 2011 18:09:41 -0600 Subject: got sorting right --- card.cpp | 37 +++++++++++++++++++++++++++++++++++-- game.cpp | 2 +- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/card.cpp b/card.cpp index 19d732b..879831a 100644 --- a/card.cpp +++ b/card.cpp @@ -80,7 +80,8 @@ void Card::rankCard(void) /* rank value that will be used by a sorting algorithm to put cards such as a * pair, two pair, three of a kind, four of a kind, or full house all the way - * to the left, SortHand will set these attributes when necessary and call sort */ + * to the left + */ void Card::specialRank(void) { char c; @@ -171,8 +172,36 @@ void Card::sortCards(Card **cards) return; } - /* reverse bubble sort */ int i, j; + int howManyOfSameCards[5] = { 1,1,1,1,1 }; + char c1, c2; + for (i = 0; i < 5; i++) + { + c1 = cards[i]->getType()[0]; + + for (j = 0; j < 5; j++) + { + /* don't count itself */ + if (j == i) + continue; + + c2 = cards[j]->getType()[0]; + if (c1 == c2) + howManyOfSameCards[i] += 1; + } + } + + /* indices that contain 1 are not associated with any other cards, + * indices that are not 1 have a pair or greater, so we want to give + * them higher rank so they get shown all the way on the left + */ + for (i = 0; i < 5; i++) + { + if (howManyOfSameCards[i] != 1) + cards[i]->specialRank(); + } + + /* reverse bubble sort */ Card *tmp; for (i = 0; i < 5; i++) @@ -185,5 +214,9 @@ void Card::sortCards(Card **cards) cards[j+1] = tmp; } } + + /* reset special rank */ + for (i = 0; i < 5; i++) + cards[i]->rankCard(); } diff --git a/game.cpp b/game.cpp index 6f8931b..a59f433 100644 --- a/game.cpp +++ b/game.cpp @@ -808,7 +808,7 @@ bool Game::hasTwoPairs(Card **cards) int cnt = 0; int i; - /* if there are 2 pairs, they should be 1 index that contains 1 */ + /* if there are 2 pairs, they should be one index that contains 1 */ for (i = 0; i < 5; i++) if (cardsToDiscard[i] == 1) cnt++; -- cgit v1.2.3