home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / yahtzee.t.Z / yahtzee.t / high.c < prev    next >
Text File  |  1988-07-28  |  6KB  |  268 lines

  1. /* high.c
  2.  *    contains routines for the maintenance of the high score file.
  3.  */
  4. #include <stdio.h>
  5. #include <curses.h>
  6. #include "defs.h"
  7.  
  8. #define High_Length (max_hs_count + 5)
  9. #define High_Width 29
  10. #define Start_Line 3
  11. #define Display_High_Time 5
  12.  
  13. extern WINDOW *screen;
  14.  
  15. /* dis_score() displays the current high score file in a
  16.  * window, 'Back_Window' is the window behind the high score
  17.  * display window, 'Back_Window' must be restored once the
  18.  * user has finished with the high score window */
  19. dis_score(Back_Window)
  20.  
  21. WINDOW *Back_Window;
  22.  
  23.     {
  24.     FILE *fp;
  25.     char Name[player_name_len], ch;
  26.     int score, i, tmp_y, tmp_x;
  27.     extern int non_stop;
  28.     WINDOW *High_Window;
  29.  
  30. /* Back_Window will be NULL in the case where yahtzee is exiting
  31.  * and displaying the high score file as its last action */
  32.     if (Back_Window != (WINDOW *) 0)
  33.         {
  34.         getyx(Back_Window, tmp_y, tmp_x);
  35.         wmove(Back_Window, 0, 0);
  36.         wrefresh(Back_Window);
  37.         }
  38.  
  39. /* create the high score window */
  40.     High_Window = newwin(High_Length, High_Width, LINES - High_Length - 1,
  41.         3);
  42.     werase(High_Window);
  43.     BoxUp(High_Window, High_Length, High_Width);
  44.     wmove(High_Window, 1, 1);
  45.  
  46. /* open high score file (note HSFILE is defined at compile time, see
  47.  * the makefile) */
  48.     fp = fopen(HSFILE, "r");
  49.     if (fp == NULL)
  50.         waddstr(High_Window, "Cannot access score file.");
  51.     else
  52.         {
  53.         wprintw(High_Window, "Top %d Yahtzee High Scores",
  54.             max_hs_count);
  55.         i = Start_Line;
  56.  
  57. /* output all high scores */
  58.         while (fscanf(fp, "%d %s\n", &score, Name) != EOF)
  59.             {
  60.             wmove(High_Window, i, 1);
  61.             wprintw(High_Window, "%20s %5d", Name, score);
  62.             ++i;
  63.             }
  64.         fclose(fp);
  65.         }
  66.     touchwin(High_Window);
  67.     wrefresh(High_Window);
  68.     if (Back_Window != (WINDOW *) 0)
  69.         {
  70.  
  71. /* if the user specified non-stop play then don't get input */
  72.         if (non_stop)
  73.             {
  74.             wmove(High_Window, 0, 0);
  75.             wrefresh(High_Window);
  76.             sleep(Display_High_Time);
  77.             }
  78.         else
  79.             {
  80.             mvwaddstr(High_Window, High_Length - 2, 1,
  81.                 "--(q)uit--");
  82. #ifndef SYS5_3
  83.             wrefresh(High_Window);
  84. #endif
  85.             do
  86.                 {
  87.                 ch = wgetch(High_Window);
  88.                 switch (ch)
  89.                     {
  90.                     case Form_Feed : redraw(High_Window);
  91.                            break;
  92.                     case '?' : help_out(7, High_Window);
  93.                            break;
  94.                     case 'b' : rools(High_Window);
  95.                            break;
  96.                     case '!' : shell(High_Window);
  97.                         break;
  98.                     case 'v' : version(High_Window);
  99.                         break;
  100. #if defined(SYS5) || defined(SYS5_3)
  101.                     case '$' : shwin(High_Window);
  102.                            break;
  103.                     case 'q' :
  104.                     case ' ' : break;
  105.                     default : flash();
  106.                           break;
  107. #endif
  108.                     }
  109.                 } while (ch != 'q' && ch != ' ');
  110.             }
  111.         delwin(High_Window);
  112.  
  113. /* restore previous window */
  114.         touchwin(Back_Window);
  115.         wmove(Back_Window, tmp_y, tmp_x);
  116.         wrefresh(Back_Window);
  117.         }
  118.     }
  119.  
  120. extern int machine[max_players];
  121. extern char *getenv(), *strcpy(), *strcat();
  122.  
  123. /* update high score file */
  124. update_high(player_number, score)
  125.  
  126. int player_number, score;
  127.  
  128.     {
  129.     FILE *hsf;
  130.     char player[max_hs_count + 1][player_name_len],
  131.         tmp_player[player_name_len];
  132.     int player_score[max_hs_count + 1], own_score,
  133.         lowest_score = 0;
  134.     register int hs_count = 0, existing_entries = 0, i, j;
  135.     char message[150];
  136.     void file_mess();
  137.  
  138. /* read contents of high score file (also check to see if it exists) */
  139.     hsf = fopen(HSFILE, "r");
  140.     if (hsf == NULL)
  141.         {
  142.         sprintf(message, "trying to create: %s", HSFILE);
  143.         if (strlen(HSFILE) > 79)
  144.             strcpy(message, "Trying to create high score file.");
  145.         file_mess(screen, message);
  146.         }
  147.     else
  148.         {
  149.         while (fscanf(hsf, "%d %s\n", &player_score[hs_count],
  150.             player[hs_count]) != EOF)
  151.             ++hs_count;
  152.         fclose(hsf);
  153.         }
  154.  
  155. /* there is probably a better way of getting a player name,
  156.  * but we don't want to pester the user for too much info */
  157. /* #ifdef BSD
  158.     strcpy(tmp_player, getenv("USER"));
  159. #else
  160.     strcpy(tmp_player, getenv("LOGNAME"));
  161. #endif
  162. */
  163.     strcpy(tmp_player, getenv("USER"));
  164.  
  165. /* make sure that the heuristics get credit for winning a game */
  166.     if (machine[player_number])
  167.         strcat(tmp_player, "-machine");
  168.  
  169. /* determine how many entries the user has in the high score file,
  170.  * and if the user has an entry what the users lowest recorded score is */
  171.     for (i = 0; i < hs_count; ++i)
  172.         if (strcmp(player[i], tmp_player) == 0)
  173.             {
  174.             own_score = i;
  175.             ++existing_entries;
  176.             lowest_score = player_score[i];
  177.             }
  178.  
  179.     if (existing_entries >= max_entries)
  180.         {
  181.         if (lowest_score < score)
  182.             {
  183.  
  184. /* if the user already has the max. number of entries in the HS file
  185.  * but the new score is better than a previous score then delete the
  186.  * users worst high score and insert the new score */
  187.             --hs_count;
  188.             for (j = own_score; j < hs_count; ++j)
  189.                 {
  190.                 player_score[j] = player_score[j + 1];
  191.                 strcpy(player[j], player[j + 1]);
  192.                 }
  193.             insert_score(player_score, player, hs_count,
  194.                 tmp_player, score);
  195.             }
  196.         }
  197.     else
  198.  
  199. /* attempt to insert the score */
  200.         insert_score(player_score, player, hs_count, tmp_player, score);
  201.     }
  202.  
  203. insert_score(player_score, player, hs_count, player_name, score)
  204.  
  205. int player_score[max_hs_count + 1], hs_count, score;
  206. char *player_name, player[max_hs_count + 1][player_name_len];
  207.  
  208.     {
  209.     int j, position = 0;
  210.     FILE *hsf;
  211.     void file_mess();
  212.  
  213. /* determine the users ranking in the HS file */
  214.     while (position < hs_count && player_score[position] > score)
  215.         ++position;
  216.  
  217. /* if the user has the a ranking lower than the number of possible
  218.  * entries then don't update file */
  219.     if (position >= max_hs_count)
  220.         return(0);
  221.  
  222. /* make room for the new score */
  223.     for (j = hs_count; j > position; --j)
  224.         {
  225.         strcpy(player[j], player[j - 1]);
  226.         player_score[j] = player_score[j - 1];
  227.         }
  228.  
  229. /* insert the new name and score */
  230.     player_score[position] = score;
  231.     strcpy(player[position], player_name);
  232.     ++hs_count;
  233.  
  234. /* truncate HS file if it exceeds the maximum allowable entries */
  235.     if (hs_count > max_hs_count)
  236.         hs_count = max_hs_count;
  237.  
  238. /* write the new HS file */
  239.     if ((hsf = fopen(HSFILE, "w")) == NULL)
  240.         {
  241.         file_mess(screen, "Cannot write to high score file.");
  242.         return;
  243.         }
  244.     for (j = 0; j < hs_count; ++j)
  245.         fprintf(hsf, "%d %s\n", player_score[j], player[j]);
  246.     fclose(hsf);
  247.     }
  248.  
  249. static void file_mess(back_window, message)
  250.  
  251. WINDOW *back_window;
  252. char *message;
  253.  
  254.     {
  255.     WINDOW *mess_win;
  256.     int mess_len = strlen(message) + 3;
  257.  
  258.     mess_win = newwin(3, mess_len, 11, (COLS - mess_len) / 2);
  259.     BoxUp(mess_win, 3, mess_len);
  260.     mvwaddstr(mess_win, 1, 1, message);
  261.     touchwin(mess_win);
  262.     wrefresh(mess_win);
  263.     sleep(3);
  264.     touchwin(back_window);
  265.     wrefresh(back_window);
  266.     delwin(mess_win);
  267.     }
  268.