home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xblockbuster / part03 / score.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-28  |  5.6 KB  |  191 lines

  1. /*
  2.  * File:       score.c
  3.  * Author:     Eric Van Gestel
  4.  * Updated for X11 by Mark S. Wedel
  5.  *
  6.  * For:                xblockbuster
  7.  *
  8.  * Implementation:
  9.  *     Upto USER_SCORES scores are logged per user.
  10.  *     The insertion low point (i.e., the entry that will disappear) is defined
  11.  *     to be either the user's previous save entry, or his USER_SCORES'th entry
  12.  *     or the last entry if neither exists.
  13.  *     Sometimes saved scores may not be completed
  14.  *     (e.g, if the game crashed or if the player Quit explicitly).
  15.  *     Such entries are given a negative number of balls when detected
  16.  *     (criterium: score not bettered).
  17.  */
  18.  
  19. #include "xblockbuster.h"
  20.  
  21. struct Score {
  22.     int             score, balls_left;
  23.     char            login[9];
  24. }               score_board[NB_SCORES];
  25.  
  26. void center_text(offset, message )
  27. int offset;
  28. char *message;
  29. {
  30.         XDrawImageString(display, win, gc, 
  31.             ( STAGE_WIDTH_IN_PIXELS - strlen( message ) * font_width ) / 2,
  32.                 BORDER + ( 4 + offset ) * font_height, 
  33.                 message, strlen(message));
  34. }
  35.  
  36.  
  37. void
  38. show_score_board(  )
  39. {
  40.     register int    lows, s, ns,scorenum=0;
  41.     register int    update = FALSE;
  42.     register int    made_it = FALSE;
  43.     FILE           *fd;
  44.     char            buf[PATH_LENGTH];
  45.  
  46.     XClearWindow(display,win);
  47.  
  48.     /* initalize score board (in case it would be empty) */
  49.     for ( s = 0; s < NB_SCORES; s++ ) {
  50.         score_board[s].score = 100;
  51.         score_board[s].balls_left = 0;
  52.         ( void ) strcpy( score_board[s].login, "computer" );
  53.     }
  54.  
  55.     /* read the score file into the array */
  56.     if ( !( fd = fopen( sprintf( buf, SCOREFILE, playground ), "r+" ) ) ) {
  57.         perror( "No score file" );
  58.         exit( 1 );
  59.     }
  60.     for ( s = 0; s < NB_SCORES; s++ ) {
  61.         fscanf( fd, "%d:%d:%s\n", &score_board[s].score,
  62.             &score_board[s].balls_left,
  63.             score_board[s].login );
  64.     }
  65.  
  66.     /* find insertion low point */
  67.     for ( lows = ns = 0; lows < NB_SCORES - 1; lows++ ) {
  68.         if ( !strcmp( score_board[lows].login, login ) ) {
  69.             if ( score_board[lows].balls_left > 0 ) {
  70.                 if ( score_board[lows].score > score ) {
  71.                     /*
  72.                      * an older incomplete game: complete
  73.                      * it
  74.                      */
  75.                     score_board[lows].balls_left *= -1;
  76.                     ns++;
  77.                     update = TRUE;
  78.                 } else
  79.                     /* (presumably) the previous save */
  80.                     break;
  81.             } else
  82.                 /* an older completed game */
  83.                 ns++;
  84.             if ( ns >= USER_SCORES )
  85.                 break;
  86.         }
  87.     }
  88.  
  89.     /* find insertion high point */
  90.     for ( s = 0; s <= lows && score_board[s].score > score; s++ );
  91.  
  92.     /* check to see if current score made it */
  93.     if ( s <= lows ) {
  94.         /* yes it did , so shift smaller scores */
  95.         for (  /* lows = lows */ ; lows > s; lows-- )
  96.             score_board[lows] = score_board[lows - 1];
  97.         score_board[s].score = score;
  98.         score_board[s].balls_left = balls_left;
  99.         ( void ) strcpy( score_board[s].login, login );
  100.         update = TRUE;
  101.         made_it = TRUE;
  102.         scorenum=s;
  103.     }
  104.     /* write updated score board */
  105.     if ( update ) {
  106.         rewind( fd );
  107.         for ( s = 0; s < NB_SCORES; s++ ) {
  108.             fprintf( fd, "%d:%d:%s\n", score_board[s].score,
  109.                  score_board[s].balls_left,
  110.                  score_board[s].login );
  111.         }
  112.     }
  113.     fclose( fd );
  114.  
  115.     /* show score board */
  116.     center_text( 0, "Top Scores" );
  117.     for ( s = 0; s < NB_SCORES; s++ ) {
  118.         if ( score_board[s].balls_left > 99 ) {
  119.             center_text( s + 2, sprintf( buf, "%7d (%3d)   %8s ",
  120.                          score_board[s].score,
  121.                          score_board[s].balls_left,
  122.                          score_board[s].login ) );
  123.         } else if ( score_board[s].balls_left > 9 ) {
  124.             center_text( s + 2, sprintf( buf, "%7d  (%2d)   %8s ",
  125.                          score_board[s].score,
  126.                          score_board[s].balls_left,
  127.                          score_board[s].login ) );
  128.         } else if ( score_board[s].balls_left > 0 ) {
  129.             center_text( s + 2, sprintf( buf, "%7d   (%1d)   %8s ",
  130.                          score_board[s].score,
  131.                          score_board[s].balls_left,
  132.                          score_board[s].login ) );
  133.         } else if ( score_board[s].balls_left < -99 ) {
  134.             center_text( s + 2, sprintf( buf, "%7d (%3d) + %8s ",
  135.                          score_board[s].score,
  136.                          -score_board[s].balls_left,
  137.                          score_board[s].login ) );
  138.         } else if ( score_board[s].balls_left < -9 ) {
  139.             center_text( s + 2, sprintf( buf, "%7d  (%2d) + %8s ",
  140.                          score_board[s].score,
  141.                          -score_board[s].balls_left,
  142.                          score_board[s].login ) );
  143.         } else if ( score_board[s].balls_left < 0 ) {
  144.             center_text( s + 2, sprintf( buf, "%7d   (%1d) + %8s ",
  145.                          score_board[s].score,
  146.                          -score_board[s].balls_left,
  147.                          score_board[s].login ) );
  148.         } else {    /* no balls left */
  149.             center_text( s + 2, sprintf( buf, "%7d         %8s ",
  150.                          score_board[s].score,
  151.                          score_board[s].login ) );
  152.         }
  153.         if ((s==scorenum) && made_it)
  154.             XDrawImageString(display,win,gc,
  155.             (STAGE_WIDTH_IN_PIXELS - strlen(buf)*font_width)/2 
  156.             -4*font_width,
  157.             BORDER+ (6+s)*font_height, ">>> ",4);
  158.     }
  159.             
  160.  
  161.  
  162.  
  163.     /* make sure the current score is on the board */
  164.     if ( !made_it ) {
  165.         if ( balls_left > 99 ) {
  166.             center_text( NB_SCORES + 3, sprintf( buf, "%7d (%3d)   %8s ",
  167.                            score, balls_left, login ) );
  168.         } else if ( balls_left > 9 ) {
  169.             center_text( NB_SCORES + 3, sprintf( buf, "%7d  (%2d)   %8s ",
  170.                            score, balls_left, login ) );
  171.         } else if ( balls_left ) {
  172.             center_text( NB_SCORES + 3, sprintf( buf, "%7d   (%1d)   %8s ",
  173.                            score, balls_left, login ) );
  174.         } else {    /* no balls left */
  175.             center_text( NB_SCORES + 3, sprintf( buf, "%7d    <>   %8s ",
  176.                              score, login ) );
  177.         }
  178.     }
  179.     /* show the current pallet shrinkage */
  180.     center_text( NB_SCORES + 5, sprintf( buf, "pallet >>> %2d %% <<<",
  181.                 ( pallet_modif * 100 ) / PALLET_DENOMINATOR ) );
  182.  
  183.  
  184.     XFlush(display);
  185.  
  186.     /* provide some time to read */
  187.     sleep( 9 );
  188.     XCloseDisplay(display);
  189.     exit( 0 );        /* BYE !! */
  190. }
  191.