home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / go / prog / sg2ishi5.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  1993-06-20  |  17.7 KB  |  588 lines

  1. #! /bin/sh
  2. # This is a shell archive, meaning:
  3. # 1. Remove everything above the #! /bin/sh line.
  4. # 2. Save the resulting text in a file.
  5. # 3. Execute the file with /bin/sh (not csh) to create:
  6. #  sg2ishi.c
  7. #  readme
  8. #  sample01.mgt
  9. #
  10. echo shar: "Now extracting shell archive for sg2ishi files.."
  11. #
  12. if test -f 'sg2ishi.c'
  13. then
  14.     echo shar: "will not overwrite existing file 'sg2ishi.c'"
  15. else
  16. cat << \SHAR_EOF > 'sg2ishi.c'
  17. /**********************************************************************/
  18. /*   Copyright  John  Unger,  all  rights  reserved.  Permission  is  */
  19. /*   given  to  modify  and  distribute  this source code as long as  */
  20. /*   proper  credit  is  given.  In using this software you agree to  */
  21. /*   spirit   of  this  copyright.  No  guarantee  is  made  of  the  */
  22. /*   correctness of this software, and it is used at your own risk.     */
  23. /**********************************************************************/
  24.  
  25. /*
  26. sg2ishi, Version 0.5, 12/17/92
  27.  
  28. program to convert SmartGo (mgt) go files to Many Faces of Go files
  29. */
  30.   
  31. #include <stdio.h>
  32. #include <ctype.h>
  33.   
  34. #define TRUE 1
  35. #define FALSE 0
  36.   
  37. void main(argc, argv)
  38. int argc;
  39. char **argv;
  40. {
  41.     FILE *in_file, *out_file;
  42.     int k;
  43.     int j=1, i=1;
  44.     int first=FALSE;
  45.     int setboard=FALSE;
  46.     char ch;
  47.     char tok1, tok2;
  48.     if (argc != 3)
  49.     {
  50.         fprintf(stderr,"\t\tTo use the program type:\n");
  51.         fprintf(stderr,"\t\tsg2ishi in_file out_file\n");
  52.         exit(0);
  53.     }
  54.   
  55.     if ((in_file = fopen(argv[1], "rt")) == NULL)
  56.     {
  57.         fprintf(stderr,"%s is unable to open %s for input\n", argv[0], argv[1]);
  58.         exit(0);
  59.     }
  60.     if ((out_file = fopen(argv[2], "wt")) == NULL)
  61.     {
  62.         fprintf(stderr, "%s is unable to open %s for output\n", argv[0], argv[2]);
  63.         exit(0);
  64.     }
  65.   
  66.     while((ch = fgetc(in_file)) != EOF)
  67.     {
  68.         if (ch == '(')
  69.             fprintf(out_file,"EVENT %d\n", j++);
  70.             
  71.         if (isupper(ch))
  72.         {
  73.             tok2 = ' ';
  74.             if (first == TRUE)
  75.             {
  76.                 tok2 = ch;
  77.                 first = FALSE;
  78.                 continue;
  79.             }
  80.             tok1 = ch;
  81.             first = TRUE;
  82.             continue;
  83.         }
  84.   
  85.         if (ch == '[')
  86.         {
  87.             switch (tok1)
  88.             {
  89.                 case 'A':
  90.                 {
  91.                     if ((tok2 == 'B'))
  92.                     {
  93.                         fputs("SETUP B ", out_file);
  94.                         
  95.                         while((ch = fgetc(in_file)) != '\n')
  96.                         {
  97.                             if ((ch == '[') || (ch == ']'))
  98.                                 continue;
  99.                             if (ch >= 'i')
  100.                                 ch++;
  101.                             fputc(ch, out_file);
  102.                             fprintf(out_file,"%d ",116-fgetc(in_file));
  103.                         }
  104.                         fputc('\n', out_file);
  105.                     }
  106.                     
  107.                     else if ((tok2 == 'W'))
  108.                     {
  109.                         fputs("SETUP W ", out_file);
  110.                         
  111.                         while((ch = fgetc(in_file)) != '\n')
  112.                         {
  113.                             if ((ch == '[') || (ch == ']'))
  114.                                 continue;
  115.                             if (ch >= 'i')
  116.                                 ch++;
  117.                             fputc(ch, out_file);
  118.                             fprintf(out_file,"%d ",116-fgetc(in_file));
  119.                         }
  120.                         fputc('\n', out_file);
  121.                     }
  122.                     break;
  123.                 }
  124.   
  125.                 case 'B':
  126.                 {
  127.                     if (tok2 == 'R')
  128.                     {
  129.                         fputs("COM Black's rank is ", out_file);
  130.                         while((ch = fgetc(in_file)) != ']')
  131.                             fputc(ch, out_file);
  132.                         fputc('\n', out_file);
  133.                         fputs("ENDCOM\n", out_file);
  134.                         break;
  135.                     }
  136.                     
  137.                     else if (tok2 == 'L')
  138.                         continue;
  139.                     
  140.                     else
  141.                     {
  142.                         fprintf(out_file,"B %d ", i);
  143.                         while((ch = fgetc(in_file)) != ']')
  144.                         {
  145.                             if (ch >= 'i')
  146.                                 ch++;
  147.                             fputc(ch, out_file);
  148.                             fprintf(out_file,"%d\n",116-fgetc(in_file));
  149.                         }
  150.                         i++;  /* increment move number */
  151.                         break;
  152.                     }
  153.                 }
  154.   
  155.                 case 'C':
  156.                 {
  157.                     fputs("COM\n", out_file);
  158.                     while((ch = fgetc(in_file)) != ']')
  159.                         fputc(ch, out_file);
  160.                     fputc('\n', out_file);
  161.                     fputs("ENDCOM\n", out_file);
  162.                     break;
  163.                 }
  164.   
  165.                 case 'D':
  166.                 {
  167.                     if ((tok2 == 'T'))
  168.                     {
  169.                         fputs("DATE ", out_file);
  170.                         while((ch = fgetc(in_file)) != ']')
  171.                             fputc(ch, out_file);
  172.                         fputc('\n', out_file);
  173.                     }
  174.                     break;
  175.                 }
  176.   
  177.                 case 'E':
  178.                 {
  179.                     fputs("COM\n", out_file);
  180.                     while((ch = fgetc(in_file)) != ']')
  181.                         fputc(ch, out_file);
  182.                     fputc('\n', out_file);
  183.                     fputs("ENDCOM\n", out_file);
  184.                     break;
  185.                 }
  186.   
  187.                 case 'K':
  188.                 {
  189.                     if ((tok2 == 'M'))
  190.                     {
  191.                         fputs("KOMI ", out_file);
  192.                         while((ch = fgetc(in_file)) != ']')
  193.                             fputc(ch, out_file);
  194.                         fputc('\n', out_file);
  195.                     }
  196.                     break;
  197.                 }
  198.   
  199.                 case 'G':
  200.                 {
  201.                     if (tok2 == 'N')
  202.                     {
  203.                         fputs("COM\n", out_file);
  204.                         while((ch = fgetc(in_file)) != ']')
  205.                             fputc(ch, out_file);
  206.                         fputc('\n', out_file);
  207.                         fputs("ENDCOM\n", out_file);
  208.                         break;
  209.                     }
  210.                     else
  211.                         break;
  212.                 }
  213.   
  214.                 case 'L':
  215.                 {
  216.                     fputs("MARK ", out_file);
  217.                     k = 0;
  218.                     
  219.                     while((ch = fgetc(in_file)) != '\n')
  220.                     {
  221.                         if ((ch == '[') || (ch == ']'))
  222.                             continue;
  223.                         if (ch >= 'i')
  224.                             ch++;
  225.                         fputc(k+97, out_file);
  226.                         fputc('@', out_file);
  227.                         fputc(ch, out_file);
  228.                         fprintf(out_file,"%d ",116-fgetc(in_file));
  229.                         k++;
  230.                     }
  231.                     fputc('\n', out_file);
  232.                     break;
  233.                 }
  234.                     
  235.                 case 'M':
  236.                 {
  237.                     fputs("MARK ", out_file);
  238.                     
  239.                     while((ch = fgetc(in_file)) != '\n')
  240.                     {
  241.                         if ((ch == '[') || (ch == ']'))
  242.                             continue;
  243.                         if (ch >= 'i')
  244.                             ch++;
  245.                         fputc('x', out_file);
  246.                         fputc('@', out_file);
  247.                         fputc(ch, out_file);
  248.                         fprintf(out_file,"%d ",116-fgetc(in_file));
  249.                     }
  250.                     fputc('\n', out_file);
  251.                     break;
  252.                 }
  253.                     
  254.                 case 'N':
  255.                 {
  256.                     fputs("COM\n", out_file);
  257.                     while((ch = fgetc(in_file)) != ']')
  258.                         fputc(ch, out_file);
  259.                     fputc('\n', out_file);
  260.                     fputs("ENDCOM\n", out_file);
  261.                     break;
  262.                 }
  263.   
  264.                 case 'P':
  265.                 {
  266.                     switch (tok2)
  267.                     {
  268.                         case 'B':
  269.                         {
  270.                             fputs("BLACK ", out_file);
  271.                             while((ch = fgetc(in_file)) != ']')
  272.                                 fputc(ch, out_file);
  273.                             fputc('\n', out_file);
  274.                             break;
  275.                         }
  276.   
  277.                         case 'C':
  278.                         {
  279.                             fputs("PLACE ", out_file);
  280.                             while((ch = fgetc(in_file)) != ']')
  281.                                 fputc(ch, out_file);
  282.                             fputc('\n', out_file);
  283.                             break;
  284.                         }
  285.   
  286.                         case 'W':
  287.                         {
  288.                             fputs("WHITE ", out_file);
  289.                             while((ch = fgetc(in_file)) != ']')
  290.                                 fputc(ch, out_file);
  291.                             fputc('\n', out_file);
  292.                             break;
  293.                         }
  294.                         default:
  295.                             break;
  296.                     } /* end switch */
  297.                     break;
  298.                 }
  299.   
  300.                 case 'R':
  301.                 {
  302.                     if ((tok2 == 'E'))
  303.                     {
  304.                         fputs("RESULT ", out_file);
  305.                         while((ch = fgetc(in_file)) != ']')
  306.                             fputc(ch, out_file);
  307.                         fputc('\n', out_file);
  308.                     }
  309.                     break;
  310.                 }
  311.   
  312.                 case 'S':
  313.                 {
  314.                     if ((tok2 == 'Z'))
  315.                     {
  316.                         fputs("BOARDSIZE ", out_file);
  317.                         while((ch = fgetc(in_file)) != ']')
  318.                             fputc(ch, out_file);
  319.                         fputc('\n', out_file);
  320.                         setboard = TRUE;
  321.                     }
  322.                     break;
  323.                 }
  324.   
  325.                 case 'U':
  326.                 {
  327.                     fputs("COM\n", out_file);
  328.                           fputs("Game entered by: ", out_file);
  329.                     while((ch = fgetc(in_file)) != ']')
  330.                         fputc(ch, out_file);
  331.                     fputc('\n', out_file);
  332.                     fputs("ENDCOM\n", out_file);
  333.                     break;
  334.                 }
  335.   
  336.                 case 'W':
  337.                 {
  338.                     if (tok2 == 'R')
  339.                     {
  340.                         fputs("COM White's rank is ", out_file);
  341.                         while((ch = fgetc(in_file)) != ']')
  342.                             fputc(ch, out_file);
  343.                         fputc('\n', out_file);
  344.                         fputs("ENDCOM\n", out_file);
  345.                         break;
  346.                     }
  347.                     
  348.                     else if (tok2 == 'L')
  349.                         break;
  350.                     
  351.                     else
  352.                     {
  353.                         fprintf(out_file,"W %d ", i);
  354.                         while((ch = fgetc(in_file)) != ']')
  355.                         {
  356.                             if (ch >= 'i')
  357.                                 ch++;
  358.                             fputc(ch, out_file);
  359.                             fprintf(out_file,"%d\n",116-fgetc(in_file));
  360.                         }
  361.                         i++; /* increment move number */
  362.                         break;
  363.                     }
  364.                 }
  365.                 default:
  366.                     break;
  367.                     
  368.             } /* end switch */
  369.   
  370.             first = FALSE;
  371.   
  372.         } /* end if */
  373.   
  374.     } /* end while */
  375.   
  376.     if (!setboard)
  377.     {
  378.         fprintf(stderr,"The size of the board was not found in the input file\n");
  379.         fprintf(stderr,"I am assuming that the game was played on a 19 x 19 board.\n\n");
  380.     }
  381.     fprintf(stderr,"I have successfully translated %s from Smart-Go format\n",argv[1]);
  382.     fprintf(stderr,"to %s in Ishi Stardard format.\n", argv[2]);
  383.     fclose(in_file);
  384.     fclose(out_file);
  385. }
  386. SHAR_EOF
  387. fi
  388. if test -f 'readme.go'
  389. then
  390.     echo shar: "will not over-write existing file 'readme.go'"
  391. else
  392. cat << \SHAR_EOF > 'readme.go'
  393. I wrote sg2ishi to convert go game files written in the
  394. popular "mgt" format into ones that would be compatible with
  395. the Ishi-standard format used by the Many Faces of Go program.
  396. Actually, sg2ishi uses the subset of Smart-Go commands that
  397. are supported by mgt and that have equivalent commands in the
  398. Ishi-standard formt. (See table 1 below..)
  399.  
  400. Certain "features" of the mgt format could not be incorporated
  401. into the Ishi-standard as it is represented by Many Faces of GO.
  402.  
  403. 1. If multiple games or problems are contained in one mgt file,
  404. Many Faces of Go will play only the first "event" in that file
  405. after it is converted to the Ishi-standard format by sg2ishi.
  406. This problem is apparently a limitation of the way that Many
  407. Faces of Go incorporates the Ishi-standard format.
  408.  
  409. 2. Variations are not incorporated into sg2ishi yet. The
  410. program will only work for single game records in this version. 
  411.  
  412. 3. The comments in mgt formatted files sometimes include
  413. references to the goban's coordinates using the standard A->T
  414. and 1->19 coordinate system. The board used by Many Faces of Go
  415. does not show coordinates. It's on my "to-do" list to abstract
  416. such information from the comments and actually mark the
  417. points on the goban referred to by the comments.
  418.  
  419. 4. Some mgt files contain problems or partially-played game
  420. situations starting out with some number of stones already
  421. played on the goban. If the mgt file uses the AB (AddBlack) or
  422. AW (AddWhite) notation, then sg2ishi will place all of the
  423. stones on the board at once. However, if the mgt file used the
  424. format:
  425.  
  426. Black[pd][dp][op][lp]
  427. ;
  428. White[qp][dc][po][pl]
  429. ;    
  430.  
  431. the stones will be placed with eight sequential "moves" when
  432. the game is played by Many Faces of Go.
  433.  
  434.  
  435. TABLE 1.
  436.  
  437. mgt notation supported by sg2ishi:
  438. --------------------------------------
  439. AB: add black stones     [point list] 
  440. AW: add white stones     [point list] 
  441. B : Black move           [move]       
  442. BR: Black's rank         [text]       
  443. C : Comment              [Text]       
  444. DT: date                 [text]       
  445. EV: event (tournament)   [text]       
  446. GC: game comment         [text]       
  447. GN: game name            [text]       
  448. KM: komi                 [real]       
  449. L : letters on points    [point list] 
  450. M : marked points        [point list] 
  451. N : Node Name            [Text]       
  452. PB: black player name    [text]       
  453. PC: place                [text]       
  454. PW: white player name    [text]       
  455. RE: result, outcome      [text]       
  456. SZ: board size           [number]     
  457. US: who entered game     [text]
  458. W : White move           [move]       
  459. WR: White's rank         [text]       
  460.  
  461.  
  462. mgt notation not supported by sg2ishi
  463. --------------------------------------
  464. AE: add empty stones     [point list] 
  465. GM: game                 [number]     
  466. HA: handicap             [number]     
  467. PL: player to play first [color]      
  468. RO: round                [text]       
  469. SO: source (book, journa [text]       
  470. TM: time limit per playe [text]       
  471. VW: view                              
  472.  
  473.  
  474. This program is in a true beta version; I have tested it on
  475. mgt files that I have downloaded from milton.u.washington.edu
  476. and that were created by a variety of players, but I am sure
  477. that there are some input files that will break the program
  478. somewhere. All suggestions, comments, etc., are solicited and
  479. should be sent via email to:
  480. junger@mtn.er.usgs.gov
  481. or, if email is unavailable, I read the Usenet group:
  482. rec.games.go regularly.
  483.  
  484. As you can see from the source code, I used a very
  485. straightforward approach to writing this program; no tables,
  486. no yacc, no lex, just some switch, case, and if statements. It
  487. should be easy to follow the logic of the code from top to
  488. bottom, as they used to say... Furthermore, nothing in the
  489. program is OS-specific; it should compile and run on any
  490. hardware that has a C compiler. If you want to add some of the
  491. unsupported mgt features, have at it.
  492.  
  493. The MS-DOS executible in the zipped file was generated with
  494. Borland C++, version 2.0. Since I have not included a Makefile
  495. with the code, perhaps for non-programmers I should add that
  496. to compile sg2ishi on a Unix system you must type the line:
  497.  
  498. cc sg2ishi.c -o sg2ishi
  499.  
  500. at the shell prompt. 
  501.  
  502. The program expects two command line arguements, one for the
  503. input file name and one for the output file name. So to run
  504. the program you type:
  505.  
  506. sg2ishi mgt_file manyface.go
  507.  
  508. Note that Many Faces of Go needs the suffix .go so that it can
  509. recognize the file as a game file.
  510. SHAR_EOF
  511. fi
  512. if test -f 'sample01.mgt'
  513. then
  514.     echo shar: "will not over-write existing file 'sample01.mgt'"
  515. else
  516. cat << \SHAR_EOF > 'sample01.mgt'
  517. (
  518. ;
  519. GaMe[1]
  520. VieW[]
  521. SiZe[19]
  522. Comment[A figment of a test file for sg2ishi
  523. to test some of the features supported mgt's notation
  524. note: this is a nonsense game...]
  525. REsult[with no komi, black wins by 4]
  526. DaTe[December 8, 1992]
  527. PlayerBlack[John Unger]
  528. PlayerWhite[Honinbo]
  529. PlaCe[Hamilton, VA]
  530. USer[Peter Cottontail, 4-dan]
  531. ;
  532. AddBlack[jq][kr][lo][lp][lq][mo][no][oo][pn][pp][qp][qr][rr]
  533. AddWhite[lr][mp][mr][np][op][or][pq][pr][qs]
  534. ;
  535. Mark[jq][kr][lo][lp][lq][mo][no][oo][pn][pp][qp][qr][rr]
  536. ;
  537. Letter[lr][mp][mr][np][op][or][pq][pr][qs]
  538. ;
  539. Black[pd]
  540. ;
  541. White[cp]
  542. ;
  543. Black[dp]
  544. ;
  545. White[dc]
  546. ;
  547. Black[od]
  548. ;
  549. White[po]
  550. ;
  551. Black[ld]
  552. ;
  553. White[pl]
  554. ;
  555. Black[de]
  556. ;
  557. White[cd]
  558. ;
  559. Black[ed]
  560. ;
  561. White[ce]
  562. ;
  563. Black[cf]
  564. ;
  565. White[ec]
  566. ;
  567. Black[ci]
  568. ;
  569. White[fd]
  570. ;
  571. Black[fe]
  572. ;
  573. White[gd]
  574. ;
  575. Black[ph]
  576. ;
  577. White[qi]
  578. ;
  579. Black[nc]
  580. ;
  581. White[cn]
  582. )
  583. SHAR_EOF
  584. fi
  585. exit 0
  586. #  end of shell archive
  587.  
  588.