home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / games / volume2 / nchess / part03 / main.c < prev    next >
C/C++ Source or Header  |  1987-11-25  |  6KB  |  219 lines

  1. /*
  2.  * Copyright 1987 Tom Anderson; 20831 Frank Waters Road;
  3.  * Stanwood, WA  98282.   All rights reserved.
  4.  */
  5.  
  6. /*
  7.  * main client part of network chess.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <strings.h>
  12. #include <errno.h>
  13. #include <suntool/tool_hs.h>
  14. #include <pwd.h>
  15.  
  16. #include "nchess.h"
  17.  
  18. int errno;                /* global error number */
  19.  
  20. char * TranscriptFileName = "nchess.transcript";
  21. int TranscriptType = TR_MIN_NORMAL;
  22. char * SaveFileName = "nchess.save";
  23. char * PlayerName[2];
  24. BOOL SetupMode = FALSE;
  25. BOOL IsMachine[2] = { FALSE, FALSE };
  26. FILE * RestoreFile;            /* restoration file */
  27. struct passwd * UserPWEntry;
  28.  
  29. #define    MAXCHESSPROCARGS    16
  30. char * ChessProcessArgs[2][MAXCHESSPROCARGS];
  31.  
  32. /*
  33.  * walk away from the game in the middle on receipt of an appropriate
  34.  * signal
  35.  */
  36. bobbyFischer()
  37. {
  38.     /* kill any and all chess processes */
  39.     KillChessProcesses();
  40.     /* send a goodbye to the peer */
  41.     SendGoodbye();
  42.     exit(1);
  43. }
  44.  
  45. main(argc, argv)
  46.     int argc;
  47.     char ** argv;
  48. {
  49.     register int i, j;
  50.     char * cp = (char *) 0, * iconDirectory = (char *) 0;
  51.     int chessArgIndex[2];
  52.     BOOL useRetained = TRUE;
  53.  
  54.     if ((UserPWEntry = getpwuid(getuid())) == (struct passwd *) 0) {
  55.     fputs("Can't determine who you are\n", stderr);
  56.     exit(1);
  57.     }
  58.     chessArgIndex[BLACK] = chessArgIndex[WHITE] = 0;
  59.     /* randomize things a bit */
  60.     (void) srandom((long) getpid());
  61.     /* parse and strip out the tool-related arguments */
  62.     ParseToolArgs(&argc, argv);
  63.     /* scan the remaining argument list */
  64.     for (i = 1 ; i < argc ; i++) {
  65.     if (argv[i][0] == '-') {
  66.         switch (argv[i][1]) {
  67.         case 'c':
  68.         useRetained = FALSE;    /* use a retained pixrect for the board */
  69.         break;
  70.         case 'v':
  71.         MachineDebug = TRUE;    /* turn on chess program i/f debugging */
  72.         break;
  73.         case 'd':            /* custom piece icon directory name */
  74.         if (argv[i][j=2] == '\0' && (j = 0 , ++i >= argc)) {
  75.             fprintf(stderr, "%s: bad piece icon directory name arg\n", argv[0]);
  76.             exit(1);
  77.         } 
  78.         iconDirectory = &argv[i][j];
  79.         break;
  80.         case 'b':            /* wants to play the black pieces */
  81.         MyColor = WANTSBLACK;
  82.         break;
  83.         case 'w':            /* wants to play the white pieces */
  84.         MyColor = WANTSWHITE;
  85.         break;
  86.         case 'x':            /* specify transcript notation type */
  87.         if (argv[i][j=2] == '\0' && (j = 0 , ++i >= argc)
  88.         || sscanf(&argv[i][j], "%d", &TranscriptType) != 1
  89.         || TranscriptType < TR_MIN_TYPE
  90.         || TranscriptType > TR_MAX_TYPE) {
  91.             fprintf(stderr, "%s: bad transcript mode arg\n", argv[0]);
  92.             exit(1);
  93.         }
  94.         break;
  95.         case 't':            /* specify transcript file name */
  96.         if (argv[i][j=2] == '\0' && (j = 0 , ++i >= argc)) {
  97.             fprintf(stderr, "%s: bad transcript filename arg\n", argv[0]);
  98.             exit(1);
  99.         } 
  100.         TranscriptFileName = &argv[i][j];
  101.         break;
  102.         case 's':            /* specify save file name */
  103.         if (argv[i][j=2] == '\0' && (j = 0 , ++i >= argc)) {
  104.             fprintf(stderr, "%s: bad save filename arg\n", argv[0]);
  105.             exit(1);
  106.         } 
  107.         SaveFileName = &argv[i][j];
  108.         break;
  109.         case 'r':
  110.         if (argv[i][j=2] == '\0' && (j = 0 , ++i >= argc)
  111.         || ((RestoreFile = fopen(&argv[i][j], "r")) == (FILE *) 0)) {
  112.             fprintf(stderr, "%s: bad restore file arg\n", argv[0]);
  113.             exit(1);
  114.         }
  115.         if (fread((caddr_t) &MyColor, sizeof(MyColor), 1, RestoreFile) != 1) {
  116.             fputs("Bad save file\n", stderr);
  117.             exit(1);
  118.         }
  119.         break;
  120.         case 'e':
  121.         SetupMode = TRUE;
  122.         Mouse = SETUP;
  123.         break;
  124.         case 'm':
  125.         IsMachine[WHITE] = TRUE;
  126.         if (argv[i][j=2] == '\0' && (j = 0 , ++i >= argc)) {
  127.             fprintf(stderr, "%s: bad chess program arg\n", argv[0]);
  128.             exit(1);
  129.         }
  130.         if (chessArgIndex[WHITE] >= MAXCHESSPROCARGS - 1) {
  131.             fprintf(stderr, "%s: too many chess program args\n", argv[0]);
  132.             exit(1);
  133.         }
  134.         ChessProcessArgs[WHITE][chessArgIndex[WHITE]++] = &argv[i][j];
  135.         ChessProcessArgs[WHITE][chessArgIndex[WHITE]] = (char *) 0;
  136.         break;
  137.         case 'n':
  138.         IsMachine[BLACK] = TRUE;
  139.         if (argv[i][j=2] == '\0' && (j = 0 , ++i >= argc)) {
  140.             fprintf(stderr, "%s: bad chess program arg\n", argv[0]);
  141.             exit(1);
  142.         }
  143.         if (chessArgIndex[BLACK] >= MAXCHESSPROCARGS - 1) {
  144.             fprintf(stderr, "%s: too many chess program args\n", argv[0]);
  145.             exit(1);
  146.         }
  147.         ChessProcessArgs[BLACK][chessArgIndex[BLACK]++] = &argv[i][j];
  148.         ChessProcessArgs[BLACK][chessArgIndex[BLACK]] = (char *) 0;
  149.         break;
  150.         }
  151.     } else 
  152.         cp = argv[i];
  153.     }
  154.     if (SetupMode && RestoreFile != (FILE *) 0) {
  155.     fprintf(stderr, "%s: can only specify one of -r and -s\n", argv[0]);
  156.     exit(1);
  157.     }
  158.     /*
  159.      * if white is a machine, start up the white chess process and
  160.      * force us to play the black pieces (if we get to play at all)
  161.      */
  162.     if (IsMachine[WHITE]) {
  163.     InitChessProcess(ChessProcessArgs[WHITE], WHITE, 
  164.         ! SetupMode && RestoreFile == (FILE *) 0);
  165.     PlayerName[WHITE] = ChessProcessArgs[WHITE][0];
  166.     MyColor = BLACK;
  167.     }
  168.     /*
  169.      * if black is a machine, start up the black chess process and
  170.      * force us to play the white pieces (if we get to play at all)
  171.      */
  172.     if (IsMachine[BLACK]) {
  173.     InitChessProcess(ChessProcessArgs[BLACK], BLACK, FALSE);
  174.     PlayerName[BLACK] = ChessProcessArgs[BLACK][0];
  175.     MyColor = WHITE;
  176.     }
  177.     /*
  178.      * if both players are human, initialize the peer-to-peer RPC 
  179.      * "connection" 
  180.      */
  181.     if ( ! IsMachine[WHITE] && ! IsMachine[BLACK]) {
  182.     InitRPC(cp, argv[0]);
  183.     /*
  184.      * else if both players are machines, lock the mouse to avoid
  185.      * spectator interference (unless the spectator is going to set
  186.      * up the cannon fodder first)
  187.      */
  188.     } else if (IsMachine[WHITE] && IsMachine[BLACK]) {
  189.     if ( ! SetupMode)
  190.         Mouse = LOCKED;            /* lock out the mouse */
  191.     /*
  192.      * else the user is playing a machine
  193.      */
  194.     } else {
  195.     PlayerName[MyColor] = UserPWEntry->pw_name;
  196.     PeerColor = OTHERCOLOR(MyColor);
  197.     }
  198.     signal(SIGINT, bobbyFischer);
  199.     signal(SIGQUIT, bobbyFischer);
  200.     signal(SIGHUP, bobbyFischer);
  201.     /* initialize the board state */
  202.     InitBoard();
  203.     /* 
  204.      * if we want to restore the game and our opponent hasn't beaten
  205.      * us to it, try to do it now 
  206.      */
  207.     if (RestoreFile != (FILE *) 0 && ! RestoringGame)
  208.     RestoreGame();
  209.     /* initialize and install the tool */
  210.     InitTool(useRetained, iconDirectory);
  211.     tool_install(NchessTool);
  212.     /* now play the game */
  213.     RunTool();
  214.     /* post-game cleanup */
  215.     KillChessProcesses();
  216.     SendGoodbye();
  217. }
  218.  
  219.