*/
Want to see what people are talking about? See the latest forum posts.
*/

View \FILE.C

C souce code for the game of Go

Submitted By: WEBMASTER
Rating: starstarstarstar (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 extern
#include "goh.c"


void readgame(FILE *stream)
{
 int status;
 int i;
 int junk;
 unsigned index;
 unsigned jndex, temp, elapsed;
 unsigned indexes, indexp, count;
 indexes = xtinsert(0, 0);
 indexp = indexes;
 count = 0;
 do {
  status = fscanf(stream, "%u", &index);
  if (status && (status != -1)) {
   ++count;
   indexp = xtlocate(count, indexp);
   indexp = xtinsert(count, indexp);
   poke(indexp, LEVEL, index);
   /*
   newline();
   printf("%d ", index);
   coords(index);
   hundelay(20);
   */

  }
 } while (status && (status != -1));
 cput(0);
 indexp = peek(indexes, NEXTL);
 if (indexp) {
  do {
   index = peek(indexp, LEVEL);
   doit(index);
   forget(nforget);
   indexp = peek(indexp, NEXTL);
  } while (!attn && indexp);
 }
 elapsed = cput(1);
 xtfreelist(indexes);
 ndoits = kount;
 if (kount > 1) {
  newline();
  printf(" %u/", ndoits);
  jndex = elapsed;
  jndex /= 10;
  temp = jndex;
  jndex /= 10;
  temp -= jndex * 10;
  printf("%u.%u", jndex, temp);
  if (elapsed) elapsed = ((long)ndoits*100) / elapsed;
  printf("=%u", elapsed);
  printf(" moves/sec");
 }
 if (movep->midx) chkcond(movep->midx);
 obseye = 0;
 headjob();
 board();
 prompt();
 /*dispate();*/
}

unsigned getsize()
{
 unsigned size;
 int handle, status;
 FILE *stream;
 int i;
 unsigned index;
 #if DDD
 size = 7;
 stream = fopen("GO3.DAT", "r+t");
 #else
 size = 19;
 stream = fopen("GO.DAT", "r+t");
 #endif
 status = 0;
 if (!stream) {
  newline();
  #if DDD
  printf("no GO3.DAT file");
  #else
  printf("no GO.DAT file");
  #endif
 }
 else {
  rewind(stream);
  status = fscanf(stream, "%d", &size);
  if(!status) {
   newline();
   printf("fscanf fails");
  }
  else {
   newline();
   printf("nsize:%d",size);
  }
  status = fclose(stream);
  if (status) {
   newline();
   printf("fclose fails");
  }
 }
 return(size);
}

void gfile(char *stringy)
{
 int handle, status;
 FILE *stream;
 int i;
 int junk;
 unsigned index, size;
 if (1) {
  stream = fopen(stringy, "r+t");
  status = 0;
  if (!stream) {
   newline();
   newline();
   printf("no %s file", stringy);
   hundelay(100);
  }
  else {
   rewind(stream);
   status = fscanf(stream, "%d", &size);
   if(!status) {
    newline();
    printf("fscanf fails");
   }
   else {
    newline();
    printf(" nsize:%u",size);
    if (size != nsize) {
     newline();
     printf("size not same");
     printf(" as GO.DAT");
    }
    else {
     readgame(stream);
    }
   }
   status = fclose(stream);
   if (status) {
    newline();
    printf("fclose fails");
   }
  }
 }
}

void infile()
{
 int handle, status;
 FILE *stream;
 int i;
 int junk;
 unsigned index, size;
 char stringy[32];
 newline();
 printf("This just reads a file!");
 newline();
 printf("  You may want to use");
 newline();
 printf("   'Esc n' before this");
 newline();
 printf("   to get new game");
 newline();
 printf("Enter file name");
 newline();
 scr_curson();
 i = bscanf(stringy, 30);
 scr_cursoff();
 if (!i) return;
 newline();
 printf("load file");
 newline();
 printf("%s", stringy);
 if (1) {
  stream = fopen(stringy, "r+t");
  status = 0;
  if (!stream) {
   newline();
   printf("fopen fails");
  }
  else {
   rewind(stream);
   status = fscanf(stream, "%d", &size);
   if(!status) {
    newline();
    printf("fscanf fails");
   }
   else {
    newline();
    printf(" nsize:%u",size);
    if (size != nsize) {
     newline();
     printf("size not same");
     newline();
     printf("as .DAT");
    }
    else {
     readgame(stream);
    }
   }
   status = fclose(stream);
   if (status) {
    newline();
    printf("fclose fails");
   }
  }
 }
}

void outfile()
{
 int handle, status;
 FILE *stream;
 int i;
 unsigned index;
 #if DDD
 stream = fopen("GO3.DAT", "w");
 #else
 stream = fopen("GO.DAT", "w");
 #endif
 status = 0;
 if (!stream) {
  newline();
  printf("fopen fails");
 }
 else {
  rewind(stream);
  fprintf(stream, "%d", nsize);
  newline();
  printf(" nsize:%d",nsize);
  xthistp = xtlocate(1, xthistory);
  if (xtmember(1, xthistp)) {
   do {
    index = peek(xthistp, LEVEL);
    if (index) curon(index);
    fprintf(stream, "\n%u", index);
    newline();
    printf(" index:%u",index);
    xthistp = peek(xthistp, NEXTL);
   } while (xthistp);
   xthistp = xthistory;
  }
  status = fclose(stream);
  if (status) {
   newline();
   printf("fclose fails");
  }
 }
}

void logfile()
{
 int handle, status;
 FILE *stream;
 int i;
 unsigned index;
 #if DDD
 stream = fopen("GOLOG3.DAT", "w");
 #else
 stream = fopen("GOLOG.DAT", "w");
 #endif
 status = 0;
 if (!stream) {
  newline();
  printf("fopen fails.. GOLOG.DAT");
 }
 else {
  rewind(stream);
  fprintf(stream, "%d", nsize);
  xthistp = xtlocate(1, xthistory);
  if (xtmember(1, xthistp)) {
   do {
    index = peek(xthistp, LEVEL);
    fprintf(stream, "\n%u", index);
    xthistp = peek(xthistp, NEXTL);
   } while (xthistp);
   xthistp = xthistory;
  }
  status = fclose(stream);
  if (status) {
   newline();
   printf("fclose fails.. GOLOG.DAT");
  }
 }
}

void texout()
{
 int handle;
 FILE *stream;
 int i, j, k;
 unsigned index;
 unsigned top, bot, lft, rgt;
 stream = fopen("TEX.ZAT", "w");
 if (!stream) {
  newline();
  printf("fopen fails");
 }
 else {
  rewind(stream);
  newline();
  printf(" output TEX.ZAT");
  fprintf(stream, "\n\\hsize%d\\unit \\noindent \\strut", nsize+2);
  fprintf(stream, "\n\\hbox{");
  for (i=1; i<=nsize2; ++i) {
   k = nsize + 1 - X[i];
   j = Y[i];
   if (!dir[i]) {
    if (handipt(i)) fprintf(stream, "\n\\point %d %d \\zthcp", j, k);
    else {
     top = (k == nsize);
     bot = (k == 1);
     lft = (j == 1);
     rgt = (j == nsize);
     if (top && lft) fprintf(stream, "\n\\point %d %d \\zttlc", j, k);
     else if (top && rgt) fprintf(stream, "\n\\point %d %d \\zttrc", j, k);
     else if (bot && lft) fprintf(stream, "\n\\point %d %d \\ztblc", j, k);
     else if (bot && rgt) fprintf(stream, "\n\\point %d %d \\ztbrc", j, k);
     else if (top) fprintf(stream, "\n\\point %d %d \\zttop", j, k);
     else if (bot) fprintf(stream, "\n\\point %d %d \\ztbot", j, k);
     else if (lft) fprintf(stream, "\n\\point %d %d \\ztlft", j, k);
     else if (rgt) fprintf(stream, "\n\\point %d %d \\ztrgt", j, k);
     else fprintf(stream, "\n\\point %d %d \\ztcrs", j, k);
    }
   }
   else {
    if (dir[i]->clr == BLK) fprintf(stream, "\n\\point %d %d \\ztblk", j, k);
    if (dir[i]->clr == WHT) fprintf(stream, "\n\\point %d %d \\ztwht", j, k);
   }
  }
  fprintf(stream, "\n}");
 }
}

corner
© 1996-2008 CommunityHeaven LLC. 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.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.