home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / XBD2_SRC.ZIP / XBD2_SRC / SCORES.C < prev    next >
C/C++ Source or Header  |  1991-02-10  |  4KB  |  123 lines

  1. /*********************************************/
  2. /* you just keep on pushing my luck over the */
  3. /*           BOULDER        DASH             */
  4. /*                                           */
  5. /*     Jeroen Houttuin, ETH Zurich, 1990     */
  6. /*                                           */
  7. /*                                           */
  8. /*      PC-VGA version from :                */
  9. /*                                           */
  10. /*        Herve SOULARD, Paris, 1990        */
  11. /*                                           */
  12. /*********************************************/
  13.  
  14. #include <stdio.h>
  15. #include "xbd.h"
  16.  
  17. char           *getenv();
  18.  
  19. #define NUMHIGH 20        /* Number of high scores that will be
  20.                           * remembered */
  21.  
  22. /* Add a high score to the high score list */
  23. void add_score()
  24. {
  25.     /* Structure containing top game results */
  26.     struct {
  27.         int            score;        /* Final score */
  28.         int            slev, elev;    /* Starting and ending level */
  29.         char           desc[80];        /* Text description */
  30.     } tops[NUMHIGH], next;
  31.     FILE           *sfile;    /* High score file */
  32.     char            buf[200];
  33.     register int    i;
  34.     int            numscore, cur, numgame;
  35.     char            *aux;
  36.     
  37.     /* Generate name of high score file */
  38.     if (aux = getenv("XBDLIB"))
  39.         strcpy(buf, aux);
  40.     else
  41.         strcpy(buf, LIB);
  42.  
  43.     strcat(buf,"\\scores");
  44.     
  45.     /* Open high score file */
  46.     sfile = fopen(buf, "r");
  47.     
  48.     /* Set default values for number of games and high scores */
  49.     numscore = 0;
  50.     numgame = 0;
  51.     /* If file is readable, load in old high score list */
  52.     if (sfile != NULL) {
  53.         /* Extract score information from line */
  54.         while (fgets(buf, 200, sfile) && numscore < NUMHIGH) {
  55.             sscanf(buf, "%d %d %d %[^\n]", &(next.score), &(next.slev), 
  56.                                           &(next.elev), next.desc);
  57.             tops[numscore] = next;
  58.             numscore++;
  59.         }    
  60.         fclose(sfile);
  61.     }
  62.     else {
  63.         printf("Error opening score file : %s\n",buf);
  64.         exit (-1);
  65.     }
  66.     /* Contruct the structure containing the score for this game */
  67.     next.score = score;
  68.     next.slev = levelstart;
  69.     next.elev = levelnum;
  70.     sprintf(next.desc, "%s ", getenv("USER"));
  71.     cur = -1;
  72.     /* Insert new score in old high score list */
  73.     if (numscore < NUMHIGH || tops[NUMHIGH - 1].score < next.score) {
  74.         /* Iterate through high score list */
  75.         for (i = (numscore >= NUMHIGH ? NUMHIGH - 2 : numscore - 1); 
  76.              i >= 0; i--) {
  77.             /* Look for place for insertion */
  78.             if (next.score > tops[i].score)
  79.                 tops[i + 1] = tops[i];
  80.             else    
  81.                 break;            /* Found spot for insertion */
  82.         }
  83.         tops[i + 1] = next;        /* Overwrite entry in high score list */
  84.         cur = i + 1;        /* Remember where new high score was inserted */
  85.         /* Increment the number of high scores */
  86.         if (numscore < NUMHIGH)
  87.             numscore++;
  88.     }
  89.     /* Increment and print the number of games played */
  90.     /* Print out new high score list */
  91.     for (i = 0; i < numscore; ++i) {
  92.         /* Flag new high score with a leading > */
  93.         if (i == cur)
  94.             putchar('*');
  95.         else
  96.             putchar(' ');
  97.         printf("%-16s- Died on level %3d. Started on level %3d.  Score: %8d.\n",
  98.             tops[i].desc, tops[i].elev, tops[i].slev, tops[i].score);
  99.     }
  100.     /* If current game did not make it to the high score list, print it */
  101.     /* afterwords */
  102.     if (cur == -1) {
  103.         puts("You are quite disappointing:");
  104.         printf("*%-16s- Died on level %3d. Started on level %3d.  Score: %8d.\n",
  105.             next.desc, next.elev, next.slev, next.score);
  106.     }
  107.     /* Save new high score list to score file */
  108.     if (aux = getenv("XBDLIB"))
  109.         sprintf(buf, "%s/scores", aux);
  110.     else
  111.         sprintf(buf, "%s/scores", LIB);
  112.     
  113.     sfile = fopen(buf, "w");
  114.     if (sfile == NULL) {
  115.         perror(buf);
  116.         return;
  117.     }
  118.     for (i = 0; i < numscore; ++i)
  119.         fprintf(sfile, "%d %d %d %s\n", tops[i].score, tops[i].slev,
  120.                tops[i].elev, tops[i].desc);
  121.     fclose(sfile);
  122. }
  123.