home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / games / volume14 / scrabble2 / part18 / smain.c < prev   
C/C++ Source or Header  |  1993-01-27  |  8KB  |  349 lines

  1. /*
  2.  *
  3.  * smain.c -- main program
  4.  *
  5.  */
  6.  
  7. #include "scrab.h"
  8. #include "globals.h"
  9.  
  10.  
  11. set_up_window() {
  12.     int xsize, ysize;
  13.     WINDOW *screen;
  14.  
  15.     screen = initscr();
  16.     refresh();
  17.     if( screen->_maxx < 80 || screen->_maxy < 24 ) {
  18.         endwin();
  19.         fprintf( stderr, "Need at least a 24x80 screen\n" );
  20.         exit( 1 );
  21.     }
  22.     cbreak();
  23.     noecho();
  24. }
  25.  
  26. exit_window() {
  27.     char c;
  28.  
  29.     move( 23, 0 );
  30.     printw( "Press any key to end? " );
  31.     refresh();
  32.     c = getch();
  33.     endwin();
  34. }
  35.  
  36. init_board() {
  37.     int i, j;
  38.  
  39.     for( i = 1; i < 16; i++ )
  40.         for( j = 1; j < 16; j++ )
  41.             board[i][j] = CH_EM;
  42.     board[1][1] = CH_TW;
  43.     board[8][1] = CH_TW;
  44.     board[8][8] = CH_DW;
  45.     for( i = 2; i < 6; i++ ) {
  46.         board[i][i] = CH_DW;
  47.     }
  48.     board[2][6] = CH_TL;
  49.     board[6][2] = CH_TL;
  50.     board[6][6] = CH_TL;
  51.     board[1][4] = CH_DL;
  52.     board[4][1] = CH_DL;
  53.     board[3][7] = CH_DL;
  54.     board[7][3] = CH_DL;
  55.     board[8][4] = CH_DL;
  56.     board[7][7] = CH_DL;
  57.     for( i = 1; i < 9; i++ )
  58.         for( j = 1; j < 8; j++ ) {
  59.             board[16-j][i] = board[i][j];
  60.             board[16-i][16-j] = board[i][j];
  61.             board[j][16-i] = board[i][j];
  62.         }
  63. }
  64.  
  65. print_info() {
  66.     move( INFO_Y    , INFO_X );
  67.     printw( "Special symbols:" );
  68.     move( INFO_Y + 1, INFO_X + 2 );
  69.     printw( "%c: Double letter score", CH_DL );
  70.     move( INFO_Y + 2, INFO_X + 2 );
  71.     printw( "%c: Triple letter score", CH_TL );
  72.     move( INFO_Y + 3, INFO_X + 2 );
  73.     printw( "%c: Double word score", 'o' );
  74.     move( INFO_Y + 4, INFO_X + 2 );
  75.     printw( "%c: Triple word score", CH_TW );
  76.     move( INFO_Y + 5, INFO_X + 2 );
  77.     printw( "%c: Blank tile", CH_BL );
  78.     move( INFO_Y + 7, INFO_X );
  79.     printw( "Tile values & distributions:" );
  80.     move( INFO_Y + 8, INFO_X + 2 );
  81.     printw( "A-1, 9  H-4, 2  O-1, 8  V-4, 2" );
  82.     move( INFO_Y + 9, INFO_X + 2 );
  83.     printw( "B-3, 2  I-1, 9  P-3, 2  W-4, 2" );
  84.     move( INFO_Y +10, INFO_X + 2 );
  85.     printw( "C-3, 2  J-8, 1  Q10, 1  X-8, 1" );
  86.     move( INFO_Y +11, INFO_X + 2 );
  87.     printw( "D-2, 4  K-5, 1  R-1, 6  Y-4, 2" );
  88.     move( INFO_Y +12, INFO_X + 2 );
  89.     printw( "E-1,12  L-1, 4  S-1, 4  Z10, 1" );
  90.     move( INFO_Y +13, INFO_X + 2 );
  91.     printw( "F-4, 2  M-3, 2  T-1, 6  %c-0, 2", CH_BL );
  92.     move( INFO_Y +14, INFO_X + 2 );
  93.     printw( "G-2, 3  N-1, 6  U-1, 4" );
  94.     refresh();
  95. }
  96.  
  97. init_tiles() {
  98.     int i, j;
  99.  
  100.     for( i = 0; i < 27; i++ )
  101.         tiles_left[i] = letters[i].tiles;
  102.     for( i = 0; i < 4; i++ )
  103.         for( j = 0; j < 7; j++ )
  104.             plr_tiles[i][j] = 0;
  105.     b1x = 0;
  106.     b2x = 0;
  107.     b1y = 0;
  108.     b2y = 0;
  109.     normal_sum = 0;
  110.     for( i = 0; i < 40; i++    ) normal_sum += normal_dist[i];
  111. }
  112.  
  113. get_num_players() {
  114.     char c;
  115.     int i, j;
  116.     char ft[4], cs[7];
  117.  
  118.     clear_prompt();
  119.     printw( "  Number of computer players (1-3)? " );
  120.     refresh();
  121.     for( ;; ) {
  122.         c = getch();
  123.         if( c >= '1' && c <= '3' ) break;
  124.     }
  125.     players = c - '0' + 1;
  126.     for( ;; ) {
  127.         init_tiles();
  128.         for( i = 0; i < players; i++ ) ft[i] = draw_tile();
  129.         for( i = 1; i < players; i++ )
  130.             if( ft[0] == ft[i] ) break;
  131.         if( i != players ) continue;
  132.         move( 20, 0 );
  133.         printw( "  All players draw a tile.  You: %c", ft[0] );
  134.         for( i = 1; i < players; i++ ) {
  135.             printw( "  Player %d: %c", i + 1, ft[i] );
  136.             if( ft[i] == CH_BL ) ft[i] = 'A' - 1;
  137.         }
  138.         j = players;
  139.         for( i = 1; i < players; i++ )
  140.             if( ft[0] < ft[i] ) j--;
  141.         if( j == 1 ) strcpy( cs, "first" );
  142.         if( j == 2 ) strcpy( cs, "second" );
  143.         if( j == 3 ) strcpy( cs, "third" );
  144.         if( j == 4 ) strcpy( cs, "fourth" );
  145.         printw( "\n  You will play %s.", cs );
  146.         human_player = j - 1;
  147.         print_tiles_left( 100 );
  148.         press_return();
  149.         clear_prompt();
  150.         refresh();
  151.         break;
  152.     }
  153.  
  154.     init_tiles();
  155.     j = '1';
  156.     for( i = 0; i < 4; i++ ) {
  157.         if( i == human_player ) {
  158.             strcpy( &(your[i][0]), "Your" );
  159.             strcpy( &(you[i][0]), "You" );
  160.         } else {
  161.             strcpy( &(your[i][0]), "CPU 's" );
  162.             strcpy( &(you[i][0]), "CPU " );
  163.             your[i][3] = j;
  164.             you[i][3] = j++;
  165.         }
  166.         plr_skills[i] = 0;
  167.     }
  168.  
  169.     for( i = 0; i < players; i++ ) {
  170.         print_tiles();
  171.         if( i == human_player ) continue;
  172.         clear_prompt();
  173.         printw( "  Select skill level 1-%d for %s, where 1 is beginner, %d is advanced, and\n", SKILL_EXPERT, you[i], SKILL_LEVELS );
  174.         printw( "  %d is expert. -> ", SKILL_EXPERT );
  175.         refresh();
  176.         for( ;; ) {
  177.             c = getch();
  178.             if( c >= '1' && c <= '6' ) break;
  179.         }
  180.         plr_skills[i] = c - '0';
  181.     }
  182.  
  183.     for( i = 0; i < players; i++ ) {
  184.         plr_scores[i] = 0;
  185.         for( j = 0; j < 7; j++ )
  186.             plr_tiles[i][j] = draw_tile();
  187.     }
  188.     print_tiles();
  189. }
  190.  
  191. end_game() {
  192.     int i, j;
  193.     int adj[4], wbs, wb[4], was, wa[4];
  194.     long p1;
  195.     FILE *fp;
  196.     char c;
  197.  
  198.     clear_turn();
  199.     printw( "*** GAME OVER\n" );
  200.     print_tiles();
  201.     clear_prompt();
  202.     printw( "  Here are the score adjustments: " );
  203.     wbs = -1;
  204.     for( i = 0; i < players; i++ )
  205.         if( plr_scores[i] > wbs ) wbs = plr_scores[i];
  206.     for( i = 0; i < players; i++ ) {
  207.         if( plr_scores[i] == wbs ) wb[i] = 1;
  208.         else wb[i] = 0;
  209.     }
  210.     for( i = 0; i < players; i++ ) {
  211.         adj[i] = 1;
  212.         for( j = 0; j < 7; j++ ) {
  213.             c = plr_tiles[i][j];
  214.             if( c != 0 ) {
  215.                 if( adj[i] == 1 ) adj[i] = 0;
  216.                 if( c != CH_BL ) adj[i] -= letters[c - 'A'].points;
  217.             }
  218.         }
  219.     }
  220.     for( j = 0; j < players; j++ )
  221.         if( adj[j] == 1 ) break;
  222.     if( j < players ) {
  223.         adj[j] = 0;
  224.         for( i = 0; i < players; i++ )
  225.             if( i != j ) adj[j] -= adj[i];
  226.     }
  227.     for( i = 0; i < players; i++ ) {
  228.         move( 19 + i, 36 );
  229.         if( i == human_player ) addch( ' ' );
  230.         printw( "%s: %d", you[i], adj[i] );
  231.         plr_scores[i] += adj[i];
  232.     }
  233.     was = -1;
  234.     for( i = 0; i < players; i++ )
  235.         if( plr_scores[i] > was ) was = plr_scores[i];
  236.     for( i = 0; i < players; i++ ) {
  237.         if( plr_scores[i] == was ) wa[i] = 1;
  238.         else wa[i] = 0;
  239.     }
  240.     press_return();
  241.     print_tiles();
  242.     clear_prompt();
  243.     move( 20, 0 );
  244.     was = 0;
  245.     for( i = 0; i < players; i++ ) was += wa[i];
  246.     wbs = 0;
  247.     for( i = 0; i < players; i++ ) wbs += wb[i];
  248.     if( was == 1 || wbs == 1 ) {
  249.         if( was == 1 ) {
  250.             for( i = 0; i < players; i++ )
  251.                 if( wa[i] == 1 ) break;
  252.         } else {
  253.             for( i = 0; i < players; i++ )
  254.                 if( wb[i] == 1 ) break;
  255.         }
  256.         printw( "  And the winner is:  %s!", you[i] );
  257.     } else {
  258.         printw( "  It was a tie between:  " );
  259.         wbs = 1;
  260.         for( i = 0; i < players; i++ )
  261.             if( wa[i] == 1 ) {
  262.                 if( wbs < was - 1 ) printw( ", " );
  263.                 else {
  264.                     if( was > 2 ) printw( ", and " );
  265.                     else printw( " and " );
  266.                 }
  267.                 printw( "%s", you[i] );
  268.                 wbs++;
  269.             }
  270.         printw( "!" );
  271.     }
  272.     refresh();
  273.     if( dict_changed == 1 ) {
  274.         press_return();
  275.         clear_prompt();
  276.         printw( "  The computer's dictionary was modified this game.\n" );
  277.         printw( "  Would you like to save the new dictionary (y/n)? " );
  278.         refresh();
  279.         do {
  280.             c = get_key();
  281.         } while( c != 'Y' && c != 'N' );
  282.         if( c == 'Y' ) {
  283.             printw( "\n\n  Writing new dictionary..." );
  284.             refresh();
  285.             fp = fopen( DICT_FILE, "w+" );
  286.             if( fp == NULL ) {
  287.                 printw( "error writing new dictionary!\n" );
  288.                 exit_window();
  289.                 exit( 1 );
  290.             }
  291.             for( p1 = 0; p1 < wlen[16]; p1++ )
  292.                 fprintf( fp, "%s\n", &(words[wptr[p1]]) );
  293.             fclose( fp );
  294.         }
  295.     }
  296. }
  297.  
  298. int main() {
  299.     int plr;
  300.     int i;
  301.  
  302.     set_up_window();
  303.     clear();
  304.     printw( "*** SCRABBLE 1.21 ***\n\n" );
  305.  
  306.     read_words();
  307.     dict_changed = 0;
  308.  
  309.     clear();
  310.     printw( "*** SCRABBLE 1.21 ***\n\n" );
  311.  
  312.     init_board();
  313.     seed_random();
  314.     print_board();
  315.     print_info();
  316.     get_num_players();
  317.  
  318.     game_done = 0;
  319.     abort = 0;
  320.     do {
  321.         for( plr = 0; plr < players; plr++ ) {
  322.             clear_turn();
  323.             printw( "*** %s turn to play  ", your[plr] );
  324.             if( plr == human_player ) {
  325.                 player_move();
  326.             } else {
  327.                 computer_move( plr );
  328.             }
  329.             if( abort != 0 ) break;
  330.             if( game_done == 0 ) clear_rect( 1, 50, 1, 79 );
  331.             else {
  332.                 move( 1, 64 );
  333.                 printw( "Passed turns:  %d", game_done );
  334.             }
  335.             refresh();
  336.             if( game_done == players ) break;
  337.             for( i = 0; i < 7; i++ )
  338.                 if( plr_tiles[plr][i] != 0 ) break;
  339.             if( i == 7 ) {
  340.                 game_done = players;
  341.                 break;
  342.             }
  343.         }
  344.     } while( game_done < players && abort == 0 );
  345.  
  346.     if( abort == 0 ) end_game();
  347.     exit_window();
  348. }
  349.