/*************************************************************************\ * Filename: bigcards.c * * Author: Sue Evans * * Date Written: 9/19/00 * * Modified: 9/18/08 * * Section #: 02XX * * 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 ** Output: 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 instructions ** for the cards program ** ** Inputs: none ** Output: 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 *********************************************/ 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 or loser*/ if (pip == playerPip && suit == playerSuit) { printf("\nYou Win !!!\n\n"); } else { printf("\nSorry, you lose\n\n"); } }
/*************************************************************************\
* Filename: separate.c *
* Author: Sue Evans *
* Date Written: 9/17/08 *
* Section #: 02XX *
* 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 "cards.h"
#include "util.h"
#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;
}
Explain
/*************************************************************************\
* Filename: cards.c *
* Author: Sue Evans *
* Date Written: 9/17/08 *
* Section #: 02XX *
* Email: bogar@cs.umbc.edu *
* *
* This file contains the function definitions that pertain to cards that *
* are needed to play the simple card game found in separate.c *
* *
* PrintInstructions() prints instructions for the player *
* PrintCards() prints the pip and suit of a card *
* PrintResults() prints the cards chosen by the computer and the *
* player and whether the player won or lost *
\*************************************************************************/
#include <stdio.h>
#include "cards.h"
#define ACE 1
#define JACK 11
#define QUEEN 12
#define KING 13
#define HEARTS 1
#define SPADES 2
#define DIAMONDS 3
#define CLUBS 4
/*********************************************
** Function: PrintInstructions()
**
** PrintInstructions() prints the instructions
** for the cards program
**
** Inputs: none
** Output: 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: 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");
}
}
Explain
/*************************************************************************\
* Filename: util.c *
* Author: Sue Evans *
* Date Written: 9/18/08 *
* Section #: 02XX *
* Email: bogar@cs.umbc.edu *
* *
* This file contains functions that are modules to be used with many *
* C programs. As new useful modules are written, we'll add them to this *
* file. Modules should never be removed from this file even if they are *
* not being used with your current program. *
* *
* Currently this file contains: *
* GetValidInt() - a function that prompts the user to enter an *
* integer value in the range of two integers passed *
* in as arguments and will assure the value returned *
* is within that range, inclusive. *
* SetRandomSeed() - a function that seeds the random number generator *
* using a value from time() as the seed. *
* GetRandomNumber() - this function returns a random number within *
* the range of the arguments passed in. *
\*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "util.h"
/*********************************************
** 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
** Output: 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: 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: 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
*********************************************/
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 ;
}
Explain
/*************************************************************************\
* Filename: cards.h *
* Author: Sue Evans *
* Date Written: 9/17/08 *
* Section #: 02XX *
* Email: bogar@cs.umbc.edu *
* *
* This file contains the function prototypes for the functions defined in *
* the cards.c file. *
\*************************************************************************/
/*********************************************
** Function: PrintInstructions()
**
** PrintInstructions() prints the instructions
** for the cards program
**
** Inputs: none
** Output: none (Side-effect: prints to screen)
*********************************************/
void PrintInstructions (void);
/*********************************************
** Function: PrintCard
** Usage: PrintCard(pip, suit);
**
** Prints the name of the card.
**
** Inputs: pip - an integer between 1 and 13
** indicating 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);
/******************************************************************
** 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);
Explain
/*************************************************************************\
* Filename: util.h *
* Author: Sue Evans *
* Date Written: 9/18/08 *
* Section #: 02XX *
* Email: bogar@cs.umbc.edu *
* *
* This file contains the prototypes for the functions defined in util.c *
\*************************************************************************/
/*********************************************
** 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
** Output: returns an integer between min and max,
** inclusively
*********************************************/
int GetValidInt(int min, int max);
/*********************************************
** 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);
/*********************************************
** 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
*********************************************/
int GetRandomNumber (int low, int high);
linuxserver1.cs.umbc.edu[101] ls
bigcards.c cards.c cards.h separate.c util.c util.h
linuxserver1.cs.umbc.edu[102] gcc -c -Wall -ansi separate.c
linuxserver1.cs.umbc.edu[103] gcc -c -Wall -ansi cards.c
linuxserver1.cs.umbc.edu[104] gcc -c -Wall -ansi util.c
linuxserver1.cs.umbc.edu[105] ls
bigcards.c cards.h separate.c util.c util.o
cards.c cards.o separate.o util.h
linuxserver1.cs.umbc.edu[106] gcc separate.o cards.o util.o
linuxserver1.cs.umbc.edu[107] ls
a.out cards.c cards.o separate.o util.h
bigcards.c cards.h separate.c util.c util.o
linuxserver1.cs.umbc.edu[108]
Explain
We are also using these symbolic constants to restrict the user's input
to valid values of pips and suits by passing them into GetValidInt().
"This file contains the function definitions that pertain to cards
that are needed to play the simple card game found in separate.c"
It is also typical to list the functions in the file with a brief
explanation of the purpose of each.
PrintInstructions() prints instructions for the player
PrintCards() prints the pip and suit of a card
PrintResults() prints the cards chosen by the computer and the
player and whether the player won or lost
"This file contains functions that are modules to be used with many
C programs. As new useful modules are written, we'll add them to
this file. Modules should never be removed from this file even if
they are not being used with your current program."
It is also typical to list the functions in the file with a brief
explanation of the purpose of each.
All of the same ideas apply.
linuxserver1.cs.umbc.edu[101] ls
bigcards.c cards.c cards.h separate.c util.c util.h
linuxserver1.cs.umbc.edu[102] gcc -c -Wall -ansi separate.c
linuxserver1.cs.umbc.edu[103] gcc -c -Wall -ansi cards.c
linuxserver1.cs.umbc.edu[104] gcc -c -Wall -ansi util.c
The -c flag means to compile the file but not to link.
If you forget the -c flag, error messages will be produced about not being
able to find the functions being called when trying to compile the file
containing main(), in this case separate.c.
When trying to compile the files that don't contain main(), in this case
cards.c and util.c error messages will be thrown about not finding a
main().
linuxserver1.cs.umbc.edu[105] ls
bigcards.c cards.h separate.c util.c util.o
cards.c cards.o separate.o util.h
linuxserver1.cs.umbc.edu[106] gcc separate.o cards.o util.o
linuxserver1.cs.umbc.edu[107] ls
a.out cards.c cards.o separate.o util.h
bigcards.c cards.h separate.c util.c util.o
linuxserver1.cs.umbc.edu[108]