home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / Games / NeXTGo-3.0-MIS / Board.m < prev    next >
Encoding:
Text File  |  1997-11-18  |  50.1 KB  |  2,063 lines

  1. #include "comment.header"
  2.  
  3. /* $Id: Board.m,v 1.4 1997/11/04 16:49:20 ergo Exp $ */
  4.  
  5. /*
  6.  * $Log: Board.m,v $
  7.  * Revision 1.4  1997/11/04 16:49:20  ergo
  8.  * ported to OpenStep
  9.  *
  10.  * Revision 1.3  1997/07/06 19:37:55  ergo
  11.  * actual version
  12.  *
  13.  * Revision 1.5  1997/06/03 23:01:55  ergo
  14.  * *** empty log message ***
  15.  *
  16.  * Revision 1.4  1997/05/30 18:44:13  ergo
  17.  * Added an Inspector
  18.  *
  19.  * Revision 1.3  1997/05/04 18:56:50  ergo
  20.  * added time control for moves
  21.  *
  22.  */
  23.  
  24. #import "Board.h"
  25. #import "gnugo.h"
  26. #include "igs.h"
  27.  
  28. #import <libc.h>
  29. #import <math.h>
  30. #import <sys/time.h>
  31. #import <AppKit/psopsOpenStep.h>    // PSxxx functions
  32. #import <SoundKit/Sound.h>
  33. #import <AppKit/AppKit.h>
  34.  
  35. #define EMPTY        0
  36. #define WHITESTONE    1
  37. #define BLACKSTONE    2
  38. #define NEUTRAL_TERR    3
  39. #define WHITE_TERR    4
  40. #define BLACK_TERR    5
  41. #define SPECIAL_CHAR    6
  42. #define KOMI            5.5
  43.  
  44. // The following values are the default sizes for the various pieces. 
  45.   
  46. #define RADIUS        14.5             // Stone radius
  47. #define STONEWIDTH    29.0            // Stone width
  48. #define STONEHEIGHT    29.0            // Stone height
  49.   
  50.   // SHADOWOFFSET defines the amount the shadow is offset from the piece. 
  51.   
  52. #define SHADOWOFFSET 2.0
  53.   
  54. #define BASEBOARDX 19.0
  55. #define BASEBOARDY 19.0
  56. #define WINDOWOFFSETX 12.0
  57. #define WINDOWOFFSETY 12.0
  58.   
  59. #define PSLine(a, b, x, y)    PSmoveto(a, b); PSlineto(x, y)
  60.  
  61. float stoneX, stoneY;
  62. int blackStones, whiteStones;
  63. char currentCharacter;
  64. unsigned char oldBoard[19][19];
  65.  
  66. void setStoneLoc(int x, int y)
  67. {
  68.   stoneX = ((19.0 - MAXX)/2.0)*STONEWIDTH + BASEBOARDX - RADIUS + (x*STONEWIDTH);
  69.   stoneY = BASEBOARDY - RADIUS + ((18 - y)*STONEHEIGHT) - ((19.0 - MAXY)/2.0)*STONEHEIGHT;
  70. }  
  71.  
  72. @implementation GoView
  73.   
  74. - initWithFrame:(NSRect)frm
  75. {
  76.   NSSize stoneSize;
  77.   
  78.   stoneSize.width = STONEWIDTH;
  79.   stoneSize.height = STONEHEIGHT;
  80.   
  81.   te = 0;
  82.   startZeit = 0;
  83.   
  84.   [super initWithFrame:frm];
  85.   
  86.   [self allocateGState];    // For faster lock/unlockFocus
  87.     
  88.   [(blackStone = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  89.   [blackStone addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawBlackStone:) delegate:self] autorelease]];
  90.   [blackStone setSize:stoneSize];
  91.   
  92.   [(whiteStone = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  93.   [whiteStone addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawWhiteStone:) delegate:self] autorelease]];
  94.   [whiteStone setSize:stoneSize];
  95.   
  96.   [(grayStone = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  97.   [grayStone addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawGrayStone:) delegate:self] autorelease]];
  98.   [grayStone setSize:stoneSize];
  99.   
  100.   [(upperLeft = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  101.   [upperLeft addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawUpperLeft:) delegate:self] autorelease]];
  102.   [upperLeft setSize:stoneSize];
  103.   
  104.   [(upperRight = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  105.   [upperRight addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawUpperRight:) delegate:self] autorelease]];
  106.   [upperRight setSize:stoneSize];
  107.   
  108.   [(lowerLeft = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  109.   [lowerLeft addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawLowerLeft:) delegate:self] autorelease]];
  110.   [lowerLeft setSize:stoneSize];
  111.   
  112.   [(lowerRight = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  113.   [lowerRight addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawLowerRight:) delegate:self] autorelease]];
  114.   [lowerRight setSize:stoneSize];
  115.   
  116.   [(midLeft = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  117.   [midLeft addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawMidLeft:) delegate:self] autorelease]];
  118.   [midLeft setSize:stoneSize];
  119.   
  120.   [(midRight = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  121.   [midRight addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawMidRight:) delegate:self] autorelease]];
  122.   [midRight setSize:stoneSize];
  123.   
  124.   [(midTop = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  125.   [midTop addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawMidTop:) delegate:self] autorelease]];
  126.   [midTop setSize:stoneSize];
  127.   
  128.   [(midBottom = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  129.   [midBottom addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawMidBottom:) delegate:self] autorelease]];
  130.   [midBottom setSize:stoneSize];
  131.   
  132.   [(innerSquare = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  133.   [innerSquare addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawInnerSquare:) delegate:self] autorelease]];
  134.   [innerSquare setSize:stoneSize];
  135.   
  136.   [(innerHandicap = [[NSImage allocWithZone:[self zone]] init]) setScalesWhenResized:NO];
  137.   [innerHandicap addRepresentation:[[[NSCustomImageRep alloc] initWithDrawSelector:@selector(drawInnerHandicap:) delegate:self] autorelease]];
  138.   [innerHandicap setSize:stoneSize];
  139.   
  140.   [self setBackgroundFile:[[[NSUserDefaults standardUserDefaults] objectForKey:@"BackGround"] cString] 
  141.  andRemember:NO];
  142.   
  143.   [self startNewGame];
  144.  
  145.   historyFont = [ [NSFont fontWithName:@"Helvetica" size:9.0] retain];
  146.   blackTerrFont = [ [NSFont fontWithName:@"Helvetica" size:25.0] retain];
  147.   whiteTerrFont = [ [NSFont fontWithName:@"Helvetica" size:22.5] retain];
  148. #warning
  149.   stoneClick = [Sound findSoundFor:@"Pop"];
  150.  
  151.   {
  152.       struct timeval tp;
  153.       struct timezone tzp;
  154.       gettimeofday(&tp, &tzp);
  155.       time = tp.tv_sec;
  156.   }
  157.   return self;
  158. }
  159.  
  160. // free simply gets rid of everything we created for MainGoView, including
  161.   // the instance of MainGoView itself. This is how nice objects clean up.
  162.   
  163. - (void)dealloc {
  164.     [backGround release];
  165.     { [super dealloc]; return; };
  166. }
  167.  
  168.  
  169.  
  170. // This methods allows changing the file used to paint the background of the
  171.   // playing field. Set fileName to NULL to revert to the default. Set
  172.   // remember to YES if you wish the write the value out in the defaults.
  173.   
  174. - setBackgroundFile:(const char *)fileName andRemember:(BOOL)remember {
  175.     [backGround release];
  176.     if (fileName) {
  177.         backGround = [NSImage alloc];
  178.         [backGround initWithContentsOfFile:[NSString stringWithCString:fileName]];
  179.         [backGround setSize:[self bounds].size];
  180.         if (remember) 
  181.               [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithCString:fileName] forKey:@"Background"];
  182.     } else {
  183.         backGround = [ [NSImage imageNamed:@"Background.tiff"] retain];
  184.         [backGround setSize:[self bounds].size];
  185.         if (remember)
  186.               [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Background"];
  187.     }
  188.     [backGround setBackgroundColor:[NSColor whiteColor]];
  189.     [backGround setScalesWhenResized:NO];
  190.     [self display];
  191.  
  192.     return self;   
  193. }
  194.  
  195. // The following two methods allow changing the background image from
  196. // menu items or buttons.
  197.   
  198. - changeBackground:sender {
  199.     
  200.     NSString *fileType1 = @"tiff",
  201.              *fileType2 = @"eps";
  202.  
  203.     id fileTypes = [NSArray arrayWithObjects:fileType1, fileType2, nil];
  204.  
  205.     if ([[NSOpenPanel new] runModalForTypes:fileTypes]) {
  206.         [self setBackgroundFile:[[[NSOpenPanel new] filename] cString] andRemember:YES];
  207.         [self display];
  208.     }
  209.   
  210.     return self;
  211. }
  212.  
  213. - revertBackground:sender
  214. {
  215.   [self setBackgroundFile:NULL andRemember:YES];
  216.   [self display];
  217.   return self;
  218. }
  219.  
  220. - resetButtons
  221. {
  222.   if (SmartGoGameFlag)
  223.     {
  224.       [startButton setEnabled:NO];
  225.       [stopButton setEnabled:NO];
  226.       [passButton setEnabled:NO];
  227.  
  228.       return self;
  229.     }
  230.     
  231.  if (bothSides)
  232.     [passButton setEnabled:NO];
  233.   else
  234.     [passButton setEnabled:YES];
  235.  
  236.   if (neitherSide)
  237.     {
  238.       [startButton setEnabled:NO];
  239.       [stopButton setEnabled:NO];
  240.     }
  241.   else
  242.     {
  243.       [startButton setEnabled:YES];
  244.       [stopButton setEnabled:YES];
  245.     }
  246.  
  247.   return self;
  248. }
  249.  
  250. // The following method will initialize all the variables for a new game.
  251.   
  252. - startNewGame {
  253.     
  254.     int i, j;
  255.   
  256.     gameRunning = NO;
  257.     finished = NO;
  258.     gameScored = NO;
  259.     resultsDisplayed = NO;
  260.     scoringGame = NO;
  261.     lastMove = 0;
  262.       blackCaptured = whiteCaptured = 0;
  263.     manualScoring = manScoreTemp;
  264.   
  265.     seed(&rd);
  266.   
  267.     for (i = 0; i < MAXX; i++)
  268.         for (j = 0; j < MAXY; j++)
  269.               oldBoard[i][j] = p[i][j] = hist[i][j] = 0;
  270.   
  271.       for (i = 0; i < 9; i++)
  272.         opn[i] = 1;
  273.       opn[4] = 0;
  274.  
  275.  
  276.       if (gameType == LOCAL) {
  277.             sethand(handicap);
  278.             currentStone = (handicap == 0)?BLACKSTONE:WHITESTONE;
  279.             opposingStone = (currentStone == BLACKSTONE)?WHITESTONE:BLACKSTONE;
  280.  
  281.             if (currentStone == BLACKSTONE)
  282.                 [gameMessage setStringValue:@"Black's Turn"];
  283.             else
  284.                 [gameMessage setStringValue:@"White's Turn"];
  285.  
  286.             [self resetButtons];
  287.  
  288.             if (((currentStone == BLACKSTONE) && blackSide) ||
  289.               ((currentStone == WHITESTONE) && whiteSide))
  290.                 [gameMessage2 setStringValue:@"Press START to begin..."];
  291.             else
  292.                 [gameMessage2 setStringValue:@"You move first..."];
  293.         }
  294.       else {
  295.             [gameMessage2 setStringValue:@"Internet Go Server"];
  296.             [gameMessage setStringValue:@""];
  297.             [self setblacksPrisoners:0];
  298.             [self setwhitesPrisoners:0];
  299.             [passButton setEnabled:YES];
  300.             [startButton setEnabled:NO];
  301.             [stopButton setEnabled:NO];
  302.             [self removeTE];
  303.             {
  304.                 struct timeval tp;
  305.             struct timezone tzp;
  306.             gettimeofday(&tp, &tzp);
  307.                 time = tp.tv_sec;
  308.             }    
  309.     }
  310.     
  311.       [ScoringWindow close];
  312.       PSWait();
  313.     
  314.       return self;
  315. }
  316.  
  317. // The stop method will pause a running game. The go method will start it up
  318.   // again.
  319.   
  320. - go:sender
  321. {
  322.   if (gameType == IGSGAME)
  323.     return self;
  324.     
  325.   if ((scoringGame) && (manualScoring))
  326.     {
  327.       int i, j;
  328.  
  329.       find_owner();
  330.       blackTerritory = 0;
  331.       whiteTerritory = 0;
  332.  
  333.       for (i = 0; i < MAXX; i++)
  334.     for (j = 0; j < MAXY; j++)
  335.       {
  336.         if (ownermat[i][j] == BLACKSTONE)
  337.           {
  338.         blackTerritory++;
  339.         p[i][j] = BLACK_TERR;
  340.           }
  341.         if (ownermat[i][j] == WHITESTONE)
  342.           {
  343.         whiteTerritory++;
  344.         p[i][j] = WHITE_TERR;
  345.           }
  346.         if (ownermat[i][j] == NEUTRAL_TERR)
  347.           {
  348.         p[i][j] = NEUTRAL_TERR;
  349.           }
  350.       }
  351.  
  352.       gameScored = YES;
  353.       [self displayScoringInfo];
  354.       PSWait();
  355.     }
  356.  
  357.   if ((gameRunning == 0) && (finished == 0)) {
  358.     gameRunning = YES;
  359.     [self step];
  360.   }
  361.   return 0;
  362. }
  363.  
  364. - (void)stop:(id)sender {
  365.     if (gameType == IGSGAME)
  366.         return;
  367.       
  368.     if (gameRunning) 
  369.         gameRunning = NO;
  370. }
  371.  
  372. - showLastMove:sender
  373. {
  374.   int i;
  375.  
  376.   if (SmartGoGameFlag)
  377.     return self;
  378.  
  379.   if (finished)
  380.     {
  381.       NSRunAlertPanel(@"NeXTGo", @"The game has concluded.  The last move was\n\
  382. the scoring.", @"OK", nil, nil);
  383.  
  384.       return self;
  385.     }
  386.  
  387.   if (lastMove == 0)
  388.     {
  389.       NSRunAlertPanel(@"NeXTGo", @"The game has not yet started.", @"OK", nil, nil);
  390.  
  391.       return self;
  392.     }
  393.  
  394.   for (i = 0; i < gameMoves[lastMove-1].numchanges; i++)
  395.     {
  396.       if (gameMoves[lastMove-1].changes[i].x < 0)
  397.     {
  398.       NSRunAlertPanel(@"NeXTGo", @"The last move was a pass.", @"OK", nil, nil);
  399.  
  400.       return self;
  401.     }
  402.     }
  403.     
  404.   [self lockFocus];
  405.   for (i = 0; i < gameMoves[lastMove-1].numchanges; i++)
  406.     {
  407.       if (gameMoves[lastMove-1].changes[i].added)
  408.     {
  409.       setStoneLoc(gameMoves[lastMove-1].changes[i].x,
  410.               gameMoves[lastMove-1].changes[i].y);
  411.       [self showGrayStone];
  412.     }
  413.     }
  414.   [self unlockFocus];
  415.   PSWait();
  416.   
  417.   [self lockFocus];
  418.   [[self window] flushWindow];
  419.   [self drawRect:[self bounds]];
  420.   [self display];
  421.   [self unlockFocus];
  422.   PSWait();
  423.   
  424.   return self;
  425. }
  426.  
  427. - undo
  428. {
  429.   int i, j, x, y;
  430.  
  431.   if (finished)
  432.     return self;
  433.   
  434.   if (lastMove == 1)
  435.     {
  436.       [self startNewGame];
  437.       [self display];
  438.  
  439.       return self;
  440.     }
  441.     
  442.   if (lastMove > 0)
  443.     {
  444.       lastMove--;
  445.  
  446.       for (i = 0; i < MAXX; i++)
  447.     for (j = 0; j < MAXY; j++)
  448.       p[i][j] = oldBoard[i][j];
  449.       blackCaptured = gameMoves[lastMove].blackCaptured;
  450.       whiteCaptured = gameMoves[lastMove].whiteCaptured;
  451.  
  452.       for (i = 0; i < gameMoves[lastMove-1].numchanges; i++)
  453.     {
  454.       x = gameMoves[lastMove-1].changes[i].x;
  455.       y = gameMoves[lastMove-1].changes[i].y;
  456.       if (gameMoves[lastMove-1].changes[i].added)
  457.         {
  458.           oldBoard[x][y] = EMPTY;
  459.         }
  460.       else
  461.         {
  462.           oldBoard[x][y] = gameMoves[lastMove-1].changes[i].color;
  463.         }
  464.     }
  465.       [self refreshIO];
  466.  
  467.       currentStone = opposingStone;
  468.       opposingStone = (currentStone == BLACKSTONE)?WHITESTONE:BLACKSTONE;
  469.  
  470.       if (gameType == LOCAL)
  471.     {
  472.       [gameMessage setStringValue:(currentStone == BLACKSTONE) ? @"Black's Turn" : @"White's Turn"];
  473.       
  474.       if ((bothSides) || (((currentStone == BLACKSTONE) && (blackSide)) ||
  475.                   ((currentStone == WHITESTONE) && (whiteSide))))
  476.         {
  477.           [gameMessage2 setStringValue:@"Press START to continue..."];
  478.           gameRunning = 0;
  479.         }
  480.       else
  481.         [gameMessage2 setStringValue:@"Your move..."];
  482.     }
  483.     }
  484.  
  485.   return self;
  486. }
  487.  
  488. - undoLastMove:sender {
  489.     if (SmartGoGameFlag)
  490.         return self;
  491.  
  492.     if (gameType == LOCAL) {
  493.         [self undo];
  494.     }
  495.     else {
  496.         sendstr("undo\n");
  497.     }
  498.  
  499.     return self;
  500. }
  501.  
  502. - toggleShowHistFlag:sender {
  503.     [self lockFocus];
  504.     [self display];
  505.     [self unlockFocus];
  506.  
  507.     return self;
  508. }
  509.  
  510. - toggleSound:sender {
  511.  
  512.   return self;
  513. }
  514.  
  515. - doClick {
  516.     if ([playSounds intValue])
  517.         [stoneClick play];
  518.     PSWait();
  519.  
  520.     return self;
  521. }
  522.  
  523. - toggleCoords:sender {
  524.     [self lockFocus];
  525.     [self display];
  526.     [self unlockFocus];
  527.  
  528.     return self;
  529. }
  530.  
  531. - (void)mouseDown:(NSEvent *)event 
  532. {
  533.       NSPoint pickedP;
  534.  
  535.     if (gameType == LOCAL) {
  536.           if ((((currentStone == BLACKSTONE) && blackSide) ||
  537.                ((currentStone == WHITESTONE) && whiteSide)) &&
  538.               (!scoringGame) && (!manualScoring))
  539.             return;
  540.  
  541.           if (SmartGoGameFlag)
  542.             return;
  543.     
  544.           if ((!gameRunning) && (!finished))
  545.             gameRunning = YES;
  546.   
  547.           if (!finished) {
  548.             int i, j, x, y;
  549.  
  550.             pickedP = [event locationInWindow];
  551.     
  552.             x = floor((pickedP.x - ((19.0 - MAXX)/2.0)*STONEWIDTH - BASEBOARDX     
  553.                                   - WINDOWOFFSETX + RADIUS)/STONEWIDTH);
  554.             y = 18 - floor((pickedP.y - BASEBOARDY - WINDOWOFFSETY + RADIUS +
  555.             ((19.0 - MAXY)/2.0)*STONEHEIGHT)/STONEHEIGHT);
  556.     
  557.             if (x < 0) x = 0;
  558.             if (x > MAXX - 1) x = MAXX - 1;
  559.             if (y < 0) y = 0;
  560.             if (y > MAXY - 1) y = MAXY - 1;
  561.     
  562.             if ((p[x][y] == 0) && (!suicide(x,y))) {
  563.                   for (i = 0; i < MAXX; i++)
  564.                     for (j = 0; j < MAXY; j++)
  565.                           oldBoard[i][j] = p[i][j];
  566.  
  567.                 p[x][y] = currentStone;
  568.                   if (currentStone == BLACKSTONE)
  569.                     blackPassed = 0;
  570.                   else
  571.                     whitePassed = 0;
  572.  
  573.                   setStoneLoc(x,y);
  574.       
  575.                   [self lockFocus];
  576.                   switch (p[x][y]) {
  577.                     case WHITESTONE: 
  578.                         [self showWhiteStone];
  579.                           break;
  580.                     case BLACKSTONE: 
  581.                         [self showBlackStone];
  582.                           break;
  583.                     default: 
  584.                         break;
  585.                 }
  586.                   [self unlockFocus];
  587.  
  588. // commented out because of double clicks                  [self doClick];
  589.  
  590.                   [self updateInfo];
  591.  
  592.                   [self addMoveToGameMoves: currentStone: x: y];
  593.  
  594.                   if ([showHistFlag intValue]) {
  595.                       NSRect tmpRect = {{floor(stoneX), floor(stoneY)},
  596.                             {floor(STONEWIDTH), floor(STONEHEIGHT)}};
  597.  
  598.                       [self lockFocus];
  599.                       [self drawRect:tmpRect];
  600.                       [self unlockFocus];
  601.                 }
  602.  
  603.                   if (!neitherSide)
  604.                     [self step];
  605.                                 [self setNeedsDisplay:YES];
  606.             } 
  607.         } 
  608.         else {
  609.             if ((scoringGame) && (manualScoring) && (!gameScored)) {
  610.             int x, y;
  611.  
  612.             pickedP = [event locationInWindow];
  613.     
  614.             x = floor((pickedP.x - ((19.0 - MAXX)/2.0)*STONEWIDTH - BASEBOARDX
  615.                - WINDOWOFFSETX + RADIUS)/STONEWIDTH);
  616.             y = 18 - floor((pickedP.y - BASEBOARDY - WINDOWOFFSETY + RADIUS +
  617.                 ((19.0 - MAXY)/2.0)*STONEHEIGHT)/STONEHEIGHT);
  618.     
  619.             if (x < 0) x = 0;
  620.             if (x > MAXX - 1) x = MAXX - 1;
  621.             if (y < 0) y = 0;
  622.             if (y > MAXY - 1) y = MAXY - 1;
  623.  
  624.             if (p[x][y] != EMPTY) {
  625.                     int k, l;
  626.  
  627.                     currentStone = p[x][y];
  628.  
  629.                     find_pattern_in_board(x, y);
  630.                     for (k = 0; k < MAXX; k++)
  631.                         for (l = 0; l < MAXY; l++)
  632.                             if (patternmat[k][l]) {
  633.                                 p[k][l] = EMPTY;
  634.                                 if (currentStone == BLACKSTONE)
  635.                                     blackCaptured++;
  636.                                 else
  637.                                     whiteCaptured++;
  638.                             }
  639.         
  640.                     [self setblacksPrisoners:blackCaptured];
  641.                     [self setwhitesPrisoners:whiteCaptured];
  642.  
  643.                     [self setNeedsDisplay:YES];
  644.                 }
  645.             }
  646.       }
  647.     }
  648.     else {
  649.         int x, y;
  650.           char s[50], n[50];
  651.           extern int observing, ingame;
  652.  
  653.           if (observing || (ingame == -1)) {
  654.             NSRunAlertPanel(@"IGS Error", @"You cannot make a move unless you are playing.", @"OK", nil, nil);                  return;
  655.     }
  656.  
  657.           pickedP = [event locationInWindow];
  658.     
  659.           x = floor((pickedP.x - ((19.0 - MAXX)/2.0)*STONEWIDTH - BASEBOARDX -
  660.              WINDOWOFFSETX + RADIUS)/STONEWIDTH);
  661.           y = 18 - floor((pickedP.y - BASEBOARDY - WINDOWOFFSETY + RADIUS +
  662.               ((19.0 - MAXY)/2.0)*STONEHEIGHT)/STONEHEIGHT);
  663.  
  664.           if (x < 0) x = 0;
  665.           if (x > MAXX - 1)
  666.             x = MAXX - 1;
  667.           if (y < 0) y = 0;
  668.           if (y > MAXY - 1)
  669.             y = MAXY - 1;
  670.  
  671.           s[0] = x + 'a';
  672.           if (x > 7)
  673.             s[0] = x + 'b';
  674.           s[1] = 0;
  675.           sprintf(n, "%d", MAXY-y);
  676.           strcat(s, n);
  677.     {
  678.             struct timeval tp;
  679.             struct timezone tzp;
  680.             gettimeofday(&tp, &tzp);
  681.             time = tp.tv_sec - time;
  682.         }
  683.  
  684.         sprintf(n, " %d", ingame);     
  685.           strcat(s, n);
  686.  
  687.         sprintf(n, " %ld", time);     
  688.           strcat(s, n);
  689.           strcat(s, "\n");
  690.           sendstr(s);
  691.     }
  692. }
  693.  
  694.  
  695. - passMove {
  696.     if (gameType == LOCAL) {
  697.         if (((currentStone == BLACKSTONE) && blackSide) ||
  698.             ((currentStone == WHITESTONE) && whiteSide)) {
  699.             return self;
  700.         }
  701.         if (currentStone == BLACKSTONE) {
  702.             blackPassed = 1;
  703.             if (AGAScoring) blackCaptured++;
  704.         }
  705.           else {
  706.             whitePassed = 1;
  707.             if (AGAScoring) whiteCaptured++;
  708.         }
  709.  
  710.           [self updateInfo];
  711.  
  712.           [self addMoveToGameMoves: currentStone: -1: -1];
  713.     
  714.           if ((!neitherSide) && (!finished))
  715.             [self step];
  716.     }
  717.     else {
  718.         sendstr("pass\n");
  719.     }
  720.     
  721.   return self;
  722. }
  723.  
  724. - refreshIO {
  725.     [self setblacksPrisoners:blackCaptured];
  726.     [self setwhitesPrisoners:whiteCaptured];
  727.  
  728.     [self lockFocus];
  729.     [[self window] flushWindow];
  730.     [self drawRect:[self bounds]];
  731.     [self display];
  732.     [self unlockFocus];
  733.  
  734.     PSWait();
  735.  
  736.     return self;
  737. }
  738.  
  739. - addMoveToGameMoves: (int)color: (int)x: (int)y {
  740.     int i, j, k, numchanges;
  741.  
  742.     numchanges = 0;
  743.     for (i = 0; i < MAXX; i++)
  744.         for (j = 0; j < MAXY; j++)
  745.             if (p[i][j] != oldBoard[i][j])
  746.                 numchanges++;
  747.     if (x < 0 || y < 0)
  748.         numchanges++;
  749.     gameMoves[lastMove].numchanges = numchanges;
  750.     gameMoves[lastMove].changes = (struct change *)
  751.         malloc((size_t)sizeof(struct change)*numchanges);
  752.     k = 0;
  753.     if (x < 0 || y < 0) {
  754.         gameMoves[lastMove].changes[0].added = NO;
  755.           gameMoves[lastMove].changes[0].x = x;
  756.         gameMoves[lastMove].changes[0].y = y;
  757.         gameMoves[lastMove].changes[0].color = color;
  758.           k++;
  759.     }
  760.     for (i = 0; i < MAXX; i++)
  761.         for (j = 0; j < MAXY; j++)
  762.             if (p[i][j] != oldBoard[i][j]) {
  763.                 gameMoves[lastMove].changes[k].x = i;
  764.                 gameMoves[lastMove].changes[k].y = j;
  765.                 if (p[i][j] != EMPTY) {
  766.                     gameMoves[lastMove].changes[k].added = YES;
  767.                     gameMoves[lastMove].changes[k].color = p[i][j];
  768.                 }
  769.                 else {
  770.                     gameMoves[lastMove].changes[k].added = NO;
  771.                     gameMoves[lastMove].changes[k].color = oldBoard[i][j];
  772.                 }
  773.                 k++;
  774.             }
  775.     gameMoves[lastMove].blackCaptured = blackCaptured;
  776.     gameMoves[lastMove].whiteCaptured = whiteCaptured;
  777.  
  778.     lastMove++;
  779.  
  780.     if (x >= 0)
  781.         hist[x][y] = lastMove;
  782.  
  783.     return self;
  784. }
  785.  
  786. - makeMove: (int)color: (int)x: (int)y {
  787.     int oldwhitesPrisoners, oldblacksPrisoners, i, j;
  788.  
  789.     currentStone = color;
  790.     opposingStone = (currentStone == BLACKSTONE)?WHITESTONE:BLACKSTONE;
  791.  
  792.     if ((x >= 0) && (y >= 0)) {
  793.         for (i = 0; i < MAXX; i++)
  794.             for (j = 0; j < MAXX; j++)
  795.                   oldBoard[i][j] = p[i][j];
  796.  
  797.           p[x][y] = color;
  798.       
  799.           setStoneLoc(x,y);
  800.  
  801.         [self lockFocus];
  802.           switch (p[x][y]) {
  803.             case WHITESTONE: 
  804.                 [self showWhiteStone];
  805.                   break;
  806.             case BLACKSTONE: 
  807.                 [self showBlackStone];
  808.                   break;
  809.             default: 
  810.                 break;
  811.         }
  812.           [self unlockFocus];
  813.  
  814.           oldblacksPrisoners = blackCaptured;
  815.           oldwhitesPrisoners = whiteCaptured;
  816.       
  817.           examboard(opposingStone);
  818.   
  819.           [self setblacksPrisoners:blackCaptured];
  820.           [self setwhitesPrisoners:whiteCaptured];
  821.   
  822.           if (((oldblacksPrisoners != blackCaptured) ||
  823.            (oldwhitesPrisoners != whiteCaptured))) {
  824.             [self lockFocus];
  825.               [self display];
  826.               [self unlockFocus];
  827.         
  828.         }
  829.  
  830.           if ([showHistFlag intValue]) {
  831.               NSRect tmpRect = {{floor(stoneX), floor(stoneY)},
  832.                       {floor(STONEWIDTH), floor(STONEHEIGHT)}};
  833.  
  834.               [self lockFocus];
  835.               [self drawRect:tmpRect];
  836.               [self display];
  837.             [self unlockFocus];
  838.         }
  839.         
  840.         if (blackPassed) {
  841.             blackPassed = 0;
  842.             [gameMessage2 setStringValue:@""];
  843.         }
  844.         
  845.         if (whitePassed) {
  846.             whitePassed = 0;
  847.             [gameMessage2 setStringValue:@""];
  848.         }
  849.     }
  850.     [self addMoveToGameMoves: color: x: y];
  851.     
  852.     [gameMessage setStringValue:(opposingStone == BLACKSTONE) ? @"Black's Turn" : @"White's Turn"];
  853.     
  854.     if ((-1 == x) && (-1 == y)) {        /* opponent has passed */
  855.         if (currentStone == BLACKSTONE) {
  856.             blackPassed = 1;
  857.             [gameMessage2 setStringValue:@"Black has passed."];
  858.         }
  859.         else {
  860.              whitePassed = 1;
  861.             [gameMessage2 setStringValue:@"White has passed."];
  862.         }
  863.     }
  864.     {
  865.     struct timeval tp;
  866.         struct timezone tzp;
  867.         gettimeofday(&tp, &tzp);
  868.         time = tp.tv_sec;
  869.     }    
  870.     [self doClick];
  871.     [self setNeedsDisplay:YES];
  872.     return self;
  873. }
  874.  
  875. - makeMoveSilent: (int)color: (int)x: (int)y
  876. {
  877.   int i, j;
  878.       
  879.   if ((x >= 0) && (y >= 0))
  880.     {
  881.       for (i = 0; i < MAXX; i++)
  882.     for (j = 0; j < MAXY; j++)
  883.       oldBoard[i][j] = p[i][j];
  884.  
  885.       p[x][y] = color;
  886.  
  887.       currentStone = color;
  888.       opposingStone = (currentStone == BLACKSTONE)?WHITESTONE:BLACKSTONE;
  889.  
  890.       examboard(opposingStone);
  891.  
  892.       [self addMoveToGameMoves: color: x: y];
  893.     }
  894.   
  895.   return self;
  896. }
  897.  
  898. - setTimeAndByo: (int)btime: (int)bbyo: (int)wtime: (int)wbyo {
  899. #ifdef DEBUG
  900.     printf("setTimeAndByo: BlackTime = %d, BlackByo = %d, WhitTime = %d, Whitebyo = %d\n", btime, bbyo, wtime, wbyo);
  901. #endif
  902.     if (bTime != -1) {
  903.         if (bTime < 0)
  904.             bTime = 0;
  905.         if (wTime < 0)
  906.             wTime = 0;
  907.         [self removeTE];
  908.         startZeit = 0;
  909.         if (currentStone == WHITESTONE) {        /* Black moved */
  910.             ts.byo = bbyo;
  911.             ts.time = btime;
  912.             ts.timeToHandle = blackTime;
  913.         }
  914.         else {                        /* White moved */
  915.             ts.byo = wbyo;
  916.             ts.time = wtime;
  917.             ts.timeToHandle = whiteTime;
  918.     }
  919.         te = [[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(TEHandler:) userInfo:self repeats:YES] retain];
  920.         bTime = btime;
  921.         bByo = bbyo;
  922.         wTime = wtime;
  923.         wByo = wbyo;
  924.     }
  925.     return self;
  926. }
  927.  
  928. - dispTime
  929. {
  930.     char bltime[25], whtime[25];
  931.  
  932.     sprintf(bltime, "%d:%02d", bTime / 60, bTime % 60);
  933.     if (bByo != -1)
  934.         sprintf(bltime, "%s, %d", bltime, bByo);
  935.     sprintf(whtime, "%d:%02d", wTime / 60, wTime % 60);
  936.     if (wByo != -1)
  937.         sprintf(whtime, "%s, %d", whtime, wByo);
  938.     [blackTime setStringValue:[NSString stringWithCString:bltime]];
  939.     [blackTime display];
  940.     [whiteTime setStringValue:[NSString stringWithCString:whtime]];
  941.     [whiteTime display];
  942.     return self;
  943. }
  944.  
  945. - setGameNumber: (int)n
  946. {
  947.   [IGSGameNumber setIntValue:n];
  948.   [IGSGameNumber display];
  949.   
  950.   return self;
  951. }
  952.  
  953. - updateTitle {
  954.     id buf = [ NSString stringWithString:[IGSBlackPlayer stringValue]];
  955.     buf = [buf stringByAppendingString:[NSString stringWithString:@" - "] ];
  956.     buf = [buf stringByAppendingString:[IGSWhitePlayer stringValue]];
  957.     [[self window] setTitle:buf];
  958.     
  959.     return self;    
  960. }
  961.  
  962. - setWhiteName: (char *)wname {
  963.     
  964.     [IGSWhitePlayer setStringValue:[NSString stringWithCString:wname]];
  965.     [IGSWhitePlayer display];
  966.     [self updateTitle];
  967.  
  968.     return self;
  969. }
  970.  
  971. - setBlackName: (char *)bname {
  972.  
  973.     [IGSBlackPlayer setStringValue:[NSString stringWithCString:bname]];
  974.     [IGSBlackPlayer display];
  975.     [self updateTitle];
  976.  
  977.     return self;
  978. }
  979.  
  980. - setIGSHandicap: (int)h
  981. {
  982.   [IGShandicap setIntValue:h];
  983.   [IGShandicap display];
  984.  
  985.   return self;
  986. }
  987.  
  988. - setIGSKomi: (char *)k
  989. {
  990.   [IGSkomi setStringValue:[NSString stringWithCString:k]];
  991.   [IGSkomi display];
  992.  
  993.   return self;
  994. }
  995.  
  996. - setByoTime: (int)aByoTime {
  997.     ByoTime = aByoTime;
  998.     
  999.     return self;
  1000. }
  1001.  
  1002. - (int) ByoTime {
  1003.     return ByoTime;
  1004. }
  1005.  
  1006. - updateInfo {
  1007.     int oldblacksPrisoners, oldwhitesPrisoners, i, j;
  1008.  
  1009.     if (finished && gameScored && resultsDisplayed) {
  1010.         [startButton setEnabled:NO];
  1011.         [stopButton setEnabled:NO];
  1012.         [passButton setEnabled:NO];
  1013.         return self;
  1014.     }
  1015.  
  1016.     oldblacksPrisoners = blackCaptured;
  1017.     oldwhitesPrisoners = whiteCaptured;
  1018.  
  1019.     examboard(opposingStone);
  1020.  
  1021.     if (currentStone == BLACKSTONE) {
  1022.         opposingStone = BLACKSTONE;
  1023.         currentStone = WHITESTONE;
  1024.         [gameMessage setStringValue:@"White's Turn"];
  1025.     }
  1026.     else {
  1027.         opposingStone = WHITESTONE;
  1028.         currentStone = BLACKSTONE;
  1029.         [gameMessage setStringValue:@"Black's Turn"];
  1030.     }
  1031.  
  1032.     [self setblacksPrisoners:blackCaptured];
  1033.     [self setwhitesPrisoners:whiteCaptured];
  1034.  
  1035.     if (((oldblacksPrisoners != blackCaptured) ||
  1036.          (oldwhitesPrisoners != whiteCaptured))) {
  1037.         [self lockFocus];
  1038.           for (i = 0; i < MAXX; i++)
  1039.             for (j = 0; j < MAXX; j++)
  1040.                 if ((oldBoard[i][j] != EMPTY) && (p[i][j] == EMPTY)) {
  1041.                     setStoneLoc(i, j);
  1042.                     [self eraseStone];
  1043.                     [self showBackgroundPiece: i: j];
  1044.                 }
  1045.         [self unlockFocus];
  1046.     }
  1047.  
  1048.     if ([showHistFlag intValue]) {
  1049.         NSRect tmpRect = {{floor(stoneX), floor(stoneY)},
  1050.               {floor(STONEWIDTH), floor(STONEHEIGHT)}};
  1051.  
  1052.         [self lockFocus];
  1053.         [self drawRect:tmpRect];
  1054.         [self display];
  1055.         [self unlockFocus];
  1056.     }
  1057.  
  1058.     if ((blackPassed) && (opposingStone == BLACKSTONE))
  1059.         [gameMessage2 setStringValue:@"Black has passed."];
  1060.  
  1061.     if ((whitePassed) && (opposingStone == WHITESTONE))
  1062.         [gameMessage2 setStringValue:@"White has passed."];
  1063.  
  1064.     if ((!blackPassed) && (!whitePassed))
  1065.         [gameMessage2 setStringValue:@""];
  1066.  
  1067.     if ((blackPassed) && (whitePassed) && (!manualScoring) && (!gameScored)) {
  1068.         [self lockFocus];
  1069.         [[self window] flushWindow];
  1070.         [gameMessage setStringValue:@"Scoring Game, Please Wait"];
  1071.         [gameMessage2 setStringValue:@"Removing Dead Groups..."];
  1072.         [self display];
  1073.         [self unlockFocus];
  1074.         finished = 1;
  1075.         score_game();
  1076. //      [self scoreGame];
  1077.         manualScoring = 1;
  1078.     }
  1079.     
  1080.     if ((blackPassed) && (whitePassed) && (manualScoring) && (!gameScored)) {
  1081.         [self lockFocus];
  1082.         [[self window] flushWindow];
  1083.         [gameMessage setStringValue:@"Please remove dead groups"];
  1084.         [gameMessage2 setStringValue:@"When finished, press Start..."];
  1085.         [self display];
  1086.         [self unlockFocus];
  1087.         [passButton setEnabled:NO];
  1088.         [stopButton setEnabled:NO];
  1089.         finished = 1;
  1090.         scoringGame = YES;
  1091.     }
  1092.  
  1093.   return self;
  1094.  
  1095. }
  1096.  
  1097. - displayScoringInfo
  1098. {
  1099.   char s[35];
  1100.   int i, j;
  1101.  
  1102.   if (gameScored)
  1103.     {
  1104.       resultsDisplayed = YES;
  1105.       if (typeOfScoring == 0)
  1106.     {
  1107.       black_Score = (float)blackTerritory - (float)blackCaptured;
  1108.       white_Score = (float)whiteTerritory - (float)whiteCaptured;
  1109.       white_Score += (handicap == 0)?KOMI:0.5;
  1110.       [TypeOfScoring setStringValue:@"Japanese Scoring Method"];
  1111.       [BlackTerrString setStringValue:@"Territory"];
  1112.       [WhiteTerrString setStringValue:@"Territory"];
  1113.       [BlackTerrValue setIntValue:blackTerritory];
  1114.       [WhiteTerrValue setIntValue:whiteTerritory];
  1115.       [BlackPrisonString setStringValue:@"Prisoners"];
  1116.       [WhitePrisonString setStringValue:@"Prisoners"];
  1117.       [BlackPrisonValue setIntValue:blackCaptured];
  1118.       [WhitePrisonValue setIntValue:whiteCaptured];
  1119.       [BlackTotalValue setFloatValue:black_Score];
  1120.       [WhiteTotalValue setFloatValue:white_Score];
  1121.     }
  1122.       else
  1123.     {
  1124.       blackStones = whiteStones = 0;
  1125.       for (i = 0; i < MAXX; i++)
  1126.         for (j = 0; j < MAXY; j++)
  1127.           {
  1128.         if (p[i][j] == BLACKSTONE) blackStones++;
  1129.         if (p[i][j] == WHITESTONE) whiteStones++;
  1130.           }
  1131.       black_Score = (float)blackTerritory + (float)blackStones;
  1132.       white_Score = (float)whiteTerritory + (float)whiteStones;
  1133.       white_Score += (handicap == 0)?KOMI:0.5;
  1134.       [TypeOfScoring setStringValue:@"Chinese Scoring Method"];
  1135.       [BlackTerrString setStringValue:@"Territory"];
  1136.       [WhiteTerrString setStringValue:@"Territory"];
  1137.       [BlackTerrValue setIntValue:blackTerritory];
  1138.       [WhiteTerrValue setIntValue:whiteTerritory];
  1139.       [BlackPrisonString setStringValue:@"Stones"];
  1140.       [WhitePrisonString setStringValue:@"Stones"];
  1141.       [BlackPrisonValue setIntValue:blackStones];
  1142.       [WhitePrisonValue setIntValue:whiteStones];
  1143.       [BlackTotalValue setFloatValue:black_Score];
  1144.       [WhiteTotalValue setFloatValue:white_Score];
  1145.     }
  1146.       if (black_Score > white_Score)
  1147.     sprintf(s, "Result:  Black wins by %3.1f points.", black_Score - white_Score);
  1148.       if (white_Score > black_Score)
  1149.     sprintf(s, "Result:  White wins by %3.1f points.", white_Score - black_Score);
  1150.       if (black_Score == white_Score)
  1151.     sprintf(s, "Result:  The game was a tie.");
  1152.       [KomiValue setFloatValue:((handicap == 0)?KOMI:0.5)];
  1153.       [GameResult setStringValue:[NSString stringWithCString:s]];
  1154.       [ScoringWindow makeKeyAndOrderFront:self];
  1155.       [gameMessage setStringValue:@"Game Over"];
  1156.       [self lockFocus];
  1157.       [self display];
  1158.       [self unlockFocus];
  1159.     }
  1160.   
  1161.   return self;
  1162. }
  1163.  
  1164. - scoreGame
  1165. {
  1166.   int i, j, k, l, changes = 1, num_in_pattern;
  1167.  
  1168.   for (i = 0; i < MAXX; i++)
  1169.     for (j = 0; j < MAXY; j++)
  1170.       scoringmat[i][j] = 0;
  1171.       
  1172.   while (changes)
  1173.     {
  1174.       changes = 0;
  1175.       find_owner();
  1176.  
  1177.       for (i = 0; i < MAXX; i++)
  1178.     for (j = 0; j < MAXY; j++)
  1179.       if ((p[i][j] != 0) && (scoringmat[i][j] == 0))
  1180.         {
  1181.           if (surrounds_territory(i, j))
  1182.         {
  1183.           find_pattern_in_board(i, j);
  1184.  
  1185.           for (k = 0; k < MAXX; k++)
  1186.             for (l = 0; l < MAXY; l++)
  1187.               if (patternmat[k][l])
  1188.             scoringmat[k][l] = p[k][l];
  1189.         }
  1190.           else
  1191.         {
  1192.           find_pattern_in_board(i, j);
  1193.           set_temp_to_p();
  1194.           num_in_pattern = 0;
  1195.  
  1196.           for (k = 0; k < MAXX; k++)
  1197.             for (l = 0; l < MAXY; l++)
  1198.               if (patternmat[k][l])
  1199.             {
  1200.               p[k][l] = EMPTY;
  1201.               [self flashStone:k:l];
  1202.               num_in_pattern++;
  1203.             }
  1204.  
  1205.           find_owner();
  1206.  
  1207.           if ((ownermat[i][j] != NEUTRAL_TERR) &&
  1208.               (ownermat[i][j] != tempmat[i][j]))
  1209.             {
  1210.               if (tempmat[i][j] == BLACKSTONE)
  1211.             blackCaptured += num_in_pattern;
  1212.               else
  1213.             whiteCaptured += num_in_pattern;
  1214.               changes++;
  1215.               [self lockFocus];
  1216.               [self display];
  1217.               [self unlockFocus];
  1218.             }
  1219.           else
  1220.             {
  1221.               set_p_to_temp();
  1222.               find_owner();
  1223.             }
  1224.         }
  1225.         }
  1226.     }
  1227.  
  1228. /*  blackTerritory = 0;
  1229.   whiteTerritory = 0;
  1230.  
  1231.   [self lockFocus];
  1232.   for (i = 0; i < MAXX; i++)
  1233.     for (j = 0; j < MAXY; j++)
  1234.       {
  1235.     if (ownermat[i][j] == BLACKSTONE)
  1236.       {
  1237.         blackTerritory++;
  1238.         p[i][j] = BLACK_TERR;
  1239.       }
  1240.     if (ownermat[i][j] == WHITESTONE)
  1241.       {
  1242.         whiteTerritory++;
  1243.         p[i][j] = WHITE_TERR;
  1244.       }
  1245.     if (ownermat[i][j] == NEUTRAL_TERR)
  1246.       {
  1247.         [self flashStone:i:j];
  1248.         p[i][j] = NEUTRAL_TERR;
  1249.       }
  1250.       }
  1251.   [self unlockFocus];   */
  1252.  
  1253.   return self;
  1254. }
  1255.  
  1256. // The following methods draw the pieces.
  1257.   
  1258.   - drawBlackStone:imageRep 
  1259. {
  1260.   //    PSscale (1.0, 1.0);
  1261.   
  1262.   // First draw the shadow under the stone.
  1263.     
  1264. //    PSarc (RADIUS+SHADOWOFFSET/2, RADIUS-SHADOWOFFSET/2, 
  1265. //       RADIUS-SHADOWOFFSET, 0.0, 360.0);
  1266. //  PSsetgray (NX_DKGRAY);
  1267. //  if (NXDrawingStatus == NX_DRAWING) {
  1268. //    PSsetalpha (0.666);
  1269. //  }
  1270. //  PSfill ();
  1271.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1272.     PSsetalpha (1.0);
  1273.   }
  1274.   
  1275.   // Draw the stone.
  1276.     
  1277.     PSarc (RADIUS, RADIUS, 
  1278.        RADIUS, 0.0, 360.0);
  1279.   PSsetgray (NSBlack);
  1280.   PSfill ();
  1281.   
  1282.   // And the lighter & darker spots on the stone...
  1283.     
  1284.     PSarcn (RADIUS, RADIUS, 
  1285.         RADIUS-SHADOWOFFSET-3.0, 170.0, 100.0);
  1286.   PSarc (RADIUS, RADIUS, 
  1287.      RADIUS-SHADOWOFFSET-2.0, 100.0, 170.0);
  1288.   PSsetgray (NSDarkGray);
  1289.   PSfill ();
  1290.   PSarcn (RADIUS, RADIUS, 
  1291.       RADIUS-SHADOWOFFSET-3.0, 350.0, 280.0);
  1292.   PSarc (RADIUS, RADIUS, 
  1293.      RADIUS-SHADOWOFFSET-2.0, 280.0, 350.0);
  1294.   PSsetgray (NSLightGray);
  1295.   PSfill ();
  1296.   
  1297.   return self;
  1298. }
  1299.  
  1300. - drawWhiteStone:imageRep 
  1301. {
  1302.   //    PSscale (1.0, 1.0);
  1303.   
  1304.   // First draw the shadow under the stone.
  1305.     
  1306. //    PSarc (RADIUS+SHADOWOFFSET/2, RADIUS-SHADOWOFFSET/2, 
  1307. //       RADIUS-SHADOWOFFSET, 0.0, 360.0);
  1308. //  PSsetgray (NX_DKGRAY);
  1309. //  if (NXDrawingStatus == NX_DRAWING) {
  1310. //    PSsetalpha (0.666);
  1311. //  }
  1312. //  PSfill ();
  1313.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1314.     PSsetalpha (1.0);
  1315.   }
  1316.   
  1317.   // Draw the stone.
  1318.     
  1319.     PSarc (RADIUS, RADIUS, 
  1320.        RADIUS, 0.0, 360.0);
  1321.   PSsetgray (NSWhite);
  1322.   PSfill ();
  1323.   
  1324.   // And the lighter & darker spots on the stone...
  1325.     
  1326.     PSarcn (RADIUS, RADIUS, 
  1327.         RADIUS-SHADOWOFFSET-3.0, 170.0, 100.0);
  1328.   PSarc (RADIUS, RADIUS, 
  1329.      RADIUS-SHADOWOFFSET-2.0, 100.0, 170.0);
  1330.   PSsetgray (NSLightGray);
  1331.   PSfill ();
  1332.   PSarcn (RADIUS, RADIUS, 
  1333.       RADIUS-SHADOWOFFSET-3.0, 350.0, 280.0);
  1334.   PSarc (RADIUS, RADIUS, 
  1335.      RADIUS-SHADOWOFFSET-2.0, 280.0, 350.0);
  1336.   PSsetgray (NSDarkGray);
  1337.   PSfill ();
  1338.   
  1339.   return self;
  1340. }
  1341.  
  1342. - drawGrayStone:imageRep 
  1343. {
  1344.   //    PSscale (1.0, 1.0);
  1345.   
  1346.   // First draw the shadow under the stone.
  1347.     
  1348. //    PSarc (RADIUS+SHADOWOFFSET/2, RADIUS-SHADOWOFFSET/2, 
  1349. //       RADIUS-SHADOWOFFSET, 0.0, 360.0);
  1350. //  PSsetgray (NX_DKGRAY);
  1351. //  if (NXDrawingStatus == NX_DRAWING) {
  1352. //    PSsetalpha (0.666);
  1353. //  }
  1354. //  PSfill ();
  1355.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1356.     PSsetalpha (1.0);
  1357.   }
  1358.   
  1359.   // Draw the stone.
  1360.     
  1361. //    PSarc (RADIUS-SHADOWOFFSET/2, RADIUS+SHADOWOFFSET/2, 
  1362. //       RADIUS-SHADOWOFFSET, 0.0, 360.0);
  1363.     PSarc (RADIUS, RADIUS, 
  1364.        RADIUS, 0.0, 360.0);
  1365.   PSsetgray (NSDarkGray);
  1366.   PSfill ();
  1367.   
  1368.   // And the lighter & darker spots on the stone...
  1369.     
  1370.     PSarcn (RADIUS, RADIUS, 
  1371.         RADIUS-SHADOWOFFSET-3.0, 170.0, 100.0);
  1372.   PSarc (RADIUS, RADIUS, 
  1373.      RADIUS-SHADOWOFFSET-2.0, 100.0, 170.0);
  1374.   PSsetgray (NSLightGray);
  1375.   PSfill ();
  1376.   PSarcn (RADIUS, RADIUS, 
  1377.       RADIUS-SHADOWOFFSET-3.0, 350.0, 280.0);
  1378.   PSarc (RADIUS, RADIUS, 
  1379.      RADIUS-SHADOWOFFSET-2.0, 280.0, 350.0);
  1380.   PSsetgray (NSWhite);
  1381.   PSfill ();
  1382.   
  1383.   return self;
  1384. }
  1385.  
  1386. - drawUpperLeft:imageRep
  1387. {
  1388.   PSsetgray(NSBlack);
  1389.   PSsetlinewidth(0.0);
  1390.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1391.     PSsetalpha (1.0);
  1392.   }
  1393.  
  1394.   PSnewpath();
  1395.   PSmoveto(RADIUS, RADIUS);
  1396.   PSlineto([self bounds].size.width,RADIUS);
  1397.   PSmoveto(RADIUS, RADIUS);
  1398.   PSlineto(RADIUS, 0.0);
  1399.   PSmoveto(RADIUS-SHADOWOFFSET, RADIUS+SHADOWOFFSET);
  1400.   PSlineto([self bounds].size.width, RADIUS+SHADOWOFFSET);
  1401.   PSmoveto(RADIUS-SHADOWOFFSET, RADIUS+SHADOWOFFSET);
  1402.   PSlineto(RADIUS-SHADOWOFFSET, 0.0);
  1403.   PSstroke();
  1404.  
  1405.   return self;
  1406. }
  1407.  
  1408. - drawUpperRight:imageRep
  1409. {
  1410.   PSsetgray(NSBlack);
  1411.   PSsetlinewidth(0.0);
  1412.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1413.     PSsetalpha (1.0);
  1414.   }
  1415.  
  1416.   PSnewpath();
  1417.   PSmoveto(RADIUS, RADIUS);
  1418.   PSlineto(0.0,RADIUS);
  1419.   PSmoveto(RADIUS, RADIUS);
  1420.   PSlineto(RADIUS, 0.0);
  1421.   PSmoveto(RADIUS+SHADOWOFFSET, RADIUS+SHADOWOFFSET);
  1422.   PSlineto(0.0, RADIUS+SHADOWOFFSET);
  1423.   PSmoveto(RADIUS+SHADOWOFFSET, RADIUS+SHADOWOFFSET);
  1424.   PSlineto(RADIUS+SHADOWOFFSET, 0.0);
  1425.   PSstroke();
  1426.  
  1427.   return self;
  1428. }
  1429.  
  1430. - drawLowerLeft:imageRep
  1431. {
  1432.   PSsetgray(NSBlack);
  1433.   PSsetlinewidth(0.0);
  1434.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1435.     PSsetalpha (1.0);
  1436.   }
  1437.  
  1438.   PSnewpath();
  1439.   PSmoveto(RADIUS, RADIUS);
  1440.   PSlineto([self bounds].size.width,RADIUS);
  1441.   PSmoveto(RADIUS, RADIUS);
  1442.   PSlineto(RADIUS, [self bounds].size.height);
  1443.   PSmoveto(RADIUS-SHADOWOFFSET, RADIUS-SHADOWOFFSET);
  1444.   PSlineto([self bounds].size.width, RADIUS-SHADOWOFFSET);
  1445.   PSmoveto(RADIUS-SHADOWOFFSET, RADIUS-SHADOWOFFSET);
  1446.   PSlineto(RADIUS-SHADOWOFFSET, [self bounds].size.height);
  1447.   PSstroke();
  1448.  
  1449.   return self;
  1450. }
  1451.  
  1452. - drawLowerRight:imageRep
  1453. {
  1454.   PSsetgray(NSBlack);
  1455.   PSsetlinewidth(0.0);
  1456.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1457.     PSsetalpha (1.0);
  1458.   }
  1459.  
  1460.   PSnewpath();
  1461.   PSmoveto(RADIUS, RADIUS);
  1462.   PSlineto(0.0,RADIUS);
  1463.   PSmoveto(RADIUS, RADIUS);
  1464.   PSlineto(RADIUS, [self bounds].size.height);
  1465.   PSmoveto(RADIUS+SHADOWOFFSET, RADIUS-SHADOWOFFSET);
  1466.   PSlineto(0.0, RADIUS-SHADOWOFFSET);
  1467.   PSmoveto(RADIUS+SHADOWOFFSET, RADIUS-SHADOWOFFSET);
  1468.   PSlineto(RADIUS+SHADOWOFFSET, [self bounds].size.height);
  1469.   PSstroke();
  1470.  
  1471.   return self;
  1472. }
  1473.  
  1474. - drawMidLeft:imageRep
  1475. {
  1476.   PSsetgray(NSBlack);
  1477.   PSsetlinewidth(0.0);
  1478.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1479.     PSsetalpha (1.0);
  1480.   }
  1481.  
  1482.   PSnewpath();
  1483.   PSmoveto(RADIUS, RADIUS);
  1484.   PSlineto([self bounds].size.width,RADIUS);
  1485.   PSmoveto(RADIUS, [self bounds].size.height);
  1486.   PSlineto(RADIUS, 0.0);
  1487.   PSmoveto(RADIUS-SHADOWOFFSET, [self bounds].size.height);
  1488.   PSlineto(RADIUS-SHADOWOFFSET, 0.0);
  1489.   PSstroke();
  1490.  
  1491.   return self;
  1492. }
  1493.  
  1494. - drawMidRight:imageRep
  1495. {
  1496.   PSsetgray(NSBlack);
  1497.   PSsetlinewidth(0.0);
  1498.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1499.     PSsetalpha (1.0);
  1500.   }
  1501.  
  1502.   PSnewpath();
  1503.   PSmoveto(RADIUS, RADIUS);
  1504.   PSlineto(0.0,RADIUS);
  1505.   PSmoveto(RADIUS, [self bounds].size.height);
  1506.   PSlineto(RADIUS, 0.0);
  1507.   PSmoveto(RADIUS+SHADOWOFFSET, [self bounds].size.height);
  1508.   PSlineto(RADIUS+SHADOWOFFSET, 0.0);
  1509.   PSstroke();
  1510.  
  1511.   return self;
  1512. }
  1513.  
  1514. - drawMidTop:imageRep
  1515. {
  1516.   PSsetgray(NSBlack);
  1517.   PSsetlinewidth(0.0);
  1518.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1519.     PSsetalpha (1.0);
  1520.   }
  1521.  
  1522.   PSnewpath();
  1523.   PSmoveto(RADIUS, RADIUS);
  1524.   PSlineto(RADIUS,0.0);
  1525.   PSmoveto(0.0, RADIUS);
  1526.   PSlineto([self bounds].size.width, RADIUS);
  1527.   PSmoveto(0.0, RADIUS+SHADOWOFFSET);
  1528.   PSlineto([self bounds].size.width, RADIUS+SHADOWOFFSET);
  1529.   PSstroke();
  1530.  
  1531.   return self;
  1532. }
  1533.  
  1534. - drawMidBottom:imageRep
  1535. {
  1536.   PSsetgray(NSBlack);
  1537.   PSsetlinewidth(0.0);
  1538.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1539.     PSsetalpha (1.0);
  1540.   }
  1541.  
  1542.   PSnewpath();
  1543.   PSmoveto(RADIUS, RADIUS);
  1544.   PSlineto(RADIUS,[self bounds].size.height);
  1545.   PSmoveto(0.0, RADIUS);
  1546.   PSlineto([self bounds].size.width, RADIUS);
  1547.   PSmoveto(0.0, RADIUS-SHADOWOFFSET);
  1548.   PSlineto([self bounds].size.width, RADIUS-SHADOWOFFSET);
  1549.   PSstroke();
  1550.  
  1551.   return self;
  1552. }
  1553.  
  1554. - drawInnerSquare:imageRep
  1555. {
  1556.   PSsetgray(NSBlack);
  1557.   PSsetlinewidth(0.0);
  1558.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1559.     PSsetalpha (1.0);
  1560.   }
  1561.  
  1562.   PSnewpath();
  1563.   PSmoveto(0.0, RADIUS);
  1564.   PSlineto([self bounds].size.width,RADIUS);
  1565.   PSmoveto(RADIUS, [self bounds].size.height);
  1566.   PSlineto(RADIUS, 0.0);
  1567.   PSstroke();
  1568.  
  1569.   return self;
  1570. }
  1571.  
  1572. - drawInnerHandicap:imageRep
  1573. {
  1574.   PSsetgray(NSBlack);
  1575.   PSsetlinewidth(0.0);
  1576.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1577.     PSsetalpha (1.0);
  1578.   }
  1579.  
  1580.   PSnewpath();
  1581.   PSmoveto(0.0, RADIUS);
  1582.   PSlineto([self bounds].size.width,RADIUS);
  1583.   PSmoveto(RADIUS, [self bounds].size.height);
  1584.   PSlineto(RADIUS, 0.0);
  1585.   PSstroke();
  1586.   
  1587.   PSarc(RADIUS, RADIUS, SHADOWOFFSET, 0.0, 360.0);
  1588.   PSfill();
  1589.  
  1590.   return self;
  1591. }
  1592.  
  1593. // The following methods show or erase the stones from the board.
  1594.   
  1595.   - showBlackStone 
  1596. {
  1597.   NSRect tmpRect = {{floor(stoneX), floor(stoneY)},
  1598.               {floor(STONEWIDTH), floor(STONEHEIGHT)}};
  1599.   [blackStone compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1600.   return self;
  1601. }
  1602.  
  1603. - showWhiteStone
  1604. {
  1605.   NSRect tmpRect = {{floor(stoneX), floor(stoneY)},
  1606.               {floor(STONEWIDTH), floor(STONEHEIGHT)}};
  1607.   [whiteStone compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1608.   return self;
  1609. }
  1610.  
  1611. - showGrayStone
  1612. {
  1613.   NSRect tmpRect = {{floor(stoneX), floor(stoneY)},
  1614.               {floor(STONEWIDTH), floor(STONEHEIGHT)}};
  1615.   [grayStone compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1616.   return self;
  1617. }
  1618.  
  1619. - eraseStone
  1620. {
  1621.   NSRect tmpRect = {{floor(stoneX), floor(stoneY)}, {floor(STONEWIDTH), floor(STONEHEIGHT)}};
  1622.   return [self drawBackground:&tmpRect];
  1623. }
  1624.  
  1625. // drawBackground: just draws the specified piece of the background by
  1626. // compositing from the background image.
  1627.  
  1628. - showBackgroundPiece: (int)x: (int)y {
  1629.   int q;
  1630.   NSRect tmpRect = {{floor(stoneX), floor(stoneY)}, {floor(STONEWIDTH), floor(STONEHEIGHT)}};
  1631.  
  1632.   if ((x == 0) && (y == 0))
  1633.     [upperLeft compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1634.   
  1635.   if ((x == 0) && (y == MAXY - 1))
  1636.     [lowerLeft compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1637.   
  1638.   if ((x == MAXX - 1) && (y == 0))
  1639.     [upperRight compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1640.   
  1641.   if ((x == MAXX - 1) && (y == MAXY - 1))
  1642.     [lowerRight compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1643.   
  1644.   if ((x == 0) && (y > 0) && (y < MAXY - 1))
  1645.     [midLeft compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1646.   
  1647.   if ((x == MAXX - 1) && (y > 0) && (y < MAXY - 1))
  1648.     [midRight compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1649.   
  1650.   if ((x > 0) && (x < MAXX - 1) && (y == 0))
  1651.     [midTop compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1652.   
  1653.   if ((x > 0) && (x < MAXX - 1) && (y == MAXY - 1))
  1654.     [midBottom compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1655.   
  1656.   if ((x > 0) && (x < MAXX - 1) && (y > 0) && (y < MAXY - 1))
  1657.     [innerSquare compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1658.     
  1659.   if (MAXX < 13)
  1660.     q = 2;
  1661.   else
  1662.     q = 3;
  1663.   
  1664.   if (((x == q) && (y == q)) || ((x == q) && (y == MAXY/2)) ||
  1665.       ((x == q) && (y == MAXY-q-1)) || ((x == MAXX/2) && (y == q)) ||
  1666.       ((x == MAXX/2) && (y == MAXY/2)) || ((x == MAXX/2) && (y == MAXY-q-1)) ||
  1667.       ((x == MAXX-q-1) && (y == q)) || ((x == MAXX-q-1) && (y == MAXY/2)) ||
  1668.       ((x == MAXX-q-1) && (y == MAXY-q-1)))
  1669.     [innerHandicap compositeToPoint:tmpRect.origin operation:NSCompositeSourceOver];
  1670.  
  1671.   return self;
  1672. }
  1673.   
  1674.   - drawBackground:(NSRect *)rect
  1675. {
  1676.   NSRect tmpRect = *rect;
  1677.   
  1678.   (&tmpRect)->origin.x = floor(NSMinX(tmpRect));
  1679.   (&tmpRect)->origin.y = floor(NSMinY(tmpRect));
  1680.   if ([[NSDPSContext currentContext] isDrawingToScreen]) {
  1681.     PSsetgray (NSWhite);
  1682.     PScompositerect (NSMinX(tmpRect), NSMinY(tmpRect),
  1683.              NSWidth(tmpRect), NSHeight(tmpRect), NSCompositeCopy);
  1684.   }
  1685.   [backGround compositeToPoint:tmpRect.origin fromRect:tmpRect operation:NSCompositeSourceOver];
  1686.   return self;
  1687. }
  1688.  
  1689. // drawSelf::, a method every decent View should have, redraws the game
  1690. // in its current state. This allows us to print the game very easily.
  1691.   
  1692. - (void)drawRect:(NSRect)rects {
  1693.   int xcnt, ycnt;
  1694.   char s[5], specialChar;
  1695.   NSRect  aRect = [self bounds];
  1696.   [self drawBackground:(&rects ? &rects : &aRect)];
  1697.  
  1698.   specialChar = 'a';
  1699.  
  1700.   for (xcnt = 0; xcnt < MAXX; xcnt++)
  1701.     {
  1702.       for (ycnt = 0; ycnt < MAXY; ycnt++)
  1703.     {
  1704.       setStoneLoc(xcnt, ycnt);
  1705.  
  1706.       switch (p[xcnt][ycnt])
  1707.         {
  1708.         case EMPTY: currentCharacter = 0;
  1709.           [self showBackgroundPiece: xcnt: ycnt];
  1710.           break;
  1711.         case WHITESTONE: [self showWhiteStone];
  1712.           if ([showHistFlag intValue])
  1713.         {
  1714.           char s[5];
  1715.           
  1716.           [historyFont set];
  1717.           PSsetgray(NSBlack);
  1718.           PSmoveto(stoneX+RADIUS -
  1719.                (floor(log(hist[xcnt][ycnt]+0.5)/log(10))+1.0)*3,
  1720.                stoneY+RADIUS - 4);
  1721.           if (hist[xcnt][ycnt] > 0)
  1722.             {
  1723.               sprintf(s, "%d", hist[xcnt][ycnt]);
  1724.               PSshow(s);
  1725.             }
  1726.           else
  1727.             {
  1728.               PSmoveto(stoneX + RADIUS - 4, stoneY + RADIUS - 4);
  1729.               PSshow("H");
  1730.             }
  1731.         }
  1732.           break;
  1733.         case BLACKSTONE: [self showBlackStone];
  1734.           if ([showHistFlag intValue])
  1735.         {
  1736.           char s[5];
  1737.  
  1738.           [historyFont set];
  1739.           PSsetgray(NSWhite);
  1740.           PSmoveto(stoneX+RADIUS -
  1741.                (floor(log(hist[xcnt][ycnt]+0.5)/log(10))+1.0)*3,
  1742.                stoneY+RADIUS - 4);
  1743.           if (hist[xcnt][ycnt] > 0)
  1744.             {
  1745.               sprintf(s, "%d", hist[xcnt][ycnt]);
  1746.               PSshow(s);
  1747.             }
  1748.           else
  1749.             {
  1750.               PSmoveto(stoneX + RADIUS - 4, stoneY + RADIUS - 4);
  1751.               PSshow("H");
  1752.             }
  1753.         }
  1754.           break;
  1755.         case NEUTRAL_TERR: [self showGrayStone];
  1756.           break;
  1757.         case WHITE_TERR: [self showBackgroundPiece: xcnt: ycnt];
  1758.           [whiteTerrFont set];
  1759.           PSsetgray(NSWhite);
  1760.           PSmoveto(stoneX+RADIUS/3, stoneY+RADIUS/3+2);
  1761.           PSshow("W");
  1762.           break;
  1763.         case BLACK_TERR: [self showBackgroundPiece: xcnt: ycnt];
  1764.           [blackTerrFont set];
  1765.           PSsetgray(NSDarkGray);
  1766.           PSmoveto(stoneX+RADIUS/3+1, stoneY+RADIUS/3);
  1767.           PSshow("B");
  1768.           break;
  1769.         case SPECIAL_CHAR: [self showBackgroundPiece: xcnt: ycnt];
  1770.           PSselectfont("Helvetica", 25.0);
  1771.           PSsetgray(NSDarkGray);
  1772.           PSmoveto(stoneX+RADIUS/3+1, stoneY+RADIUS/3);
  1773.           sprintf(s,"%c",specialChar);
  1774.           specialChar++;
  1775.           PSshow(s);
  1776.           break;
  1777.         default: currentCharacter = 0;
  1778.           [self showBackgroundPiece: xcnt: ycnt];
  1779.           break;
  1780.         }
  1781.     }
  1782.     }
  1783.  
  1784.   if ([showCoords intValue])
  1785.     {
  1786.       for (xcnt = 0; xcnt < MAXX; xcnt++)
  1787.     {
  1788.       setStoneLoc(xcnt, 0);
  1789.  
  1790.       [historyFont set];
  1791.       PSsetgray(NSDarkGray);
  1792.       PSmoveto(stoneX + RADIUS - 3, stoneY + RADIUS + 11);
  1793.       s[0] = 'A' + xcnt;
  1794.       if (xcnt > 7) s[0]++;
  1795.       s[1] = 0;
  1796.       PSshow(s);
  1797.  
  1798.           setStoneLoc(xcnt, MAXY - 1);
  1799.           PSmoveto(stoneX + RADIUS - 3, stoneY - 3);
  1800.       PSshow(s);
  1801.     }
  1802.       for (ycnt = 0; ycnt < MAXX; ycnt++)
  1803.     {
  1804.       setStoneLoc(0, ycnt);
  1805.  
  1806.       [historyFont set];
  1807.       PSsetgray(NSDarkGray);
  1808.       PSmoveto(stoneX - 4, stoneY + RADIUS - 4);
  1809.       sprintf(s, "%d", MAXY-ycnt);
  1810.       PSshow(s);
  1811.  
  1812.       setStoneLoc(MAXX - 1, ycnt);
  1813.       if (xcnt < 10)
  1814.         {
  1815.           PSmoveto(stoneX + STONEWIDTH, stoneY + RADIUS - 4);
  1816.         }
  1817.       else
  1818.         {
  1819.           PSmoveto(stoneX + STONEWIDTH - 6, stoneY + RADIUS - 4);
  1820.         }
  1821.       PSshow(s);
  1822.     }
  1823.     }
  1824. }
  1825.  
  1826. - step {
  1827. //    NSEvent *peek_ev, *get_ev;
  1828.  
  1829.     if (gameType == IGSGAME) {
  1830.         return self;
  1831.     }
  1832.  
  1833.     if (neitherSide)
  1834.         return self;
  1835.  
  1836.     if (((currentStone == BLACKSTONE) && !blackSide) ||
  1837.         ((currentStone == WHITESTONE) && !whiteSide))
  1838.         return self;
  1839.  
  1840.     if (bothSides) {
  1841.         while ((gameRunning) && (!finished)) {
  1842.             [self selectMove];
  1843.             PSWait();
  1844. /*
  1845.             if( peek_ev = [NSApp nextEventMatchingMask:NSLeftMouseDownMask untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:NO] ){
  1846.                 get_ev = [ [self window] nextEventMatchingMask:NSLeftMouseDownMask untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:YES];
  1847.                 [NSApp sendEvent: get_ev];
  1848.             }
  1849. */
  1850.         }
  1851.  
  1852.         PSWait();
  1853.     }
  1854.     else {
  1855.         [passButton setEnabled:NO];
  1856.         [self selectMove];
  1857.  
  1858.         PSWait();
  1859.  
  1860.         PSWait();
  1861.         [passButton setEnabled:YES];
  1862.         PSWait();
  1863.     }
  1864.     return self;
  1865. }
  1866.  
  1867. - selectMove {
  1868.     int i, j;
  1869.  
  1870.     PSWait();
  1871.  
  1872.     if( !bothSides )
  1873.     [stopButton setEnabled:NO];
  1874.   else
  1875.     [stopButton setEnabled:YES];
  1876.  
  1877.   for (i = 0; i < MAXX; i++)
  1878.     for (j = 0; j < MAXY; j++)
  1879.       oldBoard[i][j] = p[i][j];
  1880.   
  1881.   genmove( &i, &j );
  1882.   if (i >= 0)
  1883.     {
  1884.       p[i][j] = currentStone;
  1885.  
  1886.       [self flashStone: i: j];
  1887.     }
  1888.  
  1889.   if (((i < 0) || (j < 0)) && (AGAScoring))
  1890.     {
  1891.       if (currentStone == BLACKSTONE)
  1892.         blackCaptured++;
  1893.       else
  1894.         whiteCaptured++;
  1895.     }
  1896.   
  1897.   [self selectMoveEnd];
  1898.     
  1899.   if (i >= 0)
  1900.     {
  1901.       [self lockFocus];
  1902.       if (currentStone == BLACKSTONE)
  1903.         [self showBlackStone];
  1904.       else
  1905.         [self showWhiteStone];
  1906.       [self unlockFocus];
  1907.  
  1908.       [self doClick];
  1909.     }
  1910.  
  1911.   [self updateInfo];
  1912.     
  1913.   [self addMoveToGameMoves: currentStone: i: j];
  1914.   
  1915.   if ([showHistFlag intValue])
  1916.     {
  1917.       NSRect tmpRect = {{floor(stoneX), floor(stoneY)},
  1918.               {floor(STONEWIDTH), floor(STONEHEIGHT)}};
  1919.  
  1920.       [self lockFocus];
  1921.       [self drawRect:tmpRect];
  1922.       [self display];
  1923.       [self unlockFocus];
  1924.     }
  1925.  
  1926.   PSWait();
  1927.   return self;
  1928. }
  1929.  
  1930. - selectMoveEnd
  1931. {
  1932.   PSWait();
  1933.  
  1934.   [startButton setEnabled:YES];
  1935.   [stopButton setEnabled:YES];
  1936.   PSWait();
  1937.  
  1938.   return self;
  1939. }
  1940.  
  1941. - flashStone: (int)x :(int)y
  1942. {
  1943.   
  1944.   setStoneLoc(x, y);
  1945.   
  1946.   [self lockFocus];
  1947.   [self showGrayStone];
  1948.   [self unlockFocus];
  1949.   
  1950.   return self;
  1951. }
  1952.  
  1953. - setMess1:(char *)s
  1954. {
  1955.   [gameMessage setStringValue:[NSString stringWithCString:s]];
  1956.   [gameMessage display];
  1957.  
  1958.   return self;
  1959. }
  1960.  
  1961. - setMess2:(char *)s
  1962. {
  1963.   [gameMessage2 setStringValue:[NSString stringWithCString:s]];
  1964.   [gameMessage2 display];
  1965.  
  1966.   return self;
  1967. }
  1968.  
  1969. - setblacksPrisoners:(int)bp
  1970. {
  1971.   [blacksPrisoners setIntValue:bp];
  1972.   [blacksPrisoners display];
  1973.  
  1974.   return self;
  1975. }
  1976.  
  1977. - setwhitesPrisoners:(int)wp
  1978. {
  1979.   [whitesPrisoners setIntValue:wp];
  1980.   [whitesPrisoners display];
  1981.  
  1982.   return self;
  1983. }
  1984.  
  1985. - (long)startZeit {
  1986.     return startZeit;
  1987. }
  1988.  
  1989. - setStartZeit:(long)aTime {
  1990.     startZeit = aTime;
  1991.     return self;
  1992. }
  1993.  
  1994. - (int)bByo {
  1995.     return bByo;
  1996. }
  1997.  
  1998. - (TimeStruct*)ts {
  1999.     return &ts;
  2000. }
  2001.  
  2002. - gameCompleted {
  2003.     
  2004.     [self removeTE];    
  2005.       [self setblacksPrisoners:0];
  2006.       [self setwhitesPrisoners:0];
  2007.     [IGSGameNumber setStringValue:@""]; 
  2008.     [IGSBlackPlayer setStringValue:@""]; 
  2009.     [IGSWhitePlayer setStringValue:@""]; 
  2010.     [IGShandicap setStringValue:@""]; 
  2011.     [IGSkomi setStringValue:@""];
  2012.     [blackTime setStringValue:@""];
  2013.     [whiteTime setStringValue:@""];
  2014.  
  2015.     return self;
  2016. }
  2017.  
  2018. - removeTE {
  2019.     if (te) {
  2020.     [te invalidate];
  2021.         [te release];
  2022.         te = 0;
  2023.     }
  2024.     return self;
  2025. }
  2026.  
  2027. - (void) TEHandler:(NSTimer *)aTimer {
  2028.     id obj;
  2029.     NSString *buf;
  2030.     int myTime, now;
  2031.     struct timeval tp;
  2032.     struct timezone tzp;
  2033.     gettimeofday(&tp, &tzp);
  2034.     now = tp.tv_sec;
  2035.  
  2036.     obj  = [aTimer userInfo];
  2037.  
  2038.     if ([obj startZeit] == 0L) {
  2039.         [obj setStartZeit:now];
  2040.     }
  2041.     myTime = ts.time - (now - [obj startZeit]);
  2042. #ifdef TIMEDEBUG
  2043.     printf("TEHandler: now = %ld, startZeit = %ld, ts.time = %d, myTime = %d\n", now, [obj startZeit], ts.time, myTime); 
  2044. #endif
  2045.     if (myTime < 0) {
  2046.         if (ts.byo == -1 ||         /* player is in normal game time */
  2047.             ts.byo == 25) {         /* player is in byo-yomi but did     */
  2048.                                         /* not yet move */
  2049.             myTime += [obj ByoTime] * 60;
  2050.             ts.byo = 25;
  2051.         }
  2052.     }
  2053.     buf = [NSString stringWithFormat:@"%d:%02d", myTime / 60, myTime % 60];
  2054.     if (ts.byo != -1) {
  2055.         buf = [buf stringByAppendingFormat:@", %d", ts.byo];
  2056.     }
  2057.     [ts.timeToHandle setStringValue:buf];
  2058.     [ts.timeToHandle display];
  2059. }
  2060.  
  2061. @end
  2062.  
  2063.