Card-Dealing Engine


Example 14.3

Step 1: Enter the following code into your favorite text editor.

#include <iostream> #include <ctime> using namespace std; // prototype functions void dealcards(); int main() { dealcards(); return 0; }// end of main void dealcards() { int cardused[52];   // this array contains the cards                     // that have already been used. int cards;     // cards  int face;     // The face of the card selected int suit;     // The suit of the card selected char choice;   // deal again or not       // You must seed the random function by // calling srand and passing it some value. // Time is the one most commonly used. srand(time(NULL)); cout << "Here are the cards" <<endl;   for(int i = 0; i < 52; i++) {            cards = rand()%52;       // randomize 52 cards        while (cardused[cards] ==1)      {   cards = rand()%52;       //changing the cards  }   cardused[cards] = 1;   face = cards%13;       // calculating the      // face cards   suit = cards/13;       // calculating the      // suit cards             // the couts use the ascii codes for various symbols       // to out put the suit.       switch(suit)       {       case 0:        cout << '\005';        break;               case 1:        cout << '\006';        break;             case 2:    cout << '\x03';        break;             case 3:        cout << '\004';    break;             }// end of switch             switch(face)       {        case 0:           cout << "King ";      break;          case 1:      cout << "Ace ";      break;                    case 2:      cout << "Two ";      break;              case 3:      cout << "Three ";      break;              case 4:      cout << "Four ";      break;              case 5:      cout << "Five ";      break;              case 6:      cout << "Six ";      break;              case 7:      cout << "Seven ";      break;              case 8:      cout << "Eight ";      break;              case 9: cout << "Nine ";      break;              case 10:      cout << "Ten ";      break;              case 11:      cout << "Jack ";      break;              case 12:      cout << "Queen ";      break;       }// end of switch        cout << endl;       }// for loop cout << endl << endl; cout << "Would you like to deal again? Type y for     yes";   cout << " n for no \n"; cin >> choice;   if (choice == 'y')      dealcards(); else      return; }// end of the dealcards function

Step 2: Compile and run the code. You should see something like what is depicted in Figure 14.5.

click to expand
Figure 14.5: Dealing cards.

You should notice several things about this code. First of all, you should notice that the DealCards function is mostly contained within a for loop. This loop executes 52 times, thus dealing out 52 cards. Obviously for certain card games you would wish to deal 2, 5, 7, or an appropriate number of cards for your game. This is also another example of a game that has a for loop as its most critical component. You should also notice that the first switch statement, the one that picks the face of the card, uses ASCII codes to print-out the symbols for the faces of the cards. It uses these codes to actually print-out a heart, diamond, spade, etc. Again, although this example is not a fully functioning card game, it is the most important component to any card game you may wish to create. Now only your own creative thinking, and, of course, your knowledge of card games, stands between you and the creation of your own computer card games.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net