home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / Games / NeXTGo / Source / GoApp.m < prev    next >
Encoding:
Text File  |  1977-12-29  |  5.7 KB  |  242 lines

  1. /*
  2.                 GNU GO - the game of Go (Wei-Chi)
  3.                 Version 1.1   last revised 3-1-89
  4.            Copyright (C) Free Software Foundation, Inc.
  5.                       written by Man L. Li
  6.                       modified by Wayne Iba
  7.                     documented by Bob Webber
  8.                     NeXT version by John Neil
  9. */
  10. /*
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation - version 1.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License in file COPYING for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. Please report any bug/fix, modification, suggestion to
  25.  
  26. mail address:   Man L. Li
  27.                 Dept. of Computer Science
  28.                 University of Houston
  29.                 4800 Calhoun Road
  30.                 Houston, TX 77004
  31.  
  32. e-mail address: manli@cs.uh.edu         (Internet)
  33.                 coscgbn@uhvax1.bitnet   (BITNET)
  34.                 70070,404               (CompuServe)
  35.  
  36. For the NeXT version, please report any bug/fix, modification, suggestion to
  37.  
  38. mail address:   John Neil
  39.                 Mathematics Department
  40.                 Portland State University
  41.                 PO Box 751
  42.                 Portland, OR  97207
  43.  
  44. e-mail address: neil@math.mth.pdx.edu  (Internet)
  45.                 neil@psuorvm.bitnet    (BITNET)
  46. */
  47.  
  48. /* Generated by Interface Builder */
  49.  
  50. #import <appkit/appkit.h>
  51. #import <appkit/PrintInfo.h>
  52.  
  53. #include <string.h>
  54. #import "GoApp.h"
  55. #import "Board.h"
  56.  
  57. int saveNeXTGoFile(const char *fileName)
  58. {
  59.   FILE *NGoFile;
  60.   int i, j;
  61.  
  62.   if ((NGoFile = fopen(fileName, "w")) == NULL)
  63.     return 1;
  64.  
  65.   fprintf(NGoFile, "%d\n", handicap);
  66.   fprintf(NGoFile, "%d\n", whiteSide);
  67.   fprintf(NGoFile, "%d\n", blackSide);
  68.   fprintf(NGoFile, "%d\n", MAXX);
  69.   fprintf(NGoFile, "%d\n", MAXY);
  70.   fprintf(NGoFile, "%d\n", currentStone);
  71.   fprintf(NGoFile, "%d\n", opposingStone);
  72.   fprintf(NGoFile, "%d\n", blackCaptured);
  73.   fprintf(NGoFile, "%d\n", whiteCaptured);
  74.   fprintf(NGoFile, "%d\n", blackCapturedKoI);
  75.   fprintf(NGoFile, "%d\n", blackCapturedKoJ);
  76.   fprintf(NGoFile, "%d\n", whiteCapturedKoI);
  77.   fprintf(NGoFile, "%d\n", whiteCapturedKoJ);
  78.   fprintf(NGoFile, "%d\n", bothSides);
  79.   fprintf(NGoFile, "%d\n", neitherSide);
  80.   fprintf(NGoFile, "%d\n", blackPassed);
  81.   fprintf(NGoFile, "%d\n", whitePassed);
  82.   for (i = 0; i < 9; i++)
  83.     fprintf(NGoFile, "%d\n", opn[i]);
  84.   for (i = 0; i < MAXX; i++)
  85.     for (j = 0; j < MAXY; j++)
  86.       fprintf(NGoFile, "%d\n", p[i][j]);
  87.       
  88.   fclose(NGoFile);
  89.  
  90.   return 0;
  91. }
  92.  
  93. @implementation GoApp
  94.  
  95. - appDidInit:app 
  96. {
  97.     [self SetPreferences:self];
  98.     [self NewGame:self];
  99.     [[[NXApp printInfo] setHorizCentered:YES] setVertCentered:YES];
  100.  
  101.     openReq = [OpenPanel new];
  102.     saveReq = [SavePanel new];
  103.     
  104.     return self;
  105. }
  106.  
  107. - showError: (const char *)errorMessage
  108. {
  109.   NXRunAlertPanel(NULL, errorMessage,"OK", NULL, NULL);
  110.   return self;
  111. }
  112.  
  113. - UserPass:sender
  114. {
  115.     [MainGoView passMove];
  116.  
  117.     return self;
  118. }
  119.  
  120. - stopGame:sender
  121. {
  122.     [MainGoView stop:self];
  123.     
  124.     return self;
  125. }
  126.  
  127. - startGame:sender
  128. {
  129.     [MainGoView go:self];
  130.     
  131.     return self;
  132. }
  133.  
  134. - NewGame:sender
  135. {        
  136.     [MainGoView startNewGame];
  137.     [MainGoView display];
  138.         
  139.     return self;
  140. }
  141.  
  142. - SetPreferences:sender
  143. {
  144.     handicap = [handicapSlider intValue];
  145.     MAXX = MAXY = [sizeSlider intValue];
  146.     blackSide = [BlackPlayer selectedRow];
  147.     whiteSide = [WhitePlayer selectedRow];
  148.     
  149.     neitherSide = 0;
  150.     bothSides = 0;
  151.     
  152.     if ((blackSide == 0) && (whiteSide == 0))
  153.       neitherSide++;
  154.     if ((blackSide == 1) && (whiteSide == 1))
  155.       bothSides++;
  156.     
  157.     [self NewGame:self];
  158.     
  159.     [prefPanel close];
  160.     
  161.     return self;
  162. }
  163.  
  164. -openNeXTGoFileRequest:sender
  165. {
  166.   FILE *NGoFile;
  167.   int i, j, t;
  168.   const char *fileName;
  169.   const char *const types[2] = {"nextgo", NULL};
  170.  
  171.   if ([openReq runModalForTypes:types] && (fileName = [openReq filename])) {
  172.  
  173.     if ((NGoFile = fopen(fileName, "r")) == NULL)
  174.       return self;
  175.  
  176.     fscanf(NGoFile, "%d", &handicap);
  177.     fscanf(NGoFile, "%d", &whiteSide);
  178.     fscanf(NGoFile, "%d", &blackSide);
  179.     fscanf(NGoFile, "%d", &MAXX);
  180.     fscanf(NGoFile, "%d", &MAXY);
  181.     [MainGoView startNewGame];
  182.     fscanf(NGoFile, "%d", &opposingStone);
  183.     fscanf(NGoFile, "%d", ¤tStone);
  184.     fscanf(NGoFile, "%d", &blackCaptured);
  185.     fscanf(NGoFile, "%d", &whiteCaptured);
  186.     fscanf(NGoFile, "%d", &blackCapturedKoI);
  187.     fscanf(NGoFile, "%d", &blackCapturedKoJ);
  188.     fscanf(NGoFile, "%d", &whiteCapturedKoI);
  189.     fscanf(NGoFile, "%d", &whiteCapturedKoJ);
  190.     fscanf(NGoFile, "%d", &bothSides);
  191.     fscanf(NGoFile, "%d", &neitherSide);
  192.     fscanf(NGoFile, "%d", &blackPassed);
  193.     fscanf(NGoFile, "%d", &whitePassed);
  194.     for (i = 0; i < 9; i++)
  195.       {
  196.         fscanf(NGoFile, "%d", &t);
  197.     opn[i] = t;
  198.       }
  199.     for (i = 0; i < MAXX; i++)
  200.       for (j = 0; j < MAXY; j++)
  201.         {
  202.           fscanf(NGoFile, "%d", &t);
  203.       p[i][j] = t;
  204.     }
  205.     [MainGoView updateInfo];
  206.  
  207.     fclose(NGoFile);
  208.     
  209.     [MainGoView lockFocus];
  210.     [[MainGoView window] flushWindow];
  211.     [MainGoView display];
  212.     [MainGoView unlockFocus];    
  213.   } else
  214.     [self showError:"Error on Open Request"];
  215.  
  216.   NXPing();
  217.  
  218.   return self;
  219. }
  220.  
  221.  
  222. -saveNeXTGoFileRequest:sender
  223. {
  224.   const char *fileName;
  225.   const char *const types[2] = {"nextgo", NULL};
  226.  
  227.   [saveReq setRequiredFileType:types[0]];
  228.  
  229.   if (([saveReq runModalForDirectory:""
  230.       file:""]) && (fileName = [saveReq filename]))
  231.     {
  232.       saveNeXTGoFile(fileName);
  233.     }
  234.     
  235.   NXPing();
  236.   
  237.   return self;
  238. }
  239.  
  240. @end
  241.  
  242.