home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / games / volume14 / scrabble2 / part14 / misc.c next >
C/C++ Source or Header  |  1993-01-27  |  5KB  |  228 lines

  1. /*
  2.  *
  3.  * misc.c -- miscellaneous routines
  4.  *
  5.  */
  6.  
  7. #include "scrab.h"
  8. #include "globals.h"
  9.  
  10. char spaces[81] =
  11. "                                        \
  12.                                         ";
  13.  
  14. read_words() {
  15.     FILE *fp;
  16.     char inp[16], tinp[16];
  17.     int i, tlen, rlen;
  18.     long pt, wr;
  19.  
  20.     fp = fopen( DICT_FILE, "r" );
  21.     if( fp == NULL ) {
  22.         move( 4, 0 );
  23.         printw( "Error: could not find dictionary file '%s'\n", DICT_FILE );
  24.         exit_window();
  25.         exit( 1 );
  26.     }
  27.     printw( "Reading dictionary...\n" );
  28.     refresh();
  29.     pt = 0;
  30.     wr = 0;
  31.     tlen = 2;
  32.     wlen[tlen] = wr;
  33.     tinp[0] = '\0';
  34.     while( fscanf( fp, "%s", inp ) != EOF ) {
  35.         rlen = strlen( inp );
  36.         if( rlen < 2 || rlen > 15 ) {
  37.             move( 4, 0 );
  38.             printw( "Error in dictionary file at word %s: words must be between 2 and 15\n", inp );
  39.             printw( "characters long.\n" );
  40.             exit_window();
  41.             exit( 1 );
  42.         }
  43.         if( rlen < strlen( tinp ) ||
  44.             ( strcmp( tinp, inp ) > 0 && strlen( tinp ) == rlen ) ) {
  45.             move( 4, 0 );
  46.             printw( "Error in dictionary file at the words %s and %s.\n",
  47.                       tinp, inp );
  48.             printw( "The dictionary must be sorted from shortest words to longest, and\n" );
  49.             printw( "alphabetically within each list of words of the same length." );
  50.             exit_window();
  51.             exit( 1 );
  52.         }
  53.         for( i = 0; i < rlen; i++ ) {
  54.             if( inp[i] >= 'a' && inp[i] <= 'z' ) inp[i] = inp[i] - 'a' + 'A';
  55.             else if( inp[i] < 'A' || inp[i] > 'Z' ) {
  56.                 move( 4, 0 );
  57.                 printw( "Error in dictionary file at word %s: words must contain alphabetic characters\n", inp );
  58.                 printw( "only.\n" );
  59.                 exit_window();
  60.                 exit( 1 );
  61.             }
  62.         }
  63.         if( rlen > tlen ) {
  64.             tlen = rlen;
  65.             wlen[tlen] = wr;
  66.         }
  67.         strcpy( &words[pt], inp );
  68.         wptr[wr++] = pt;
  69.         pt += rlen + 1;
  70.         strcpy( tinp, inp );
  71.     }
  72.     dict_size = pt;
  73.     wlen[16] = wr;
  74.     fclose( fp );
  75. }
  76.  
  77. clear_rect( y1, x1, y2, x2 )
  78.   int y1, x1, y2, x2;
  79. {
  80.     int i;
  81.  
  82.     for( i = y1; i <= y2; i++ ) {
  83.         move( i, x1 );
  84.         printw( "%s", &spaces[79 - ( x2 - x1 )] );
  85.     }
  86.     move( y1, x1 );
  87. }
  88.  
  89. clear_prompt() {
  90.     clear_rect( 19, 0, 23, 78 );
  91.     move( 19, 0 );
  92. }
  93.  
  94. clear_turn() {
  95.     clear_rect( 18, 0, 18, 79 );
  96.     move( 18, 0 );
  97. }
  98.  
  99. press_return() {
  100.     char c;
  101.  
  102.     move( 23, 0 );
  103.     printw( "Press <RETURN> -> " );
  104.     refresh();
  105.     do {
  106.         c = getch();
  107.         if( c == 'q' || c == 'Q' ) abort = 1;
  108.     } while( c != '\n' );
  109. }
  110.  
  111. print_board() {
  112.     int i, j;
  113.  
  114.     for( i = 1; i < 16; i++ ) {
  115.         move( BOARD_UY + i - 1, BOARD_LX );
  116.         for( j = 1; j < 16; j++ ) {
  117.             if( board[i][j] >= 'A' ) standout();
  118.             if( ( i == b1y && j == b1x ) || ( i == b2y && j == b2x ) )
  119.                 addch( board[i][j] - 'A' + 'a' );
  120.             else if( board[i][j] == CH_DW ) addch( 'o' );
  121.             else addch( board[i][j] );
  122.             if( board[i][j] >= 'A' ) standend();
  123.         }
  124.     }
  125.     refresh();
  126. }
  127.  
  128. print_tiles() {
  129.     int i, j;
  130.  
  131.     clear_rect( TILE_RACKS_Y, TILE_RACKS_X,
  132.                   TILE_RACKS_Y + 7, TILE_RACKS_X + 24 );
  133.     for( i = 0; i < players; i++ ) {
  134.         move( TILE_RACKS_Y + i * 3, TILE_RACKS_X - 4 );
  135.         if( plr_skills[i] != 0 ) printw( "(%d)", plr_skills[i] );
  136.         move( TILE_RACKS_Y + i * 3, TILE_RACKS_X );
  137.         if( i == human_player )
  138.             printw( "Your tiles:" );
  139.         else
  140.             printw( "%s tiles:", you[i] );
  141.         for( j = 0; j < 7; j++ ) {
  142.             addch( ' ' );
  143.             if( plr_tiles[i][j] > 0 ) {
  144. #ifndef PRINT_CPU_TILES
  145.                 if( i == human_player || game_done == players )
  146. #endif
  147.                     addch( plr_tiles[i][j] );
  148. #ifndef PRINT_CPU_TILES
  149.                 else addch( '-' );
  150. #endif
  151.             } else addch( ' ' );
  152.         }
  153.         move( TILE_RACKS_Y + i * 3 + 1, TILE_RACKS_X + 5 );
  154.         printw( "Score: %3d", plr_scores[i] );
  155.     }
  156.     move( TILE_RACKS_Y + 12, TILE_RACKS_X - 2 );
  157.     i = 0;
  158.     if( b1x != 0 ) i++;
  159.     if( b2x != 0 ) i++;
  160.     printw( "Blank tiles placed so far: %d", i );
  161.     if( b1x != 0 ) {
  162.         move( TILE_RACKS_Y + 13, TILE_RACKS_X + 3 );
  163.         printw( "Row %2d, Column %2d (%c)", b1y, b1x, board[b1y][b1x] );
  164.     }
  165.     if( b2x != 0 ) {
  166.         move( TILE_RACKS_Y + 14, TILE_RACKS_X + 3 );
  167.         printw( "Row %2d, Column %2d (%c)", b2y, b2x, board[b2y][b2x] );
  168.     }
  169.     move( TILE_RACKS_Y + human_player * 3, TILE_RACKS_X - 1 );
  170.     refresh();
  171. }
  172.  
  173. char draw_tile() {
  174.     int i, tleft;
  175.     int tile;
  176.  
  177.     tleft = 0;
  178.     for( i = 0; i < 27; i++ )
  179.         tleft += tiles_left[i];
  180.  
  181.     if( tleft == 0 ) return( 0 );
  182.     tile = get_random() % tleft;
  183.     for( i = 0;; i++ ) {
  184.         if( tile - tiles_left[i] >= 0 )
  185.             tile -= tiles_left[i];
  186.         else
  187.             break;
  188.     }
  189.     tiles_left[i]--;
  190.     print_tiles_left( tleft - 1 );
  191.     return( i == 26 ? CH_BL : i + 'A' );
  192. }
  193.  
  194. print_tiles_left( t )
  195.   int t;
  196. {
  197.     move( TILES_LEFT_Y, TILES_LEFT_X );
  198.     printw( "Tiles left to draw: %3d", t );
  199. }
  200.  
  201. char get_key() {
  202.     char c;
  203.  
  204.     c = getch();
  205.     if( c >= 'a' && c <= 'z' ) c = c - 'a' + 'A';
  206. /*    if( c == '\033' )
  207.         if( getch() == '[' ) {
  208.             c = getch();
  209.             if( c == 'A' ) c = 'K';
  210.             else if( c == 'B' ) c = 'J';
  211.             else if( c == 'C' ) c = 'L';
  212.             else if( c == 'D' ) c = 'H';
  213.             else c = '\0';
  214.         } else c = '\0'; */
  215.     return( c );
  216. }
  217.  
  218. seed_random() {
  219.     struct timeval tp;
  220.  
  221.     gettimeofday( &tp, 0 );
  222.     srandom( tp.tv_sec % 65536 - 32768 );
  223. }
  224.  
  225. long get_random() {
  226.     return( (long) random() );
  227. }
  228.