summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-02-01 18:09:41 -0600
committerKamil Kaminski <kamilkss@gmail.com>2011-02-01 18:09:41 -0600
commit56c6c8d0a485768e591a48a18414305ac3452edb (patch)
tree08b8dd9c4c0836948c4560efcd8dd20cda66de18
parent6c30258da6449f9ffae4b42f4aeda587fe4a3111 (diff)
downloadpoker-56c6c8d0a485768e591a48a18414305ac3452edb.tar.gz
poker-56c6c8d0a485768e591a48a18414305ac3452edb.tar.bz2
poker-56c6c8d0a485768e591a48a18414305ac3452edb.zip
got sorting right
-rw-r--r--card.cpp37
-rw-r--r--game.cpp2
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++;