home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume8 / mgt / part03 / play.c < prev   
C/C++ Source or Header  |  1990-02-23  |  2KB  |  111 lines

  1.  
  2. /*
  3.  
  4.         "mgt" Copyright 1990 Shodan
  5.         All Rights Reserved.
  6.         Program by Greg Hale
  7.  
  8. Permission to use, copy, modify, and distribute this software and its
  9. documentation for any purpose and without fee is hereby granted,
  10. provided that this entire comment and copyright notice appear in all
  11. copies and that both that copyright notice and this permission notice
  12. appear in supporting documentation.  No representations are made about
  13. the suitability of this software for any purpose.  It is provided "as
  14. is" without express or implied warranty.
  15.  
  16. Please send copies of extensions to:
  17.  
  18. hale@scam.berkeley.edu
  19. 128.32.138.4    scam.berkeley.edu sting sting.Berkeley.EDU
  20.  
  21. Donations for the 'From My Go Teacher' series may be sent to:
  22.     Shodan
  23.     P.O. Box 4456
  24.     Berkeley, CA 94704
  25.     (415) 849-9475
  26.  
  27. */
  28.  
  29. #include "mgt.h"
  30. #include "var.h"
  31.  
  32. #define whichPiece(i) (i)
  33.  
  34.  
  35. FUNCTION boolean inRange(i,j)
  36. {
  37.     return  i >= 0 && i < boardsize && j >= 0 && j < boardsize;
  38. }
  39.  
  40. FUNCTION int lib0(b,m,i,j,t)
  41. pBoard b;
  42. pBoard m;
  43. int i,j;
  44. piece t;
  45. {
  46.     piece p,pt;
  47.  
  48.     if (!inRange(i,j)) return 0;
  49.     p = b->b[i][j];
  50.     pt = whichPiece(p);
  51.     if ((pt != P_NOTHING && pt != t) || m->b[i][j])
  52.         return 0;
  53.     m->b[i][j] = (pt == t) ? (piece)1 : (piece)2;
  54.     if (whichPiece(p) == P_NOTHING) return 1;
  55.     return lib0(b,m,i,j+1,t) + lib0(b,m,i+1,j,t) + lib0(b,m,i-1,j,t) +
  56.             lib0(b,m,i,j-1,t);
  57. }
  58.  
  59. FUNCTION int liberties(b,i,j)    /* count the # of liberties for group at i,j */
  60. pBoard b;
  61. int i,j;
  62. {
  63.     board m;
  64.  
  65.     boardClear(&m);
  66.     return lib0(b,&m,i,j, whichPiece(b->b[i][j]));
  67. }
  68.  
  69. FUNCTION void removeStones(b,i,j)
  70. pBoard b;
  71. int i,j;
  72. {
  73.     board m;
  74.  
  75.     boardClear(&m);
  76.     lib0(b,&m,i,j, whichPiece(b->b[i][j]));
  77.     for (i=19;i--;)
  78.         for(j=19;j--;)
  79.             if (m.b[i][j]==(piece)1)
  80.                 b->b[i][j] = P_NOTHING;
  81. }
  82.  
  83. FUNCTION boolean tryKill(b,i,j,t)
  84. pBoard b;
  85. int i,j;
  86. piece t;
  87. {
  88.     piece w;
  89.     if (!inRange(i,j))
  90.         return false;
  91.     w = whichPiece(b->b[i][j]);
  92.     if (w != P_NOTHING && w != t && !liberties(b,i,j)) {
  93.         removeStones(b,i,j);
  94.         return true;
  95.     }
  96.     return false;
  97. }
  98.  
  99. FUNCTION boolean placeStone(b,i,j,t)
  100. pBoard b;
  101. int i,j;
  102. piece t;
  103. {
  104.     board bold;
  105.     if (!inRange(i,j))
  106.         return false;
  107.     b->b[i][j] = t;
  108.     return !(tryKill(b,i,j-1,t) | tryKill(b,i,j+1,t) |
  109.         tryKill(b,i-1,j,t) | tryKill(b,i+1,j,t));
  110. }
  111.