home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / games / table / mst / mst.c next >
Encoding:
C/C++ Source or Header  |  1991-05-29  |  26.9 KB  |  921 lines

  1. /*
  2.  * May 28, 1991
  3.  *                         _   . . _   _  __
  4.  *                        /_)_(_/_/_)_</_/ (_
  5.  *                               /
  6.  *                              '
  7.  *
  8.  *                            _/_                              
  9.  *           ______  __.  _   /  _  __      ______  o ____  __/
  10.  *          / / / <_(_/|_/_)_<__</_/ (_    / / / <_<_/ / <_(_/_
  11.  *
  12.  *
  13.  *
  14.  * 
  15.  *                                    Another public domain game
  16.  *                                             by
  17.  *                                             Harry Karayiannis
  18.  *
  19.  *
  20.  *
  21.  *  This is my implementation of the famous game of Master-Mind. I have
  22.  * tried to make the code as portable as possible, so it can compile
  23.  * on any machine with minimal changes. This particular file is meant
  24.  * to be compiled on any ATARI ST with any of the following compilers
  25.  *    o   gcc
  26.  *    o   Mark Williams C     (I used this one)
  27.  *    o   Sozobon C
  28.  *    o   Megamax C
  29.  *    o   Laser C
  30.  *    o   Alcyon C
  31.  *    o   Turbo C  (just replace the header file OSBIND.H with TOS.H)
  32.  *
  33.  * To compile it on a different machine, just remove the line
  34.  * " #include <osbind.h> "
  35.  * and replace the function Random(), with the one provided by
  36.  * your compiler. Also, you should **rewrite** the function: disk_free()
  37.  * ---------
  38.  * (If you don't know how to code the function disk_free() on your
  39.  *  computer, just use the following code:
  40.  *
  41.  *  long disk_free()
  42.  *  {
  43.  *    return( (long)sizeof(hi_table) );
  44.  *  }
  45.  *
  46.  *  It will work, unless your disk does not have room to save the table
  47.  *  of scorers (normally this is 132 bytes, but it will change if you 
  48.  *  change the values of the macros defined below)
  49.  * )
  50.  *
  51.  *
  52.  *
  53.  *
  54.  * ==================================================================
  55.  *                             Disclaimer
  56.  * ==================================================================
  57.  * "I make no warranty with respect to this file, or the programs 
  58.  * it describes, and disclaim any implied or explicit suggestions of 
  59.  * usefulness for any particular purpose.  Use this software only if 
  60.  * you are willing to assume all risks and damages,  if any, arising 
  61.  * as a result, even if it is caused by negligence or other fault."
  62.  * ==================================================================
  63.  */
  64.  
  65.  
  66.  
  67. #include <stdio.h>
  68. #include <ctype.h>
  69.  
  70. /*
  71.  * the following header file is only included for
  72.  * the machine depended call: Random(). If you
  73.  * compile the program on a computer other than the
  74.  * ST, you may have to use a different header file
  75.  * (or none: I think Turbo C on the IBM PC has a
  76.  *  random function in its standard library)
  77.  */
  78. #include <osbind.h>
  79.  
  80.  
  81. /* MACRO Definitions
  82.  * -----------------
  83.  * if you don't like the current values of the following
  84.  * macros you can change them. The game will be still running fine
  85.  */
  86. #define MAXTURN 12          /* Maximum # of turns */
  87. #define MAXNAME 10          /* Maximum # of letters in player's name  */
  88. #define MAXSYMB  5          /* Maximum # of symbols in hidden pattern */
  89. #define MAXTABL 11          /* Maximum # of entries in table of hi-scores */
  90.  
  91. /*
  92.  * the following macro clears the screen on a VT52 terminal (ST screen).
  93.  * you should modify it according to your terminal's escape sequences
  94.  * (if you are using Turbo C on an IBM PC (or compatible), you should
  95.  *  use the function ClrScr() instead)
  96.  */
  97. #define clear()    putchar(27); putchar('E');
  98.  
  99.  
  100.  
  101. /* TYPE Definitions
  102.  * ---------------- */
  103. typedef struct{           /* current player's name and score */
  104.     int  score;
  105.     char name[MAXNAME];
  106. }REC_TYPE;
  107.  
  108.  
  109.  
  110. /* GLOBAL Variables
  111.  * ---------------- */
  112. long  seedval;            /* used for the random-value calculation  */
  113. FILE  *seedfile;          /* file to keep the seed-value  */
  114. FILE  *hi_file;           /* file holding the High Scores */
  115. FILE  *lastgame;          /* file keeping last player's performance */
  116. int   tab_changed;        /* TRUE when table-of-scores has been changed */
  117. REC_TYPE  hi_table[MAXTABL];  /* table of high scorers */
  118.  
  119.  
  120.  
  121. /* ======================================================================== *
  122.  *                                  main                                    *
  123.  * ======================================================================== */
  124.  
  125. main()
  126. {
  127.   /* VARIABLE Declarations
  128.    * --------------------- */
  129.   REC_TYPE  lastrec;            /* last player's record */
  130.  
  131.   char  name[MAXNAME];          /* player's name */  
  132.   char  secret[MAXSYMB];        /* the hidden pattern */
  133.   char  inptrn[MAXSYMB];        /* the input (guessing) pattern */
  134.   char  hlpsecret[MAXSYMB];     /* - look in function check() - */
  135.   char  hlpinptrn[MAXSYMB];     /* - look in function check() - */
  136.   char  answer[2];
  137.  
  138.   int   same, identical;        /* - look in function check() -  */
  139.   int   errid;                  /* error id */
  140.   int   score, turn, found;
  141.  
  142.  
  143.   /* FUNCTION References
  144.    * ------------------- */
  145.   long  getseed();
  146.   void  gethitab();
  147.   void  welcome(), readstr(), strupper();
  148.   void  init(), info(), error_check();
  149.   void  check(), give_hints(), comments(), show_hitab();
  150.   void  upd_table(), upd_lastfile(), upd_tabfile(), upd_seed();
  151.   REC_TYPE  *getlastgame();
  152.  
  153.  
  154.  
  155.   gethitab(hi_table);                    /* get/init the hi-table  */
  156.   lastrec = *getlastgame();              /* get/init last player's record */
  157.   seedval = getseed();                   /* get/init value of seed */
  158.   strcpy(answer,"Y");
  159.   tab_changed = 0;                       /* init flag to FALSE */
  160.   do{
  161.     init(secret, hlpsecret, &score);     /* set initial values  */
  162.     welcome(hi_table);                   /* show welcome screen */
  163.     if ( answer[0] != 'A' ){
  164.       printf("\tWhat is your name? ");
  165.       readstr(name,MAXNAME);
  166.       strupper(name);
  167.     }
  168.     else{
  169.       printf("\tPress <Return> to start the game  ");
  170.       rewind(stdin);
  171.       getchar();
  172.     }
  173.     info(lastrec,name);
  174.     found = 0;
  175.     turn = 1;
  176.     printf("        ?????           Enter guess # 1: ");
  177.     do{
  178.       readstr(inptrn,MAXSYMB);
  179.       strupper(inptrn);                 /* convert pattern to uppercase */
  180.       error_check(inptrn,&errid,&turn); /* check for any errors */
  181.  
  182.       if ( !strcmp(inptrn,"Q")  ||  !strcmp(inptrn,"QUIT") ){
  183.         score = turn = 0;
  184.         break;
  185.       }
  186.  
  187.       if ( !strcmp(inptrn,"R") )
  188.         strcpy(inptrn,"     ");
  189.  
  190.       strcpy(hlpsecret,secret);
  191.       strcpy(hlpinptrn,inptrn);
  192.       check(hlpsecret, hlpinptrn, &identical, &same);
  193.       score = score -  turn*100 + (5*identical + 3*same);
  194.       printf("%5d   %s   ", score, inptrn);
  195.       give_hints(identical,same);
  196.       if ( !strcmp(secret,inptrn) ){
  197.         found = 1;  /* TRUE */
  198.         break;
  199.       }
  200.       turn++;
  201.       if ( turn<=MAXTURN )
  202.         printf("   Enter guess #%2d: ", turn);
  203.     }while( turn<=MAXTURN  &&  !found );
  204.  
  205.     if (!found){
  206.       printf("\nThe hidden pattern was <%s>", secret);
  207.       score = 0;
  208.     }
  209.     puts("\n----------------------------------------------------------------\
  210. ---------------");
  211.     printf("FINAL SCORE:%-5d >>> ", score);
  212.     comments(turn);
  213.     puts("------------------------------------------------------------------\
  214. -------------");
  215.     upd_table(hi_table, name, score);
  216.     strcpy(lastrec.name, name);
  217.     lastrec.score = score;
  218.     printf("Do you wanna play again (y/n/a)? ");
  219.     rewind(stdin);
  220.     gets(answer);
  221.     strupper(answer);
  222.     seedval = Random();
  223.   }while( answer[0] == 'Y'  ||  answer[0] == 'A' );
  224.  
  225.   puts("\n\n");
  226.   show_hitab(hi_table);
  227.   puts("\n\n");
  228.   upd_lastfile(lastrec);
  229.   upd_tabfile(hi_table);
  230.   upd_seed();
  231.   exit(0);
  232. }
  233.  
  234.  
  235.  
  236. /* ======================================================================== *
  237.  *                                gethitab                                  *
  238.  * ======================================================================== */
  239.  
  240. void gethitab( hi_table )
  241.     REC_TYPE hi_table[];
  242. {
  243. /*
  244.  * load (or initialize) the table of scorers into the array 'hitable[]'
  245.  */
  246.  
  247.   register int i;
  248.  
  249.   /* load the table */
  250.   if ( hi_file=fopen("TABLE.DAT","rb") ){
  251.     for (i=0; i<MAXTABL; i++)
  252.       fscanf(hi_file, "%s%d", hi_table[i].name, &hi_table[i].score);
  253.     fclose(hi_file);
  254.   }
  255.   /* if not on disk, initialize it */
  256.   else
  257.     for (i=0; i<MAXTABL; i++){
  258.       strcpy(hi_table[i].name,"harryk");
  259.       hi_table[i].score = 1100;
  260.     }
  261. }
  262.  
  263.  
  264.  
  265. /* ======================================================================== *
  266.  *                                 getseed                                  *
  267.  * ======================================================================== */
  268.  
  269. long  getseed()
  270. {
  271. /*
  272.  * get the seed value from the disk
  273.  */
  274.  
  275.   long temp;
  276.  
  277.   if ( seedfile=fopen("SEED.DAT","rb") ){
  278.     fscanf(seedfile, "%ld", &temp);
  279.     fclose(seedfile);
  280.     return(temp);
  281.   }
  282.   return( 11224 );
  283. }
  284.  
  285.  
  286.  
  287. /* ======================================================================== *
  288.  *                                getlastgame                               *
  289.  * ======================================================================== */
  290.  
  291. REC_TYPE  *getlastgame()
  292. {
  293. /*
  294.  * get the name and the score of the last player
  295.  */
  296.  
  297.   REC_TYPE *temp;
  298.  
  299.   /* load name and score from disk */
  300.   temp = (REC_TYPE *) malloc( sizeof(REC_TYPE) );
  301.   if ( lastgame=fopen("LAST.DAT","rb") ){
  302.     fscanf(lastgame, "%s%d", temp->name, &(temp->score) );
  303.     fclose(lastgame);
  304.   }
  305.   /* if not on disk, initialize it */
  306.   else{
  307.     strcpy(temp->name,"harryk");
  308.     temp->score = 1100;
  309.   }
  310.  
  311.   return(temp);
  312. }
  313.  
  314.  
  315.  
  316. /* ======================================================================== *
  317.  *                                  welcome                                 *
  318.  * ======================================================================== */
  319.  
  320. void welcome( table )
  321.     REC_TYPE table[];   /* table of scorers */
  322. {
  323. /*
  324.  * display welcome screen and table of scorers
  325.  */
  326.  
  327.   void show_hitab();
  328.  
  329.  
  330.   clear();
  331.  
  332.   /* display the welcome screen */
  333.   puts("\0");
  334.   puts("                      ()   _    _ _ __   __   _ __  ");
  335.   puts("                      /\\  ' )  / ' )  ) /  ` ' )  ) ");
  336.   puts("                     /  )  /  /   /--' /--    /--'  ");
  337.   puts("                    /__/_ (__/   /    (___,  /  \\_  ");
  338.   putchar('\n');
  339.   puts("      _ _ _   __  ()    ______ __   _ __      _ _ _      _   _ __   __ ");
  340.   puts("     ' ) ) ) /  ) /\\      /   /  ` ' )  )    ' ) ) )    | ) ' )  ) /  )");
  341.   puts("      / / / /--/ /  )  --/   /--    /--'      / / / ,---|/   /  / /  / ");
  342.   puts("     / ' (_/  ( /__/_ (_/   (___,  /  \\_     / ' (_  \\_/ \\  /  (_/__/_ ");
  343.   putchar('\n');
  344.  
  345.   /* display the table of scorers */
  346.   show_hitab(  table );
  347. }
  348.  
  349.  
  350.  
  351. /* ======================================================================== *
  352.  *                                  info                                    *
  353.  * ======================================================================== */
  354.  
  355. void info( last, name )
  356.     REC_TYPE last;      /* information about the last player */
  357.     char   name[];      /* name of the current player */
  358. {
  359. /*
  360.  * display some useful information for the gameplay
  361.  */
  362.  
  363.   clear();
  364.   printf("Last Player was: %10s - %5d pts\t * - Right symbol,right place\n",
  365.                                                     last.name, last.score);
  366.   printf("Current  Player: %10s          \t\t# - Right symbol,wrong place\n",
  367.                                                                      name);
  368.   puts("----------------------------------------------------------------------\
  369. ---------");
  370.   puts("SCORE  PATTERN  HINTS   (valid symbols ->  A, B, C, D, E, F, G, H)");
  371.   puts("----------------------------------------------------------------------\
  372. ---------");
  373. }
  374.  
  375.  
  376.  
  377. /* ======================================================================== *
  378.  *                                 readstr                                  *
  379.  * ======================================================================== */
  380.  
  381. void readstr( str, max )
  382.     char  str[];    /* the string to be read */
  383.     short max;      /* the maximum number of chars to be read */
  384. {
  385. /*
  386.  * read a string up to 'max' characters long
  387.  */
  388.  
  389.   register short i;
  390.  
  391.   rewind(stdin);
  392.   for (i=0; (str[i]=getchar()) != '\n' && i<max; i++);
  393.   str[i] = '\0';
  394. }
  395.  
  396.  
  397.  
  398. /* ======================================================================== *
  399.  *                                strupper                                  *
  400.  * ======================================================================== */
  401.  
  402. void strupper( str )
  403.     char *str;
  404. {
  405. /*
  406.  * convert the string 'str' to uppercase
  407.  */
  408.  
  409.   register int i;
  410.  
  411.   for (i=0; str[i] != '\0'; i++)
  412.     if ( islower(str[i]) )
  413.       str[i] = toupper(str[i]);
  414. }
  415.  
  416.  
  417.  
  418. /* ======================================================================== *
  419.  *                                  init                                    *
  420.  * ======================================================================== */
  421.  
  422. void init( secret, hlpsecret, score )
  423.     char  secret[];       /* the hidden pattern */
  424.     char  hlpsecret[];    /* temporary pattern */ 
  425.     int   *score;         /* the score */
  426. {
  427. /*
  428.  * initialization code
  429.  * --NOTE--
  430.  * this function uses the machine depended call: Random()
  431.  * when compiling on a machine other than the ST, you should modify
  432.  * the routine so it uses the appropriate function for generating
  433.  * random numbers
  434.  */
  435.  
  436.   register int i;
  437.  
  438.  
  439.   /* generate a random hidden-pattern */
  440.   srand(seedval);
  441.   for (i=0; i<MAXSYMB; i++)
  442.     secret[i] = Random()%8 + 65;
  443.   secret[i] = '\0';
  444.   strcpy(hlpsecret,secret);
  445.   
  446.   /* initialize the score */
  447.   *score = 10125;
  448. }
  449.  
  450.  
  451.  
  452. /* ======================================================================== *
  453.  *                               error_check                                *
  454.  * ======================================================================== */
  455.  
  456. void error_check( pattern, id, turn )
  457.     char  pattern[];    /* the guessing pattern */
  458.     short *id;          /* error id */
  459.     int   *turn;        /* current turn */
  460. {
  461. /*
  462.  * do some error checking on input (guessing pattern)
  463.  */
  464.  
  465.   void  strupper(), show_error(), readstr();
  466.   short set_errid();
  467.  
  468.  
  469.   /* if the player types either 'Q' or 'QUIT' (note that the pattern
  470.    * has been already converted to uppercase), then the program stops
  471.    */
  472.   if ( !strcmp(pattern,"Q") || !strcmp(pattern,"QUIT") )
  473.     return;
  474.  
  475.   /* if the user types 'R', show him the current error message */
  476.   else if ( !strcmp(pattern,"R") ){
  477.     show_error(0);
  478.     (*turn)--;
  479.   }
  480.  
  481.   /* when an error occurs, we set the error-id & display x's under the
  482.    * column labelled PATTERN; also, we don't increase the variable 'turn'
  483.    */
  484.   else
  485.     while ( (*id=set_errid(pattern)) != 0 ){
  486.       printf("        xxxxx           Enter guess #%2d: ", *turn);
  487.       readstr(pattern,MAXSYMB);
  488.       strupper(pattern);
  489.       if ( !strcmp(pattern,"R") )
  490.         show_error(*id);
  491.       if ( !strcmp(pattern,"Q") || !strcmp(pattern,"QUIT") )
  492.         break;
  493.     }
  494. }
  495.  
  496.  
  497.  
  498. /* ======================================================================== *
  499.  *                               set_errid                                  *
  500.  * ======================================================================== */
  501.  
  502. short set_errid( pattern )
  503.     char pattern[];   /* guessing pattern */
  504. {
  505. /*
  506.  * set the error-id:
  507.  * 0: no errors
  508.  * 1: guessing pattern does not consist of 5 symbols
  509.  * 2: guessing pattern includes illegal symbols
  510.  */
  511.  
  512.   register short i=0;
  513.  
  514.  
  515.   if ( strlen(pattern) != MAXSYMB )
  516.     return(1);
  517.   else
  518.     for (i=0; i<MAXSYMB; i++)
  519.       if ( pattern[i]<'A'  ||  pattern[i]>'H' )
  520.     return(2);
  521.  
  522.   return(0);
  523. }
  524.  
  525.  
  526.  
  527. /* ======================================================================== *
  528.  *                               show_error                                 *
  529.  * ======================================================================== */
  530.  
  531. void show_error( id )
  532.     short id;   /* the error-id */
  533. {
  534. /*
  535.  * display an error message, according to the error-id
  536.  */
  537.  
  538.   switch (id){
  539.     case 1: printf("\t\t\t\tERR1> You should enter %d symbols\n",MAXSYMB);
  540.             break;
  541.     case 2: puts("\t\t\t\tERR2> Valid symbols are letters from A to H");
  542.             break;
  543.     default:puts("\t\t\t\t**** NO ERRORS! Keep playing");
  544.   }
  545. }
  546.  
  547.  
  548.  
  549. /* ======================================================================== *
  550.  *                                  check                                   *
  551.  * ======================================================================== */
  552.  
  553. void check( hlpsecret, hlpinptrn, identical, same )
  554.     char  hlpsecret[];
  555.     char  hlpinptrn[];
  556.     int   *identical;
  557.     int   *same;
  558. {
  559. /*
  560.  * compare the hidden-pattern against the guessing-pattern.
  561.  * the function discovers how many common symbols are placed correctly (*)
  562.  * and how many are not (#). The first number is assigned to the variable
  563.  * 'indentical', the second number is assigned to the variable 'same' (notice
  564.  * that both variables are passed as pointers to the function, so they maintain
  565.  * their values). The strings 'hlpsecret' and 'hlpinptrn' are used in order not
  566.  * to touch the original strings 'secret' and 'pattern', because we need their
  567.  * original contents to be printed on the screen.
  568.  * ----------------------------------------------
  569.  * The algorithm for calculating 'identical' and 'same' is really simple:
  570.  * first we count the correctly placed symbols; every time we find one we
  571.  * replace it with the charcter '!' in the hidden pattern, and with ':' in
  572.  * the guessing pattern. We do that because we do not want to count them
  573.  * again when searching for misplaced symbols. Then we rescan the 2 patterns
  574.  * counting common symbols; again we use the trick with the chars '!' and ':'
  575.  * because this way we will never count the same symbol twice.
  576.  */
  577.  
  578.   int index, member();
  579.   register int i;
  580.  
  581.   /* initialize 'identical' and 'same' */
  582.   *identical = *same = 0;
  583.  
  584.   /* calculate 'identical' */
  585.   for (i=0; i<MAXSYMB; i++)
  586.     if ( hlpsecret[i] == hlpinptrn[i] ){
  587.       hlpsecret[i] = '!';
  588.       hlpinptrn[i] = ':';
  589.       (*identical)++;
  590.     }
  591.  
  592.   /* calculate 'same' */
  593.   for (i=0; i<MAXSYMB; i++){
  594.     index = member(hlpsecret, hlpinptrn[i]);
  595.     if ( index != -1 ){
  596.       hlpsecret[index] = '!';
  597.       hlpinptrn[i] = ':';
  598.       (*same)++;
  599.     }
  600.   }
  601.  
  602. }
  603.  
  604.  
  605.  
  606. /* ======================================================================== *
  607.  *                                  member                                  *
  608.  * ======================================================================== */
  609.  
  610. int member( str, ch )
  611.     char  *str, ch;
  612. {
  613. /*
  614.  * return the index of the character 'ch' in the string 'str'.
  615.  * if 'ch' is not member of 'str' the function returns -1
  616.  */
  617.  
  618.   register int i;
  619.  
  620.   for (i=0; i<MAXSYMB; i++)
  621.     if ( ch==str[i] )
  622.       return(i);
  623.  
  624.   return(-1);
  625. }
  626.  
  627.  
  628.  
  629. /* ======================================================================== *
  630.  *                              give_hints                                  *
  631.  * ======================================================================== */
  632.  
  633. void give_hints( identical, same )
  634.     int identical, same;
  635. {
  636. /*
  637.  * display the hint-pattern on the screen.
  638.  * the function outputs 'identical' stars and 'same' sharp signs
  639.  */
  640.  
  641.   register int i;
  642.  
  643.   /* print stars */
  644.   for (i=0; i<identical; i++)
  645.     putchar('*');
  646.  
  647.   /* print sharp signs */
  648.   for (i=0; i<same; i++)
  649.     putchar('#');
  650.  
  651.   /* pad the rest with spaces */
  652.   for ( i=(identical+same); i<MAXSYMB; i++ )
  653.     putchar(' ');
  654.  
  655. }
  656.  
  657.  
  658.  
  659. /* ======================================================================== *
  660.  *                              upd_table                                   *
  661.  * ======================================================================== */
  662.  
  663. void upd_table( table, name, score )
  664.     REC_TYPE  table[];  /* table of scorers */
  665.     char  name[];       /* name of current player */
  666.     int   score;        /* final score of current player */
  667. {
  668. /*
  669.  * update the table of scorers
  670.  */
  671.  
  672.   void  insert();
  673.  
  674.   if ( score > table[9].score )
  675.   {
  676.     tab_changed = 1;  /* TRUE */
  677.     insert( table, name, score );
  678.     if ( score==table[0].score )
  679.       puts("C O N G R A T U L A T I O N S !!!  You are the TOP-SCORER!!");
  680.     else
  681.       puts("You just entered SUPER MASTER MIND's Top-10 chart !!!!!!");
  682.     puts("-------------------------------------------------------------------\
  683. ------------");
  684.   }
  685. }
  686.  
  687.  
  688.  
  689. /* ======================================================================== *
  690.  *                                insert                                    *
  691.  * ======================================================================== */
  692.  
  693. void insert( table, name, score )
  694.     REC_TYPE  table[];  /* table of scorers */
  695.     char  name[];       /* name of current player */
  696.     int   score;        /* final score of current player */
  697. {
  698. /*
  699.  * insert current player in the table of scorers
  700.  */
  701.  
  702.   register int i, place;
  703.  
  704.   /* find where to put the new entry (store the index in 'place') */
  705.   place=0;
  706.   while( table[place].score > score )
  707.     place++;
  708.  
  709.   /* shift the entries, after 'place', one location to the right */
  710.   for ( i=MAXTABL-1;  i>place;  i-- ){
  711.     strcpy(table[i].name,table[i-1].name);
  712.     table[i].score = table[i-1].score;
  713.   }
  714.  
  715.   /* put current player in the table of scorers */
  716.   strcpy(table[place].name, name);
  717.   table[place].score = score;
  718. }
  719.  
  720.  
  721.  
  722. /* ======================================================================== *
  723.  *                            upd_lastfile                                  *
  724.  * ======================================================================== */
  725.  
  726. void upd_lastfile( record )
  727.     REC_TYPE  record;
  728. {
  729. /*
  730.  * update the file with the data of the last player
  731.  */
  732.  
  733.   long disk_free();
  734.  
  735.  
  736.   if (disk_free() < (long)sizeof(record) )
  737.   {
  738.     puts("\tDISK ERROR: there is not enough space to save file LAST.DAT");
  739.     return;
  740.   }
  741.  
  742.   if ( lastgame=fopen("LAST.DAT","wb") )
  743.   {
  744.     fprintf(lastgame, "%s %d", record.name, record.score);
  745.     fclose(lastgame);
  746.     return;
  747.   }
  748.   puts("\tFILE ERROR: cannot open file LAST.DAT (data are not saved)");
  749. }
  750.  
  751.  
  752.  
  753. /* ======================================================================== *
  754.  *                            upd_tabfile                                   *
  755.  * ======================================================================== */
  756.  
  757. void upd_tabfile( table )
  758.     REC_TYPE  table[];
  759. {
  760. /*
  761.  * update the file with the table of scorers
  762.  */
  763.  
  764.   register int i;
  765.   long disk_free();
  766.  
  767.  
  768.   if ( tab_changed )    /* table has been changed */
  769.   {
  770.     if (disk_free() < (long)sizeof(table) )
  771.     {
  772.       puts("\tDISK ERROR: there is not enough space to save file TABLE.DAT");
  773.       return;
  774.     }
  775.  
  776.     if ( hi_file=fopen("TABLE.DAT","wb") ){
  777.       for (i=0; i<MAXTABL; i++)
  778.         fprintf(hi_file, "%s %d", table[i].name, table[i].score);
  779.       fclose(hi_file);
  780.       return;
  781.     }
  782.     puts("\tFILE ERROR: cannot open file TABLE.DAT (data are not saved)");
  783.   }
  784. }
  785.  
  786.  
  787.  
  788. /* ======================================================================== *
  789.  *                             upd_seed                                     *
  790.  * ======================================================================== */
  791.  
  792. void upd_seed()
  793. {
  794. /*
  795.  * update the file with the seed value
  796.  * --NOTE--
  797.  * again, this function uses the machine depended function: Random();
  798.  * when compiling on a machine different than an ST, replace it with
  799.  * the function provided by the BIOS
  800.  */
  801.  
  802.   long disk_free();
  803.  
  804.   if (disk_free() < (long)sizeof(seedval) )
  805.   {
  806.     puts("\tDISK ERROR: there is not enough space to save file SEED.DAT");
  807.     return;
  808.   }
  809.  
  810.   if ( seedfile=fopen("SEED.DAT","wb") )
  811.   {
  812.     fprintf(seedfile, "%ld", Random());
  813.     fclose(seedfile);
  814.     return;
  815.   }
  816.   puts("\tFILE ERROR: cannot open file SEED.DAT (data are not saved)");
  817. }
  818.  
  819.  
  820.  
  821. /* ======================================================================== *
  822.  *                            disk_free                                     *
  823.  * ======================================================================== */
  824.  
  825. long disk_free()
  826. {
  827. /*
  828.  * return the number of free bytes on the disk
  829.  * --NOTE--
  830.  * This function is NOT PORTABLE. It only works on the ST.
  831.  * To compile it on a different machine you should modify it
  832.  */
  833.  
  834.  
  835.   struct {
  836.     unsigned long d_freeAUs;  /* free Allocation Units */
  837.     unsigned long d_manyAUs;  /* how many AUs on disk */
  838.     unsigned long d_secsize;  /* sector size */
  839.     unsigned long d_secperAU; /* sectors per AU */
  840.   }disk;
  841.  
  842.   int   drive;
  843.   long  free_bytes;
  844.   long  free_sectors;
  845.  
  846.  
  847.   drive = Dgetdrv();
  848.   Dfree(&disk, drive+1);
  849.   free_sectors =  disk.d_freeAUs * disk.d_secperAU;
  850.   free_bytes   =  free_sectors * disk.d_secsize;
  851.  
  852.   return(free_bytes);
  853. }
  854.  
  855.  
  856.  
  857. /* ======================================================================== *
  858.  *                              comments                                    *
  859.  * ======================================================================== */
  860.  
  861. void comments( turn )
  862.     int turn;
  863. {
  864. /*
  865.  * print an appropriate message, depending on how
  866.  * many turns it took to find the hidden pattern
  867.  */
  868.  
  869.   switch(turn){
  870.     case  0:printf("RIGHT..THIS IS TOO MUCH FOR YOU, ISN'T IT?");
  871.             break;
  872.     case  1:
  873.     case  2:printf("THAT WAS LUCK! YOU SHOULD TAKE A TRIP TO LAS VEGAS!");
  874.             break;
  875.     case  3:
  876.     case  4:printf("ARE YOU GENIOUS OR JUST LUCKY? CAN YOU DO IT AGAIN?");
  877.             break;
  878.     case  5:
  879.     case  6:printf("THAT WAS IMPRESSIVE! GOOD PERFORMANCE, NICE SCORE!");
  880.             break;
  881.     case  7:
  882.     case  8:printf("SOME PEOPLE CANNOT DO BETTER THAN AVERAGE....RIGHT?");
  883.             break;
  884.     case  9:
  885.     case 10:printf("NOT GOOD ENOUGH! WHY DON'T YOU GIVE IT ANOTHER TRY?");
  886.             break;
  887.     case 11:
  888.     case 12:printf("   AT LAST YOU GOT IT; BUT IT WAS EASY, WASN'T IT?");
  889.             break;
  890.     default:printf("I BET YOUR GPA IS AROUND 2.0! YOU HAVE TO LIVE WITH IT!");
  891.             break;
  892.   }
  893.   putchar('\n');
  894. }
  895.  
  896.  
  897.  
  898. /* ======================================================================== *
  899.  *                           show_hitab                                     *
  900.  * ======================================================================== */
  901.  
  902. void show_hitab( table )
  903.     REC_TYPE table[];
  904. {
  905. /*
  906.  * display the table of scorers
  907.  */
  908.  
  909.   register short i;
  910.  
  911.   puts("\t*->>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<-*");
  912.   puts("\t|              T O P    S C O R E R S              |");
  913.   puts("\t*->>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<-*");
  914.   
  915.   for (i=0; i<MAXTABL-1; i++)
  916.     printf("\t|     Number #%2d: %-10s with %5d points     |\n",
  917.                                          i+1, table[i].name, table[i].score);
  918.  
  919.   puts("\t*==================================================*");
  920. }
  921.