diff options
author | Kyle K <kylek389@gmail.com> | 2011-01-30 19:00:50 -0600 |
---|---|---|
committer | Kamil Kaminski <kamilkss@gmail.com> | 2011-01-30 19:00:50 -0600 |
commit | 63344c3ae40119b5f9513b39e9fb81a55c556940 (patch) | |
tree | 536125a86a7dd170baea69b804657e4607e042c7 /game.cpp | |
parent | 16e2253bd1d1b77c45c3213b0cc60aeb3ac0f13d (diff) | |
download | poker-63344c3ae40119b5f9513b39e9fb81a55c556940.tar.gz poker-63344c3ae40119b5f9513b39e9fb81a55c556940.tar.bz2 poker-63344c3ae40119b5f9513b39e9fb81a55c556940.zip |
begin implementing determineWinner, fix few thinkos
Diffstat (limited to 'game.cpp')
-rw-r--r-- | game.cpp | 72 |
1 files changed, 62 insertions, 10 deletions
@@ -10,8 +10,9 @@ /* forward declaration of hands array, very interesting, I didn't know C++ needs this */ string *Game::hands[10]; string *Game::suits[5]; +string *Game::winmsg[4]; -Game::Game(void) +Game::Game(void) : handsPlayed(0), handsWon(0) { hands[0] = new string("Royal Flush"); hands[1] = new string("Straight Flush"); @@ -29,6 +30,11 @@ Game::Game(void) suits[2] = new string("Diamonds"); suits[3] = new string("Hearts"); suits[4] = new string("Spades"); + + winmsg[0] = new string("User is the winner!"); + winmsg[1] = new string("Opponent 1 is the winner!"); + winmsg[2] = new string("Opponent 2 is the winner!"); + winmsg[3] = new string("Opponent 3 is the winner!"); } void Game::shufflePile(void) @@ -353,6 +359,12 @@ struct countAndType Game::numOfSameType(Card **cards) whereMaxWasFound = i; } +#ifdef DEBUG + cout << ANSI_COLOR_CYAN << "\ndebug: \"" << same[whereMaxWasFound].type + << "\" has highest occurence, occurs " << same[whereMaxWasFound].count + << " times" << ANSI_COLOR_RESET; +#endif + return same[whereMaxWasFound]; } @@ -769,6 +781,7 @@ void Game::evaluateHand(Card **cards, int who) bool haveAce = doWeHaveAnAce(cards); bool inOrderByOne = isInOrderByOne(cards); + struct countAndSuit tmp = numOfSameSuit(cards); struct countAndType tmp2 = numOfSameType(cards); @@ -777,35 +790,35 @@ void Game::evaluateHand(Card **cards, int who) whatHand = 0; /* straight flush */ - if (!haveAce && inOrderByOne && (tmp.count == 5)) + else if (!haveAce && inOrderByOne && (tmp.count == 5)) whatHand = 1; /* four of a kind */ - if (tmp2.count == 4) + else if (tmp2.count == 4) whatHand = 2; /* full house */ - if ((tmp2.count == 3) && (cards[3]->getType()[0] == cards[4]->getType()[0])) + else if ((tmp2.count == 3) && (cards[3]->getType()[0] == cards[4]->getType()[0])) whatHand = 3; /* flush */ - if (tmp.count == 5) + else if (tmp.count == 5) whatHand = 4; /* straight */ - if (inOrderByOne) + else if (inOrderByOne) whatHand = 5; /* three of a kind */ - if (tmp2.count == 3) + else if (tmp2.count == 3) whatHand = 6; /* two pair */ - if (hasTwoPairs(cards)) + else if (hasTwoPairs(cards)) whatHand = 7; /* one pair */ - if (tmp2.count == 2) + else if (tmp2.count == 2) whatHand = 8; else @@ -832,10 +845,49 @@ void Game::evaluateHand(Card **cards, int who) void Game::showHands(void) { - cout << "\nUser has: " << *hands[getUserHand()] << " " << endl; + cout << "\n User has: " << *hands[getUserHand()] << " " << endl; int i; for (i = 0; i < numOfOpponents; i++) cout << "Opponent " << i+1 << " has: " << *hands[getOpponentHand(i+1)] << " " << endl; } + +void Game::determineWinner(void) +{ + /* check for 'whatHand' variable from User and Opponent class */ + + int numOfPlayers = 1 + numOfOpponents; + int *playerHands = new int[numOfPlayers](); + + int i; + for (i = 0; i < numOfPlayers; i++) + playerHands[i] = 0; + + playerHands[0] = getUserHand(); + for (i = 1; i < numOfOpponents + 1; i++) + playerHands[i] = getOpponentHand(i); + + bool *ties = new bool[numOfPlayers](); + for (i = 0; i < numOfPlayers; i++) + ties[i] = false; + + /* look for lowest hand */ + int min = 9; + int whatIndex = -1; + + for (i = 0; i < numOfPlayers; i++) + if (playerHands[i] < min) + { + min = playerHands[i]; + whatIndex = i; + } + + cout << ANSI_COLOR_CYAN << endl << *winmsg[whatIndex] << endl + << ANSI_COLOR_RESET; + + if (whatIndex == 0) + handsWon++; + handsPlayed++; +} + |