home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume8
/
mgt
/
part03
/
play.c
< prev
Wrap
C/C++ Source or Header
|
1990-02-23
|
2KB
|
111 lines
/*
"mgt" Copyright 1990 Shodan
All Rights Reserved.
Program by Greg Hale
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that this entire comment and copyright notice appear in all
copies and that both that copyright notice and this permission notice
appear in supporting documentation. No representations are made about
the suitability of this software for any purpose. It is provided "as
is" without express or implied warranty.
Please send copies of extensions to:
hale@scam.berkeley.edu
128.32.138.4 scam.berkeley.edu sting sting.Berkeley.EDU
Donations for the 'From My Go Teacher' series may be sent to:
Shodan
P.O. Box 4456
Berkeley, CA 94704
(415) 849-9475
*/
#include "mgt.h"
#include "var.h"
#define whichPiece(i) (i)
FUNCTION boolean inRange(i,j)
{
return i >= 0 && i < boardsize && j >= 0 && j < boardsize;
}
FUNCTION int lib0(b,m,i,j,t)
pBoard b;
pBoard m;
int i,j;
piece t;
{
piece p,pt;
if (!inRange(i,j)) return 0;
p = b->b[i][j];
pt = whichPiece(p);
if ((pt != P_NOTHING && pt != t) || m->b[i][j])
return 0;
m->b[i][j] = (pt == t) ? (piece)1 : (piece)2;
if (whichPiece(p) == P_NOTHING) return 1;
return lib0(b,m,i,j+1,t) + lib0(b,m,i+1,j,t) + lib0(b,m,i-1,j,t) +
lib0(b,m,i,j-1,t);
}
FUNCTION int liberties(b,i,j) /* count the # of liberties for group at i,j */
pBoard b;
int i,j;
{
board m;
boardClear(&m);
return lib0(b,&m,i,j, whichPiece(b->b[i][j]));
}
FUNCTION void removeStones(b,i,j)
pBoard b;
int i,j;
{
board m;
boardClear(&m);
lib0(b,&m,i,j, whichPiece(b->b[i][j]));
for (i=19;i--;)
for(j=19;j--;)
if (m.b[i][j]==(piece)1)
b->b[i][j] = P_NOTHING;
}
FUNCTION boolean tryKill(b,i,j,t)
pBoard b;
int i,j;
piece t;
{
piece w;
if (!inRange(i,j))
return false;
w = whichPiece(b->b[i][j]);
if (w != P_NOTHING && w != t && !liberties(b,i,j)) {
removeStones(b,i,j);
return true;
}
return false;
}
FUNCTION boolean placeStone(b,i,j,t)
pBoard b;
int i,j;
piece t;
{
board bold;
if (!inRange(i,j))
return false;
b->b[i][j] = t;
return !(tryKill(b,i,j-1,t) | tryKill(b,i,j+1,t) |
tryKill(b,i-1,j,t) | tryKill(b,i+1,j,t));
}