summaryrefslogtreecommitdiffstats
path: root/game.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'game.cpp')
-rw-r--r--game.cpp166
1 files changed, 166 insertions, 0 deletions
diff --git a/game.cpp b/game.cpp
index ce76406..e3a1266 100644
--- a/game.cpp
+++ b/game.cpp
@@ -612,3 +612,169 @@ void Game::userAI(Card **cards)
}
}
+bool Game::isInOrderByOne(Card **cards)
+{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\nisInOrderByOne(): cards parameter is NULL!\n");
+ return false;
+ }
+
+ int back1, back2;
+ back1 = cards[0]->getRank();
+
+ int i;
+ for (i = 1; i < 5; i++)
+ {
+ back2 = cards[i]->getRank();
+ if ((back2 + 1) != back1)
+ return false;
+
+ back1 = back2;
+ }
+
+ return true;
+}
+
+bool Game::isInOrder(Card **cards)
+{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\nisInOrder(): cards parameter is NULL!\n");
+ return false;
+ }
+
+ int back1, back2;
+ back1 = cards[0]->getRank();
+
+ int i;
+ for (i = 1; i < 5; i++)
+ {
+ back2 = cards[i]->getRank();
+ if (!(back2 < back1))
+ return false;
+
+ back1 = back2;
+ }
+
+ return true;
+}
+
+bool Game::hasTwoPairs(Card **cards)
+{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\nhasTwoPairs(): cards parameter is NULL!\n");
+ return false;
+ }
+
+ char c1, c2;
+ int ret = 0;
+ int whitelist[4];
+ whitelist[0] = 0;
+ whitelist[1] = 0;
+ whitelist[2] = 0;
+ whitelist[3] = 0;
+
+ int i, j;
+ for (i = 0; i < 5; i++)
+ {
+ if (whitelist[i] == 1)
+ continue;
+
+ c1 = cards[i]->getType()[0];
+
+ for (j = i+1; j < 5; j++)
+ {
+ if (whitelist[j] == 1)
+ continue;
+
+ c2 = cards[j]->getType()[0];
+ if (c1 == c2)
+ {
+ ret++;
+ whitelist[j] = 1;
+ whitelist[i] = 1;
+ }
+ }
+ }
+
+ return (ret != 0);
+}
+
+/* second param, 0 = user, 1~3 designate opponent */
+void Game::evaluateHand(Card **cards, int who)
+{
+ int whatHand = 9;
+
+ bool haveAce = doWeHaveAnAce(cards);
+ bool inOrderByOne = isInOrderByOne(cards);
+ struct countAndSuit tmp = numOfSameSuit(cards);
+
+ /* royal flush */
+ if (haveAce && inOrderByOne && (tmp.count == 5))
+ whatHand = 0;
+
+ /* straight flush */
+ if (!haveAce && inOrderByOne && (tmp.count == 5))
+ whatHand = 1;
+
+ /* four of a kind */
+ if (tmp.count == 4)
+ whatHand = 2;
+
+ /* full house */
+ if ((tmp.count == 3) && (cards[3]->getType()[0] == cards[4]->getType()[0]))
+ whatHand = 3;
+
+ /* flush */
+ if (tmp.count == 5)
+ whatHand = 4;
+
+ /* straight */
+ if (inOrderByOne)
+ whatHand = 5;
+
+ /* three of a kind */
+ if (tmp.count == 3)
+ whatHand = 6;
+
+ /* two pair */
+ if (hasTwoPairs(cards))
+ whatHand = 7;
+
+ /* one pair */
+ if (tmp.count == 2)
+ whatHand = 8;
+
+ else
+ whatHand = 9;
+
+ switch (who)
+ {
+ case 0:
+ setUserHand(whatHand);
+ break;
+ case 1:
+ setOpponentHand(whatHand, 1);
+ break;
+ case 2:
+ setOpponentHand(whatHand, 2);
+ break;
+ case 3:
+ setOpponentHand(whatHand, 3);
+ break;
+ default:
+ cout << "something went wrong in evaluation" << endl;
+ }
+}
+
+void Game::showHands(void)
+{
+ cout << "User has: " << *hands[getUserHand()] << " " << endl;
+
+ int i;
+ for (i = 0; i < numOfOpponents; i++)
+ cout << "Opponent " << i+1 << " has: " << *hands[getOpponentHand(i+1)] << " " << endl;
+}
+