UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

An Example of Good Design Using Modularity & Hiding Details

The Program

/*************************************************************************\ * Filename: cards.c * * Author: Sue Bogar * * Date Written: 9/19/00 * * Section #: 101 * * Email: bogar@cs.umbc.edu * * * * This program will simulate the drawing of a card randomly from a deck * * of 52 cards, ask the user to guess that card, and report either a win * * or a loss. * \*************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <time.h> int GetValidInt (int min, int max); void PrintInstructions (void); void SetRandomSeed (void); int GetRandomNumber (int low, int high); void PrintCard(int pip, int suit); void PrintResults (int pip, int suit, int playerPip, int playerSuit); #define MINPIP 1 #define MAXPIP 13 #define MINSUIT 1 #define MAXSUIT 4 #define ACE 1 #define JACK 11 #define QUEEN 12 #define KING 13 #define HEARTS 1 #define SPADES 2 #define DIAMONDS 3 #define CLUBS 4 int main ( ) { int pip, suit, playerPip, playerSuit; /*seed the random number generator*/ SetRandomSeed( ); /*computer draws a card randomly*/ pip = GetRandomNumber (MINPIP, MAXPIP); suit = GetRandomNumber (MINSUIT, MAXSUIT); /*greet user and get the user's card*/ PrintInstructions( ); printf("Pick the face value of your card.\n"); playerPip = GetValidInt (MINPIP, MAXPIP); printf("Pick the suit of your card.\n"); playerSuit = GetValidInt (MINSUIT, MAXSUIT); PrintResults(pip, suit, playerPip, playerSuit); return 0; } /*Function: GetValidInt() */ /*GetValidInt gets an integer from the user between */ /*specified minimum and maximum values, and will */ /*reprompt the user until it gets one that is valid.*/ /* */ /*Inputs: min and max, the minimum and maximum */ /* (inclusive) values for the entered integer */ /*Outputs: returns an integer between min and max, */ /* inclusively */ int GetValidInt(int min, int max) { /* is set greater than max so the loop will be entered*/ int input = max + 1; /* Loop assures valid input */ while( input < min || input > max ) { printf("Please enter an integer between"); printf(" %d and %d.\n", min, max); scanf("%d", &input); } return input; } /*Function: PrintInstructions() */ /* */ /*PrintInstructions prints the greeting for the */ /*the grades program */ /* */ /*Inputs: none */ /*Outputs: none (Side-effect: prints to screen) */ void PrintInstructions (void) { printf("\n\n"); printf("***************************************\n"); printf("*This program draws a card randomly *\n"); printf("*from a deck of 52 cards. You should *\n"); printf("*guess a card by entering a number *\n"); printf("*between 1 and 13 that shows the face *\n"); printf("*value of the card, where Jack is 11, *\n"); printf("*Queen is 12, and King is 13 and then *\n"); printf("*Indicate the suit of the card using *\n"); printf("*1 through 4, where a 1 is hearts, a *\n"); printf("*2 is spades, 3 is diamonds and 4 is *\n"); printf("*clubs. A win or a loss is declared. *\n"); printf("***************************************\n\n"); } /********************************************* ** Function: GetRandomNumber ** Usage: x = GetRandomNumber(); ** ** GetRandomNumber() returns a pseudo-random integer ** between the values passed into the function: low and ** high, inclusive. ** ** Inputs: low - an int which is the lowest value ** in the range of acceptable random numbers ** high - an int which is the highest value ** in the range of acceptable random numbers ** Output: returns a random integer between low and ** high, inclusive ** ** Assumptions: ** random number generator has been seeded *********************************************/ int GetRandomNumber (int low, int high) { int num ; /* Call rand() to get a large random number. */ num = rand() ; /* Scale the random number within range. */ num = num % (high - low + 1) + low ; return num ; } /********************************************* ** Function: SetRandomSeed ** Usage: SetRandomSeed(); ** ** The pseudo-random number generator is seeded ** with a call to time() assuring a different ** seed everytime the program runs. ** ** Inputs: None ** Output: No value is returned *********************************************/ void SetRandomSeed (void) { int timeSeed ; /* Use the time function to set the seed. */ timeSeed = (int) time(0) ; srand(timeSeed) ; } /********************************************* ** Function: PrintCard ** Usage: PrintCard(pip, suit); ** ** Prints the name of the card. ** ** Inputs: pip - an integer between 1 and 13 ** indicating and Ace - King ** suit - an integer between 1 and 4 ** indicating the card's suit ** Output: None ** ** Assumptions: Relies on #defines for suits and ** Ace, Jack, Queen & King. *********************************************/ void PrintCard(int pip, int suit) { /*print the pip of the card*/ switch (pip) { case ACE: printf ("Ace "); break; case JACK: printf ("Jack "); break; case QUEEN: printf ("Queen "); break; case KING: printf ("King "); break; case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: printf ("%2d ", pip); break; default: printf("%d - Not Valid\n", pip); break; } /*print the suit of the card*/ switch (suit) { case HEARTS: printf ("of Hearts\n"); break; case SPADES: printf ("of Spades\n"); break; case DIAMONDS: printf ("of Diamonds\n"); break; case CLUBS: printf ("of Clubs\n"); break; default: printf("%d - Not Valid\n", suit); break; } } /****************************************************************** ** Function: PrintResults ** Usage: PrintResults(pip, suit, playerPip, playerSuit); ** ** Prints the two cards and the results of the game (win or lose) ** ** Inputs: pip - an integer between 1 and 13 indicating ** Ace - King that was drawn by the computer ** suit - an integer between 1 and 4 indicating ** the card's suit that was drawn by the computer ** playerPip - an integer between 1 and 13 ** indicating Ace - King chosen by the user ** playerSuit - an integer between 1 and 4 ** indicating the card's suit chosen by the user ** Output: None *****************************************************************/ void PrintResults (int pip, int suit, int playerPip, int playerSuit) { /*print the cards*/ printf("\n\nI picked the "); PrintCard(pip, suit); printf("and you picked the "); PrintCard(playerPip, playerSuit); /*compare the cards and declare winner pr loser*/ if (pip == playerPip && suit == playerSuit) { printf("\nYou Win !!!\n\n"); } else { printf("\nSorry, you lose\n\n"); } }

The Output

*************************************** *This program draws a card randomly * *from a deck of 52 cards. You should * *guess a card by entering a number * *between 1 and 13 that shows the face * *value of the card, where Jack is 11, * *Queen is 12, and King is 13 and then * *Indicate the suit of the card using * *1 through 4, where a 1 is hearts, a * *2 is spades, 3 is diamonds and 4 is * *clubs. A win or a loss is declared. * *************************************** Pick the face value of your card. Please enter an integer between 1 and 13. 10 Pick the suit of your card. Please enter an integer between 1 and 4. 4 I picked the Jack of Diamonds and you picked the 10 of Clubs Sorry, you lose

The Lesson


CSEE | 201 | 201 F'06 | lectures | news | help

Thursday, 21-Sep-2006 13:21:14 EDT