home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume5 / golddig / part02 / golddig1.0 / scores.c < prev   
Encoding:
C/C++ Source or Header  |  1989-11-12  |  3.6 KB  |  112 lines

  1. /* This program was written by Alexander Siegel in September of 1989   */
  2. /* at Cornell University.  It may may copied freely for private use or */
  3. /* public dispersion provided that this comment is not removed.  This  */
  4. /* program, any portion of this program, or any derivative of this     */
  5. /* program may not be sold or traded for financial gain.               */
  6.  
  7. #include <stdio.h>
  8. #include <X11/Xlib.h>
  9. #include <X11/keysym.h>
  10. #include "golddig.h"
  11.  
  12. char *getenv(),*sprintf();
  13.  
  14. #define NUMHIGH 15  /* Number of high scores that will be remembered */
  15.  
  16. /* Add a high score to the high score list */
  17. void add_score(whydie)
  18. char *whydie;       /* Textual description of reason for death */
  19. {
  20.   /* Structure containing top game results */
  21.   struct {
  22.     int score;      /* Final score */
  23.     int slev,elev;  /* Starting and ending level */
  24.     int uid;        /* Player account uid */
  25.     char desc[80];  /* Text description */
  26.   } tops[NUMHIGH],next;
  27.   FILE *sfile;      /* High score file */
  28.   char buf[200];
  29.   register int i;
  30.   int numscore,cur,numgame;
  31.  
  32.   /* Generate name of high score file */
  33.   sprintf(buf,"%s/scores",LIB);
  34.   /* Open high score file */
  35.   sfile = fopen(buf,"r");
  36.   /* Set default values for number of games and high scores */
  37.   numscore = 0;
  38.   numgame = 0;
  39.   /* If file is readable, load in old high score list */
  40.   if(sfile != NULL) {
  41.     /* Load a line on text */
  42.     if(fgets(buf,200,sfile))
  43.       sscanf(buf,"%d",&numgame);
  44.     /* Extract score information from line */
  45.     while(fgets(buf,200,sfile) && numscore < NUMHIGH) {
  46.       sscanf(buf,"%d %d %d %d %[^\n]",&(next.score),&(next.slev),&(next.elev),
  47.          &(next.uid),next.desc);
  48.       tops[numscore] = next;
  49.       numscore ++;
  50.     }
  51.     fclose(sfile);
  52.   }
  53.  
  54.   /* Contruct the structure containing the score for this game */
  55.   next.score = score;
  56.   next.slev = levelstart;
  57.   next.elev = levelnum;
  58.   next.uid = getuid();
  59.   sprintf(next.desc,"%s %s",getenv("USER"),whydie);
  60.   cur = -1;
  61.   /* Insert new score in old high score list */
  62.   if(numscore < NUMHIGH || tops[NUMHIGH - 1].score < next.score) {
  63.     /* Iterate through high score list */
  64.     for(i = (numscore >= NUMHIGH ? NUMHIGH-2 : numscore-1);i >= 0;i--) {
  65.       /* Look for place for insertion */
  66.       if(next.score > tops[i].score)
  67.         tops[i+1] = tops[i];    /* Move old scores down one place in list */
  68.       else
  69.         break;                  /* Found spot for insertion */
  70.     }
  71.     tops[i+1] = next;   /* Overwrite entry in high score list */
  72.     cur = i+1;          /* Remember where new high score was inserted */
  73.     /* Increment the number of high scores */
  74.     if(numscore < NUMHIGH)
  75.       numscore ++;
  76.   }
  77.  
  78.   /* Increment and print the number of games played */
  79.   numgame ++;
  80.   printf("High scores after %d games played:\n",numgame);
  81.   /* Print out new high score list */
  82.   for(i=0;i<numscore;++i) {
  83.     /* Flag new high score with a leading > */
  84.     if(i == cur)
  85.       putchar('>');
  86.     else
  87.       putchar(' ');
  88.     printf("%s on level %d (started at %d).  Final score was %d.\n",
  89.        tops[i].desc,tops[i].elev,tops[i].slev,tops[i].score);
  90.   }
  91.   /* If current game did not make it to the high score list, print it */
  92.   /* afterwords */
  93.   if(cur == -1) {
  94.     puts("...");
  95.     printf(">%s on level %d (started at %d).  Final score was %d.\n",
  96.        next.desc,next.elev,next.slev,next.score);
  97.   }
  98.  
  99.   /* Save new high score list to score file */
  100.   sprintf(buf,"%s/scores",LIB);
  101.   sfile = fopen(buf,"w");
  102.   if(sfile == NULL) {
  103.     perror(buf);
  104.     return;
  105.   }
  106.   fprintf(sfile,"%d\n",numgame);
  107.   for(i=0;i<numscore;++i)
  108.     fprintf(sfile,"%d %d %d %d %s\n",tops[i].score,tops[i].slev,
  109.          tops[i].elev,tops[i].uid,tops[i].desc);
  110.   fclose(sfile);
  111. }
  112.