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 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'card.cpp') 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(); } -- cgit v1.2.3