An Example of Good Design Using Modularity & Hiding Details
The Program
/*************************************************************************\
* Filename: cards.c *
* Author: Sue Bogar *
* Date Written: 9/19/00 *
* SSN: 123-45-6789 *
* 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
#include
#include
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
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;
}
/*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;
while( input < min || input > max )
{
printf("Please enter an integer between");
printf(" %d and %d.\n", min, max);
scanf("%d", &input);
}
return input;
}
/*PrintInstructions prints the greeting for the */
/*the grades program */
/*Inputs: none */
/*Outputs: none (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();
**
** 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();
**
** Inputs: None
** Output: the random number generator is seeded
** with time(). 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);
**
** 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 - prints the name of the card
*********************************************/
void PrintCard(int pip, int suit)
{
/*print the pip of the card*/
switch (pip)
{
case 1:
printf ("Ace ");
break;
case 11:
printf ("Jack ");
break;
case 12:
printf ("Queen ");
break;
case 13:
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 1:
printf ("of Hearts\n");
break;
case 2:
printf ("of Spades\n");
break;
case 3:
printf ("of Diamonds\n");
break;
case 4:
printf ("of Clubs\n");
break;
default:
printf("%d - Not Valid\n", suit);
break;
}
}
/******************************************************************
** Function: PrintResults
** Usage: PrintResults(pip, suit, playerPip, playerSuit);
**
** 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 - prints the two cards and the results of
the game (win or lose)
*****************************************************************/
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
- We've built a program using some previously written functions as
modules.
- Properly-designed general purpose functions shouldn't use symbolic
constants within their code.
- Hiding details by making use of functions keeps main() small and
readable.