home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume6 / sun-tetris2 / score.c < prev    next >
C/C++ Source or Header  |  1989-07-06  |  5KB  |  149 lines

  1. #include "defs.h"
  2.  
  3. update_highscore_table()
  4. {
  5.         int     i, j;
  6.         long    when;
  7.         extern char *ctime();
  8.         extern long time();
  9.         char    hostname[20];
  10.         char    buf[BUFSIZ];
  11.  
  12.         /* re-read high-score table in case someone else on the network is
  13.          * playing at the same time */
  14.         read_high_scores();
  15.  
  16.         /* Next line finds score greater than current one */
  17.         for (i = 0; ((i < HIGH_TABLE_SIZE) && (score >= high_scores[i].score)); i++);
  18.         i--;
  19.         score_position = i;
  20.         if (i >= 0) {
  21.                 for (j = 0; j < i; j++)
  22.                         high_scores[j] = high_scores[j + 1];
  23.                 strcpy(high_scores[i].name, name);
  24.                 high_scores[i].score = score;
  25.                 high_scores[i].rows = rows;
  26.                 high_scores[i].level = rows / 10;
  27.                 if (gethostname(hostname, BUFSIZ) == -1)
  28.                         strcpy(high_scores[i].hostname, "unknown-host");
  29.                 else
  30.                         strcpy(high_scores[i].hostname, hostname);
  31.                 time(&when);
  32.                 strcpy(buf, ctime(&when));      /* ctime() adds a newline
  33.                                                  * char */
  34.                 strip_eoln(buf);/* so remove it          */
  35.                 strcpy(high_scores[i].date, buf);
  36.                 write_high_scores();
  37.         }
  38. }
  39.  
  40. read_high_scores()
  41. {
  42.         FILE   *fp;
  43.         int     i;
  44.         char   *c, buf[BUFSIZ];
  45.  
  46.         for (i = 0; i < HIGH_TABLE_SIZE; i++) {
  47.                 strcpy(high_scores[i].name, " ");
  48.                 high_scores[i].score = 0;
  49.                 high_scores[i].rows = 0;
  50.                 high_scores[i].level = 0;
  51.                 strcpy(high_scores[i].hostname, " ");
  52.                 strcpy(high_scores[i].date, " ");
  53.         }
  54.         if ((fp = fopen(HIGH_SCORE_TABLE, "r")) == NULL) {
  55.                 fprintf(stderr, "tetris: No High score file\n");
  56.                 return;
  57.         }
  58.         for (i = 0; i < HIGH_TABLE_SIZE; i++) {
  59.                 fgets(buf, BUFSIZ, fp);
  60.                 strip_eoln(buf);
  61.                 strcpy(high_scores[i].name, buf);
  62.                 fgets(buf, BUFSIZ, fp);
  63.                 strip_eoln(buf);
  64.                 high_scores[i].score = atoi(buf);
  65.                 fgets(buf, BUFSIZ, fp);
  66.                 strip_eoln(buf);
  67.                 high_scores[i].rows = atoi(buf);
  68.                 fgets(buf, BUFSIZ, fp);
  69.                 strip_eoln(buf);
  70.                 high_scores[i].level = atoi(buf);
  71.                 fgets(buf, BUFSIZ, fp);
  72.                 strip_eoln(buf);
  73.                 strcpy(high_scores[i].hostname, buf);
  74.                 fgets(buf, BUFSIZ, fp);
  75.                 strip_eoln(buf);
  76.                 strcpy(high_scores[i].date, buf);
  77.         }
  78.         fclose(fp);
  79. }
  80.  
  81. strip_eoln(s)
  82.         char   *s;
  83. {
  84.         char   *s1;
  85.  
  86.         while (*s != '\0') {
  87.                 if (*s == '\n') {       /* End of line char */
  88.                         s1 = s;
  89.                         do {
  90.                                 *s1 = *(s1 + 1);        /* Copy rest of string */
  91.                                 s1++;
  92.                         } while (*s1 != '\0');
  93.                 } else
  94.                         s++;
  95.         }
  96. }
  97.  
  98. write_high_scores()
  99. {
  100.         FILE   *fp;
  101.         int     i;
  102.  
  103.         if ((fp = fopen(HIGH_SCORE_TABLE, "w")) == NULL) {
  104.                 fprintf(stderr, "tetris: Couldn't open high score file %s\n", HIGH_SCORE_TABLE);
  105.                 return;
  106.         }
  107.         for (i = 0; i < HIGH_TABLE_SIZE; i++)
  108.                 fprintf(fp, "%s\n%d\n%d\n%d\n%s\n%s\n",
  109.                         high_scores[i].name,
  110.                         high_scores[i].score,
  111.                         high_scores[i].rows,
  112.                         high_scores[i].level,
  113.                         high_scores[i].hostname,
  114.                         high_scores[i].date);
  115.         fclose(fp);
  116. }
  117.  
  118. void
  119. print_high_scores()
  120. {
  121.         int     i;
  122.         char    buf[BUFSIZ];
  123.  
  124.         /* re-read high-score table in case someone else on the network is
  125.          * playing at the same time */
  126.         read_high_scores();
  127.  
  128.         for (i = HIGH_TABLE_SIZE - 1; i >= 0; i--) {
  129.                 sprintf(buf, "%3d) %-15s %6d %5d %3d  %-10s  %s\n",
  130.                         HIGH_TABLE_SIZE - i,
  131.                         high_scores[i].name,
  132.                         high_scores[i].score,
  133.                         high_scores[i].rows,
  134.                         high_scores[i].level,
  135.                         high_scores[i].hostname,
  136.                         high_scores[i].date);
  137.                 panel_set(high_score_item[HIGH_TABLE_SIZE - i], PANEL_LABEL_BOLD, FALSE, PANEL_LABEL_STRING, buf, 0);
  138.         }
  139.         if (score_position != -1)
  140.                 panel_set(high_score_item[HIGH_TABLE_SIZE - score_position], PANEL_LABEL_BOLD, TRUE, 0);
  141.         window_set(score_frame, WIN_SHOW, TRUE, 0);
  142. }
  143.  
  144. print_authors()
  145. {
  146.         printf("This version of tetris was written by Phill Everson <everson@cs.bris.ac.uk>\nand Martyn Shortley <shortley@cs.bris.ac.uk>, ");
  147.         printf("Based on the version posted\nto comp.sources.games by Adam Marguilies <vespa@ssyx.ucsc.edu>\n\nLet us know if you like it.\n31st March 1989 \n");
  148. }
  149.