Love this site? Hate it? Leave us some comments.

View \GO.C

C souce code for the game of Go

Submitted By: WEBMASTER
Rating: starstarstarhalf star (Rate It)


#include <dos.h>
#include <conio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <alloc.h>
#include <graphics.h>
#define EXTCDECL
#include "goh.c"

void headjob()
{
 nowkount = kount;      /* kount is actually number of next move */
 maxlizard = kount + 59;/* 60 move lookahead for last play by opponent */

 /* these no longer used ... fight.c replaced by tac.c */
 maxenv = maxlizard;
 maxeyes = kount-1;
 maxcounter = kount-1;
 max3 = kount-1;
 maxgeta = kount-1;
 maxfork = kount-1;
 maxspan = kount-1;
 bugcheck = 0/* kill/save lifeform if 1 */

 player = colour;
 opponent = flip(player);
}

void allon()
{
 envon = 1/* set all perceptions on */
 eyeson = 1;
 sphron = 1;
 spanon = 1;
 edgeon = 0;
 areaon = 0;
}

void alloff()
{
 envon = 1/* set all perceptions off (except envon) */
 eyeson = 0;
 sphron = 0;
 spanon = 0;
 edgeon = 0;
 areaon = 0;
}

void warm()
{
 unsigned i, j, k, *kp, index, backup, control, elapsed, ttltime;
 unsigned x4, x3, where;
 int undex, bounty;
 do {
  do {
   occ = 0;
   ko = 0;
   sui = 0;
   suilegal = 0;
   if (!fixhead) headjob();
   evaluate(); /* score by partitions */
   evaperim(); /* perimeter */
   if (col80) prompt();
   /*if (col80) dispate();*/
   index = scrindex;
   if (wauto && bauto && col80) {
    curon(index);
    hundelay(100);
   }
   ndoits = 0;
   cput(0);
   undex = getmove();
   ttltime = cput(1);
   allon(); /* in case any perceptions were turned off */
   hide = 0;
   if (!col80) curof(index);
   suilegal = 1;
   inform = 1;
   control = !((0 <= undex) && (undex <= nsize2));
   backup = (undex == -1);
   if (control) valid = control;
   else {
    index = undex;
    cput(0);
    valid = doit(index);
    if (valid) {
     if (index) scrindex = index;
     if (col80) {
      elapsed = cput(1);
      ttltime += elapsed;
      newline();
      printf("#%u", kount-1);
      if (colour == WHT) printf(" %c", BCHAR);
      else printf(" %c", WCHAR);
      if (index) coords(index);
      else printf(" pass");
      printf(" ntr:%d", ntrans);
      if ((bauto && (colour == WHT)) || (wauto && (colour == BLK))) {
       newline();
       printf(" %u / %u = ", ndoits, ttltime);
       elapsed = ((long) ndoits * 100) / ttltime;
       printf("%u", elapsed);
      }
     }
    }
    else curof(scrindex);
    if (!valid) sayso(index);
    else if (!gameover) {
     if (!index && (kount > 2)) {
      where = peek(xthistp, PREVL);
      if (where) {
       if (!peek(where, LEVEL)) {
        bauto = 0;
        wauto = 0;
        newline();
        newline();
        printf("GAMEOVER ");
        for (i=1; i<=1; ++i) {  /* used to be 8 */
         for (j=1; j<=10; ++j) {
          sound((10-i)*150 + (11-j)*150);
          hundelay(5);
         }
         newline();
        }
        nosound();
        printf("Insert quarter");
       }
      }
     }
     /*logfile();*/ /* log every move to GOLOG.DAT .. good for debugging */
    }
   }
  } while (!valid);
  if (backup && movep->prevm) {
   index = undoit();
   if (col80) {
    newline();
    printf(" undo #%d", kount);
    if (colour == BLK) printf(" %c", BCHAR);
    else printf(" %c", WCHAR);
    if (index) coords(index);
    else printf(" pass");
   }
   else curof(index);
  }
  if (!control) forget(nforget);
  if (movep->midx) chkcond(movep->midx);
  if (col80) {
   #if DONUT
   if (movep->midx) {
    xy(movep->midx, &i, &j);
    xdonut = (nsize + 1) / 2 - j;
    ydonut = (nsize + 1) / 2 - i;
   }
   #endif
   board();
  }
  else {
   if (movep->midx) scrindex = movep->midx;
   curof(scrindex);
  }
 } while (!gameover);
}

/* function prototypes necessary for farcoreleft, farmalloc */
/*
unsigned long farcoreleft(void);
void far *farmalloc(unsigned long nbytes);
void farfree(void far *block);
*/



void fix()
{/* free all memory */
 /* 1M globals */
 farfree(Y);
 farfree(X);
 farfree(gudeye);
 farfree(oneeye);
 farfree(coneye);
 farfree(eye);
 farfree(lib);
 farfree(farndir);
 farfree(farxt);
 /* 64k globals */
 free(flg);
 free(first);
 closegraph();
 textmode(7);
 scr_curson();
}



void cold(unsigned foist)
{
 unsigned size;
 unsigned i, j, k, *kp, index, backup, control, nplanes;
 int undex;
 unsigned elapsed, jndex, temp;
 unsigned long nodetab;
 unsigned far *farp;
 /*stklen = 8192;*/  /* double stack size */
 clkptr = MK_FP(0x0040, 0x006c);
 opengraph();
 setvisualpage(0);
 setactivepage(0);
 visipage = 0;
 graphmode = getgraphmode();
 restorecrtmode();
 
 col80 = 1;
 directvideo = 1;
 clrscr();

 size = getsize(); /* size is set by first line of GO.DAT */
 nsize = size;  /* minimum size is 4? due to edge nodes in tri.c */

 nforget = 10;
 if (nsize <= 13) nforget = 50;


 #if DDD
 nplanes = nsize;
 nsize2 = nsize * nsize * nsize;
 #else
 nplanes = 1;
 nsize2 = nsize * nsize;
 #endif
 #if DONUT
 xdonut = 0;
 ydonut = 0;
 #endif
 nsize21 = nsize2 + 1;

 /* get space from system here ... use fix() to free space on exit! */
 initlarge()/* initlarge (LARGE) must be before initsmall (SMALL) */
 initsmall();   /* small memory units take rest of 64K global memory */
 farndir = farmalloc(nsize21*4); /* far ptr node directory */
 lib = farmalloc(nsize21*4); /* far ptr lib info 1=lib 1bit */
 eye = farmalloc(nsize21*4); /* far ptr eye info 0=deadeye 1,2,3,4=eye 3bit */
 coneye = farmalloc(nsize21*4); /* far ptr conflicting eye 1bit */
 oneeye = farmalloc(nsize21*4); /* far ptr oneeye (false eyes) 1bit */
 gudeye = farmalloc(nsize21*4); /* far ptr gudeye (good eyes) 1bit */
 X = farmalloc(nsize21*2); /* far ptr x coord of index */
 Y = farmalloc(nsize21*2); /* far ptr y coord of index */
 initextra()/* allocate remaining memory for segment only (EXTRA) access */
 hundelay(100);

 ntrans = 0;

 scr_cursoff();
 clrscr(); /* clear screen */

 clrscr(); /* reset screen */

 maxarea = 5;
 maxscale = 5;

 maxdist = 4;
 xdist = 18;

#if DDD
 scrindex = fxyz(nsize/2+1,nsize/2+1,1);
#else
 scrindex = fxy(nsize/2+1,nsize/2+1);
#endif
 bauto = 0;
 wauto = 0;
 nvalrow = 0;
 kount = 0;
 timer = 0;
 dizzy = 0;
 gameover = 0;
 movep = 0;
 nopen = nsize2;
 for (index=0; index<=nsize2; ++index) dir[index] = 0;
 /* initialize neighbours (news=north,west,east,south) */
 kp = &bors[0];
 #if !DONUT
 for (k=1; k<=nplanes; ++k) {
  for (j=1; j<=nsize; ++j) {
   for (i=1; i<=nsize; ++i) {
    index = i + (j-1) * nsize + (k-1) * nsize * nsize;
    /*news[index] = kp;*/
    #if DDD
    if (k > 1) *kp = index - nsize * nsize;
    else *kp = 0; /* down */
    ++kp;
    #endif
    if (j > 1) *kp = index - nsize;
    else *kp = 0; /* north */
    ++kp;
    if (i > 1) *kp = index - 1;
    else *kp = 0; /* west */
    ++kp;
    if (i < nsize) *kp = index + 1;
    else *kp = 0; /* east */
    ++kp;
    if (j < nsize) *kp = index + nsize;
    else *kp = 0; /* south */
    ++kp;
    #if DDD
    if (k < nplanes) *kp = index + nsize * nsize;
    else *kp = 0; /* up */
    ++kp;
    #endif
   }
  }
 }
 #endif
 #if DONUT
 for (k=1; k<=nplanes; ++k) {
  for (j=1; j<=nsize; ++j) {
   for (i=1; i<=nsize; ++i) {
    index = i + (j-1) * nsize + (k-1) * nsize * nsize;
    if (j > 1) *kp = index - nsize;
    else *kp = index - nsize + nsize2; /* north */
    ++kp;
    if (i > 1) *kp = index - 1;
    else *kp = index - 1 + nsize; /* west */
    ++kp;
    if (i < nsize) *kp = index + 1;
    else *kp = index + 1 - nsize; /* east */
    ++kp;
    if (j < nsize) *kp = index + nsize;
    else *kp = index + nsize -nsize2; /* south */
    ++kp;
   }
  }
 }
 #endif

 /* initialize X,Y coords and xtlists of rings */
 initring();
 
 /* set up edgedist to contain minimum distance (howfar) to edge (0) */
 /* on edge is zero, one away from edge is 2, ... */
 for (index=1; index<=nsize2; ++index) {
  j = X[index];
  k = Y[index];
  i = min(j, k) - 1;
  j = min(nsize-j, nsize-k);
  k = min(i, j);
  k += k;
  edgedist[index] = k;
 }

 /* permanent list structures outside of 64k small memory */
 xthistory = xtinsert(0,0);
 xthistp = xthistory;

 nodes[BLK] = xtinsert(0,0);
 nodes[WHT] = xtinsert(0,0);
 spans[BLK] = xtinsert(0,0);
 spans[WHT] = xtinsert(0,0);
 areas[BLK] = xtinsert(0,0);
 areas[WHT] = xtinsert(0,0);

 gnodes[BLK] = xtinsert(0,0);
 gnodes[WHT] = xtinsert(0,0);
 gspans[BLK] = xtinsert(0,0);
 gspans[WHT] = xtinsert(0,0);

 treelist = xtinsert(0,0);
 maxsort(treelist);
 treep = treelist;


 /* no eyes to start (mkeyes) */
 eyeterr[BLK] = 0;
 eyeterr[WHT] = 0;
 sekisto[BLK] = 0;
 sekisto[WHT] = 0;
 deadsto[BLK] = 0;
 deadsto[WHT] = 0;
 deadlib[BLK] = 0;
 deadlib[WHT] = 0;
 eyescore[BLK] = 0;
 eyescore[WHT] = 0;

 /* no territory to start */
 triterr[BLK] = 0;
 triterr[WHT] = 0;
 triscore[BLK] = 0;
 triscore[WHT] = 0;

 /* no inverse video to start */
 for (index=1; index<=nsize2; ++index) invid[index] = 0;


 /* no node directory to start .. node directory is far */
 farp = farndir; /* black */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 farp = farndir;
 farp += nsize21; /* white */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 
 /* no lib info to start */
 farp = lib; /* black */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 farp = lib;
 farp += nsize21; /* white */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 
 /* no eye info to start */
 farp = eye; /* black */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 farp = eye;
 farp += nsize21; /* white */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 
 /* no conflicting eye info to start */
 farp = coneye; /* black */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 farp = coneye;
 farp += nsize21; /* white */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 
 /* no oneeye info to start */
 farp = oneeye; /* black */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 farp = oneeye;
 farp += nsize21; /* white */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 
 /* no gudeye info to start */
 farp = gudeye; /* black */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }
 farp = gudeye;
 farp += nsize21; /* white */
 for (index=1; index<=nsize2; ++index) {
  ++farp;
  *farp = 0;
 }

 /* no iz to start */
 for (index=1; index<=nsize2; ++index) ownctrl[index] = 0;
 for (index=1; index<=nsize2; ++index) owniz[index] = 255;

 /* no spheres of influence to start */
 for (index=1; index<=nsize2; ++index) whose[index] = 255;

 /* no perimeter of sphere of influence to start */
 for (index=1; index<=nsize2; ++index) perisphr[index] = 0;

 /* no partitions */
 numparts = 0;
 for (index=1; index<=nsize2; ++index) gloparts[index] = 0;
 for (i=0; i<=NUMPARTS; ++i) {
  partinfo[i] = 0;
  partnvac[i] = 0;
  partnocc[i] = 0;
 }

 /* no score */
 sphscore[BLK] = 0;
 sphscore[WHT] = 0;
 sphscore[0] = 0; /* not used */
 
 attn = 0;
 timeattn = 0;
 numticks = 2; /* specify here in seconds  then convert to ticks */
 numticks = (long) (numticks * 1092) / 60 + 1; /* 18.2 ticks/sec */
 hide = 0;
 hidelook = 0;
 xrad = 13;
 yrad = 9;
 xrad *= 19;
 xrad /= nsize;
 yrad *= 19;
 yrad /= nsize;
 npersp = 18;
 #if DDD
 xrad = 19;
 yrad = 9;
 zinc = yrad + yrad - 2;
 #endif;
 persp = 0;
 twister = 0;
 maxdist = 7;
 dbglevel = 0;
 chinese = 0;
 bstones = 0;
 wstones = 0;
 bcaps = 0;
 wcaps = 0;
 bter = 0;
 wter = 0;
 bdead = 0;
 wdead = 0;
 bscore = 0;
 wscore = 0;
 condition = 100;
 cyclops = 0;
 suilegal = 1;
 fixhead = 0;
 rootlist = insert(0, 0);
 allon();
 colour = WHT;
 valid = doit(0);
 board();
 menu();
 #if DDD
 gfile("GO3.DAT"); /* input */
 #else
 if (foist) gfile("GO.DAT"); /* input */
 #endif
 ndoits = 0;
 prompt();
}

void main()
{
 unsigned foist;
 foist = 1;
 do {
  clrscr();
  cold(foist);
  foist = 0;
  warm();
  fix();
 } while (1);
}

corner
© 1996-2008. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin.
bootstrapLabs Logo A bootstrapLabs project.