L-14 MCS 260 Fri 21 Sep 2001

In the second half of the lecture we sketched how a program can be distributed over several files. First we have the file "heads_or_tails.h" :
/* L-14 MCS 260 Fri 21 Sep 2001 : prototypes for heads or tails game */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void  prn_instructions ( void );
/* instructions about the game are displayed on screen */

int   get_guess_from_user ( void );
/* user is prompted for a guess, which is then returned */

void  play ( int n );
/* n coin tosses and guess are executed */

void prn_final_report ( int n, int wins, int losses );
/* prints #wins, #losses, and total number of n games */
After the prototypes we normally have the main program. For this application, the main program is in the separate file "heads_or_tails.c" :
/* L-14 MCS 260 Fri 21 Sep 2001 : heads or tails game

This main program needs the files heads_or_tails.h, which
contains prototypes for the function whose definitions are
in the files prn.c, get.c, and play.c.

To create the game, type

   gcc -o game heads_or_tails.c get.c prn.c play.c

To see if one of the .c files, say get.c, compiles correctly,
type

   gcc -c get.c

*/

#include "heads_or_tails.h"

int main(void)
{
   int   no_of_plays;

   printf("\nWelcome to the game of head and tails.\n\n");

   prn_instructions();

   printf("How many times you wish to play ? ");
   scanf("%d", &no_of_plays);

   play(no_of_plays);

   return 0;
}
We group the printing functions in one file : "prn.c" :
/* L-14 MCS 260 Fri 14 Sep 2001 : printing in heads or tails game 

This file collects the printing of instructions and the printing
of the final report after the game is done.                       */

#include "heads_or_tails.h"

void prn_instructions ( void )
{
   printf("%s\n",
     "This is the game of calling heads or tails.\n"
     "I will flip a coin; you call it.\n"
     "If you call it correctly, you win;\n"
     "otherwise, I win.\n\n"
     "As I toss the coin, I will tell you to \"call it.\"\n"
     "To call heads, type 0; to call tails, type 1.\n"
     "Enjoy.\n");
}

void prn_final_report ( int n, int wins, int losses )
{
   printf("\n%s\n%s%3d\n%s%3d\n%s%3d\n\n",
     "Final Report :",
     "   number of games you won  : ", wins,
     "   number of games you lost : ", losses,
     "   total number of games    : ", n);
}
The guess of the user is checked for errors, the function is defined in the file "get.c" :
/* L-14 MCS 260 Fri 21 Sep 2001 : get the guess of the user */

#include "heads_or_tails.h"

int get_guess_from_user(void)
{
   int guess;           /* 0 = heads, 1 = tails */

   for (;;)
   {
      printf("Call it heads (0) or tails (1) : ");
      if (scanf("%d", &guess) != 1)
      {
         printf("The input is not a number - bye!\n");
         exit(1);       /* wrongful termination of program */
      }
      if (guess != 0 && guess != 1)
         printf("%s%s","Type 0 for heads, 1 for tails.\n",
                       "Please try again.\n");
      else
         return guess;
   }
}
The loop is implemented by the function play, in the file "play.c". Note that play calls the functions get_guess_from_user and prn_final_results.
/* L-14 MCS 260 Fri 21 Sep 2001 : executing n coin tosses */

#include "heads_or_tails.h"

void play ( int n )
{
   int coin, i, losses = 0, wins = 0;

   srand(time(NULL));

   for (i = 0; i < n; i++)
   {
      coin = rand() % 2;
      if (get_guess_from_user() == coin)
      {
         wins++;
         printf("  Congratulations, you win.\n");
      }
      else
      {
         losses++;
         printf("  Sorry, you lose.\n");
      }
   }
   prn_final_report(n,wins,losses);
}