// cppHearts, Copyright 2001 Michael W. Godfrey, email: migod@uwaterloo.ca // See copyright.txt for details. #include "SmartPlayer.h" #include "RandomPlayer.h" #include "Deck.h" #include "Card.h" #include #include enum PlayerKind {Random, Smart}; // These are the player names used for the demo. const string PlayerNames [] = {"Abe", "Bart", "Crusty", "D'oh"}; // Change these to suit yerself, he said rankly. // const PlayerKind PlayerSkill [] = {Random, Smart, Random, Random}; const PlayerKind PlayerSkill [] = {Smart, Smart, Smart, Smart}; const int numPlayers = sizeof(PlayerNames) / sizeof(PlayerNames[0]); // This is the main program for the hearts game. int main (int argc, char* argv[]) { // This says, use this seed to generate random numbers. // Changing the seed (in Globals) will give you a different sequence of // random numbers, and therfore a different game. srand(Globals::RandomSeed); // Initialize the players. Player *player[numPlayers]; for (int i=0; isetPlayerToMyLeft (player [(i+1) % numPlayers]); } // Now set up the deck and deal. Deck *deck = new Deck(); int numHands = 0; Player* curDealer = player [0]; // We will use ``break'' to leave this loop if/when a player's // score exceeds Globals::MaxPoints points. See below. while (true) { numHands += 1; deck->shuffle(); // A small cheat: we don't actually delete the cards from the // deck when we deal them, but we do reshuffle each round. Player* curPlayer = curDealer; for (vector::const_iterator viter=deck->begin(); viter!=deck->end(); viter++){ Card *nextCard = (*viter); curPlayer->addCard (nextCard); curPlayer = curPlayer->getPlayerToMyLeft(); } // Now poll for who has lead. for (int i=0; ihasTwoOfClubs()) { curPlayer = player [i]; break; } } // Now print out the starting hands of the players. cout << "\n\nHere are the hands of each player for hand number " << numHands << endl << endl; for (int i=0; igetName() << " has these cards:" << endl; player[i]->printHand(); cout << endl; } // Now play all of the rounds of this hand. // We will count up the scores at the end of the hand. const int numRounds = Card::numCards / numPlayers; for (int i=1; i <= numRounds; i++) { // The trick will contain at most numRounds cards. Trick *currentTrick = new Trick(); cout << "\nNow starting round " << i << " of hand " << numHands << endl; for (int j=0; jplayCard (currentTrick); curPlayer = curPlayer->getPlayerToMyLeft(); } // Now figure out who won this trick and give him/her the // cards; (s)he also gets to lead next time around. curPlayer = currentTrick->getTrumper(); curPlayer->addTrick (currentTrick); } // Print out the scores at the end of the hand. cout << "\nAt the end of hand " << numHands << ", the score is:" << endl; for (int i=0; icountAndAddPoints(); cout << " " << player[i]->getName() << " " << player[i]->getPoints() << endl; } // Check if someone is over Globals.MaxPoints points. int maxPts = player[0]->getPoints(); int minPts = player[0]->getPoints(); int maxPlayer = 0, minPlayer = 0; // Start at player 1, since we've already looked at player 0. for (int i=1; igetPoints() > maxPts) { maxPts = player[i]->getPoints(); maxPlayer = i; } else if (player[i]->getPoints() < minPts) { minPts = player[i]->getPoints(); minPlayer = i; } } if (maxPts >= Globals::MaxPoints){ cout << "\nThe game is over." << endl; cout << player[maxPlayer]->getName() << " has exceeded " << Globals::MaxPoints << " points." << endl; cout << "The winner is " << player[minPlayer]->getName() << endl; // Leave the main loop, we are all done! // This breaks out of the outer ``while(true)'' loop. break; } // Now ask for everyone's cards back for (int i=0; iresetHand(); } // Deal passes to the left. curDealer = curDealer->getPlayerToMyLeft(); } }