home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / sot.zip / SOTH.C < prev    next >
Text File  |  1988-08-15  |  3KB  |  125 lines

  1. /********** The Son of Tetris Project ************/
  2.  
  3. /***************** HIGH SCORES ********************/
  4.  
  5. #include <ctype.h>
  6. #include <io.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #include "sot.h"
  12.  
  13. #define MARK_CHAR 'α' /* detect unused table */
  14. #define SPACE_STRING "                    "
  15.  
  16. struct scre
  17. {
  18.   char name[20];
  19.   long scre_val;
  20. } /* scre */;
  21.  
  22. static char embed_str[] = "High_score_table:"; /* Table marker in .EXE file */
  23. static char score_space[TABLE_LEN * sizeof(struct scre)] = { MARK_CHAR };
  24.  
  25. static FILE *prog_file;
  26. static int   rec_no;
  27. const struct scre *score = (void *)&score_space;
  28.  
  29. void  error(int error_no)
  30. {
  31.   static char *msg[] = {"Insufficient memory for program. Sorry!",
  32.                         "Disk error here, giving up. Sorry!",
  33.                };
  34.   puts(msg[error_no]);
  35.   exit(error_no);
  36. }     /* error */
  37.  
  38.  
  39. void  write_exe_file(void)
  40. {
  41.   fseek(prog_file, rec_no, SEEK_SET);
  42.   fwrite(score, sizeof(struct scre), TABLE_LEN,prog_file);
  43.   fflush(prog_file);
  44. }     /* write_exe_file */
  45.  
  46.  
  47.  
  48. void  init_score(char *prog_name)
  49. {
  50.   void *buf_ptr;
  51.   char *srch_ptr;
  52.   unsigned f_l;
  53.   int handle;
  54.   int i;
  55.  
  56.   prog_file = fopen(prog_name,"r+b");
  57.   handle = fileno(prog_file);
  58.   f_l = (unsigned)filelength(handle);
  59.   buf_ptr = malloc(f_l);
  60.  
  61.   if (buf_ptr == NULL)
  62.     error(1);
  63.  
  64.   if (f_l != read(handle, buf_ptr, f_l))
  65.     error(2);
  66.  
  67.   for (rec_no = 0, srch_ptr = buf_ptr;
  68.        strcmp(srch_ptr,embed_str);
  69.        rec_no++, srch_ptr++);
  70.   free(buf_ptr);
  71.   srch_ptr += sizeof(embed_str);
  72.   rec_no += sizeof(embed_str);
  73.  
  74.   if (score_space[0] == MARK_CHAR)  /* unused score table */
  75.   {
  76.     for (i = 0; i < TABLE_LEN; i++)
  77.     {
  78.       strcpy((score + i)->name,SPACE_STRING);
  79.       (score + i)->scre_val = 0;
  80.     } /* for each table entry */
  81.     write_exe_file();
  82.   } /* score table need initialising */
  83. }     /* init_score */
  84.  
  85.  
  86. void end_score(void)
  87. {
  88.   fclose(prog_file);
  89. } /*  end_score */
  90.  
  91.  
  92. void champ_val(char *name, long *score_val, int rank)
  93. {
  94.   strcpy(name,(score + (--rank)) -> name);
  95.   *score_val = (score + rank) ->scre_val;
  96. } /* champ_val */
  97.  
  98.  
  99. void  update_score_table(char *name,long new_score)
  100. {
  101.   int rank;
  102.  
  103.   while (isspace(*name))   /* kill off non-entered names */
  104.     name++;
  105.   if (! (*name))
  106.     return;
  107.   for (rank = TABLE_LEN - 2; rank >= -1; rank--)
  108.   {
  109.     /* if new score exceeds current rank */
  110.     if (((score + rank)->scre_val < new_score) && (rank >= 0))
  111.     {
  112.       (score + rank + 1)->scre_val = (score + rank)->scre_val;
  113.       strcpy((score + rank + 1) -> name,(score + rank) -> name);
  114.     } /* new_score execeeds current rank */
  115.  
  116.     else  /* copy new data into rank below */
  117.     {
  118.       (score + rank + 1)->scre_val = new_score;
  119.       strcpy((score + rank + 1) -> name, name);
  120.       break; /* all done */
  121.     }
  122.   } /* for */
  123.   write_exe_file();
  124. } /* update_score_table */
  125.