diff options
-rw-r--r-- | game.cpp | 72 | ||||
-rw-r--r-- | game.h | 2 | ||||
-rw-r--r-- | main.cpp | 2 |
3 files changed, 66 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++; +} + @@ -63,6 +63,7 @@ class Game : virtual public CardPile, public User, public Opponent bool hasTwoPairs(Card **); void evaluateHand(Card **, int); void showHands(void); + void determineWinner(void); /* variables */ int handsPlayed; @@ -70,6 +71,7 @@ class Game : virtual public CardPile, public User, public Opponent static string *hands[10]; static string *suits[5]; + static string *winmsg[4]; }; #endif @@ -77,6 +77,8 @@ int main(int argc, char *argv[]) pokerGame->evaluateHand(pokerGame->getOpponentsCards(i+1), i+1); pokerGame->showHands(); + pokerGame->determineWinner(); + /* ask to repeat the game and reset the deck of cards */ pokerGame->repeatGame(&quit); pokerGame->resetDeck(); |