#include "Deck.h" #include "Card.h" #include int main(void) { Deck cards; // this is our deck of cards. int score = 0; // this keeps track of the players score. char cont = 'y'; /* and this keeps track of whether or not the player wants * to continue the game... */ int cardnum1,cardnum2,cardnum3,cardnum4; // these store the cards read from the user Card *card1,*card2,*card3,*card4; // these store the actual cards taken from the deck // to start, we shuffle the deck... cards.shuffle(); // and loop while the player still wants to play AND there are still cards // left in the deck. while (( cont != 'n') && (cards.size() > 0)) { // here we read the next four cards from the user cout << "\nCards remaining: 1 though " << cards.size() << ".\n"; cout << "Select card 1: "; cin >> cardnum1; cout << "Select card 2: "; cin >> cardnum2; cout << "Select card 3: "; cin >> cardnum3; cout << "Select card 4: "; cin >> cardnum4; // we then check them for basic sanity, make sure they are unique and that // they are within the valid range of cards left in the deck. if ((cardnum1 != cardnum2) && (cardnum2 != cardnum3) && (cardnum3 != cardnum4)) { if ((cardnum1 <= cards.size()) && (cardnum2 <= cards.size()) && (cardnum3 <= cards.size()) && (cardnum4 <= cards.size())) { // we now show the luser which cards were chosen card1 = cards.get_card(cardnum1); cout << "\nCard 1 is: "; card1->show(); card2 = cards.get_card(cardnum2); cout << "Card 2 is: "; card2->show(); card3 = cards.get_card(cardnum3); cout << "Card 3 is: "; card3->show(); card4 = cards.get_card(cardnum4); cout << "Card 4 is: "; card4->show(); cout << "\n"; // and then calculate the score. // did the user match the rank of all four cards? if ((card1->get_rank() == card2->get_rank()) && (card2->get_rank() == card3->get_rank()) && (card3->get_rank() == card4->get_rank())) { score += 1000; cout << "all cards are the same rank, you score 1000 points.\n"; } // did the user match the suit of all four cards? else if ((card1->get_suit() == card2->get_suit()) && (card2->get_suit() == card3->get_suit()) && (card3->get_suit() == card4->get_suit())) { score += 10; cout << "all cards are the same suit, you score 10 points.\n"; } // did the user match nothing at all? else { score -= 7; cout << "cards do not have matching rank or suit, you lose 7 points.\n"; } // now we pack the deck to remove empty the empty slots, and we are ready // go again... cards.pack(); } else { cout << "card value out of range.\n"; } } else { cout << "card requests must be unique.\n"; } // the the user her score cout << "You have " << score << " points. "; if (cards.size() > 0) { // no need to prompt if there are no cards left... // ask the user if the want to continue cout << "Continue game? (y/n): "; cin >> cont; } else { cout << "No more cards in deck.\n"; } } cout << "Game ended. Score: " << score << " points.\n"; }