Ejemplo n.º 1
0
char *clientformat(char* input){
  if(input[0] == 'S'){
    //Text output starting with Server: ...
    printf("%s",input);
  }else if(input[0] == 'm'){
    //Move should update board
    updateboard(input[1],input[2],input[3]);
  }else if(input[0] == 'w'){
    //Win condition update
    winplayer = 
  }
  return boardstate;
}
Ejemplo n.º 2
0
int 
load_sgf(FILE *input, FILE *outfile, char *fromstr, char *untilstr)
{
  /* use a simple state engine : 
  * 0 = skipping junk, looking for ';' or 'H'
  * 1 = seen ';'  - looking for A or B/W.
  * 2 = seen B/W, looking for [
  * 3 = skip until ], then return to 1  
  *       (eg for PW, see P, skip to the ],  then state 1)
  * 4 = seen 'H', looking for A
  * 5 = seen 'S', looking for Z
  * 6 = seen 'A', looking for B or W
  * 7 = seen 'AB' or 'AW', adding stones
  * 8 = seen '[' while adding stones
  */

  int state = 0;
  float ll, rr, tt, bb;
  int bigfonts = 0;

  /* if we see AB[], we need to return to state 1, else state 0 */
  int nextstate = 0;  
  int color = 0;
  int c, n, m, i, j;
  int k;
  
  int untilm = -1, untiln = -1;
  int until = 9999;
  int from_m = -1, from_n = -1;
  int from = 1;
  int numbering = 0;

  if (movenum <= 99)
    fprintf(outfile, "defaultfont:=\"%s\";\n", fontstring);
  else {
    fprintf(outfile, "defaultfont:=\"%s\";\n", bignumberstring);
    bigfonts = 1;
  }

  if (!input) {
    perror("Cannot open sgf file");
    return 1;
  }
  
 reparse_untilstr:
  
  if (fromstr) {
    if (*fromstr > '0' && *fromstr <= '9') {
      from = atoi(fromstr);
      DEBUG("Loading from move %d\n", from);
    }
    else {
      from_n = *fromstr - 'A';
      if (*fromstr >= 'I')
	--from_n;

      from_m = board_size - atoi(fromstr+1);
      DEBUG("Loading from move at %d,%d (%m)\n", 
	    from_m, from_n, from_m, from_n);
    }
  }
  if (untilstr) {
    if (*untilstr > '0' && *untilstr <= '9') {
      until = atoi(untilstr);
      DEBUG("Loading until move %d\n", until);
    }
    else {
      untiln = *untilstr - 'A';
      if (*untilstr >= 'I')
	--untiln;
      untilm = board_size - atoi(untilstr+1);
      DEBUG("Loading until move at %d,%d (%m)\n", 
	    untilm, untiln, untilm, untiln);
    }
  }

  while ((c = getc(input)) != EOF) {
    if (0)
      gprintf("%c", c);

    switch (state) {

    case 0:
      if (c == ';') 
	state = 1;
      if (c == 'H') 
	state = 4;
      if (c == 'S') 
	state = 5;
      if (c == 'A') 
	state = 6;
      break;
      
    case 1:
      if ('A' <= c && c <= 'Z') {
	if (c == 'A') state = 6;
	else if (c == 'H') state = 4;
	else if (c == 'S') state = 5;
	else if (c == 'A') state = 6;
	else if (c == 'B' || c == 'W') {
	  if (c == 'B') color = BLACK;
	  if (c == 'W') color = WHITE;
	  state = 2;
	}
	else state = 3;
      }
      break;

    case 2:
      if (c == 'L') 
	state = 0;
      else if (c == '[') {
	n = getc(input)-'a';
	m = getc(input)-'a';
	getc(input);
	
	DEBUG("load_sgf : Adding [%c%c] = %m\n", 'a'+n, 'a'+m, m, n);
	if (!numbering
	    && movenum >= from || (m == from_m && n == from_n)) {
	  if (left_column <= 1)
	    ll = 0.;
	  else
	    ll = (float) left_column-1.5;
	  if (right_column >= board_size)
	    rr = (float) board_size-1.;
	  else
	    rr = right_column-0.5;
	  if (bottom_row <= 1)
	    bb = 0.;
	  else
	    bb = (float) bottom_row-1.5;
	  if (top_row >= board_size)
	    tt = (float) board_size-1.;
	  else
	    tt = top_row-0.5;

	  DEBUG("Move specified by from reached\n");
	  fprintf(outfile, "sx:=%.2f*17; sy:=%.2f*19;\n", scale, scale);
	  fprintf(outfile, PROLOG, VERSION);
	  /* mark hoshi points */
	  if (board_size == 19 || board_size == 13) {
	    fprintf(outfile, "pickup pencircle scaled 2.5;\n");
	    if (left_column-1 <= 3
		&& right_column-1 >= 3
		&& bottom_row-1 <= 3
		&& top_row-1 >= 3)
	      fprintf(outfile, "drawdot z0+3*z1+3*z2;\n");
	    if (left_column-1 <= 3
		&& right_column-1 >= 3
		&& bottom_row-1 <= 9
		&& top_row-1 >= 9)
	      fprintf(outfile, "drawdot z0+3*z1+9*z2;\n");
	    if (left_column-1 <= 3
		&& right_column-1 >= 3
		&& bottom_row-1 <= 15
		&& top_row-1 >= 15)
	      fprintf(outfile, "drawdot z0+3*z1+15*z2;\n");
	    if (left_column-1 <= 9
		&& right_column-1 >= 9
		&& bottom_row-1 <= 3
		&& top_row-1 >= 3)
	      fprintf(outfile, "drawdot z0+9*z1+3*z2;\n");
	    if (left_column-1 <= 9
		&& right_column-1 >= 9
		&& bottom_row-1 <= 9
		&& top_row-1 >= 9)
	      fprintf(outfile, "drawdot z0+9*z1+9*z2;\n");
	    if (left_column-1 <= 9
		&& right_column-1 >= 9
		&& bottom_row-1 <= 15
		&& top_row-1 >= 15)
	      fprintf(outfile, "drawdot z0+9*z1+15*z2;\n");
	    if (left_column-1 <= 15
		&& right_column-1 >= 15
		&& bottom_row-1 <= 3
		&& top_row-1 >= 3)
	      fprintf(outfile, "drawdot z0+15*z1+3*z2;\n");
	    if (left_column-1 <= 15
		&& right_column-1 >= 15
		&& bottom_row-1 <= 9
		&& top_row-1 >= 9)
	      fprintf(outfile, "drawdot z0+15*z1+9*z2;\n");
	    if (left_column-1 <= 15
		&& right_column-1 >= 15
		&& bottom_row-1 <= 15
		&& top_row-1 >= 15)
	      fprintf(outfile, "drawdot z0+15*z1+15*z2;\n");
	  }
	  fprintf(outfile, "pickup pencircle scaled 1;\n");
	  /* draw horizontal lines */
	  fprintf(outfile, "for i=%d upto %d:\n", bottom_row-1, top_row-1);
	  fprintf(outfile, "draw z0+%.1f*z1+i*z2--z0+%.1f*z1+i*z2;\nendfor;\n",
		  ll, rr);
	  /* draw vertical lines */
	  fprintf(outfile, "for i=%d upto %d:\n", left_column-1, right_column-1);
	  fprintf(outfile, "draw z0+%.1f*z2+i*z1--z0+%.1f*z2+i*z1;\nendfor;\n",
		  bb, tt);
	  /* draw edges */
	  fprintf(outfile, "pickup pencircle scaled 1.5;\n");
	  if (left_column <= 1)
	    fprintf(outfile, "draw z0+%.1f*z2--z0+%.1f*z2;\n", bb, tt);
	  if (right_column >= board_size)
	    fprintf(outfile, "draw z0+%.1f*z2+%d*z1--z0+%.1f*z2+%d*z1;\n", 
		    bb, board_size-1, tt, board_size-1);
	  if (bottom_row <= 1)
	    fprintf(outfile, "draw z0+%.1f*z1--z0+%.1f*z1;\n", ll, rr);
	  if (top_row >= board_size)
	    fprintf(outfile, "draw z0+%.1f*z1+%d*z2--z0+%.1f*z1+%d*z2;\n", 
		    ll, board_size-1, rr, board_size-1);
	  fprintf(outfile, "pickup pencircle scaled 1;\n");
	  
	  numbering = 1;
	  for (i = 0; i < board_size; i++)
	    for (j = 0; j < board_size; j++) {
	      if (i <= board_size-bottom_row 
		  && i >= board_size-top_row
		  && j <= right_column-1
		  && j >= left_column-1) {
		if (p[i][j] == BLACK) {
		  if (label[i][j] == -TRIANGLE) {
		    fprintf(outfile, "triangleblackstone(%d, %d);\n", 
			    j, board_size-1-i);
		    board_image[i][j] = -TRIANGLE;
		  }
		  else {
		    fprintf(outfile, "blackstone(%d, %d, \"\");\n", 
			    j, board_size-1-i);
		    board_image[i][j] = -BLACK;
		  }
		}
		else if (p[i][j] == WHITE) {
		  if (label[i][j] == -TRIANGLE) {
		    fprintf(outfile, "trianglewhitestone(%d, %d);\n", 
			    j, board_size-1-i);
		    board_image[i][j] = -TRIANGLE;
		  }
		  else {
		    fprintf(outfile, "whitestone(%d, %d, \"\");\n", 
			    j, board_size-1-i);
		    board_image[i][j] = -WHITE;
		  }
		}
	      }
	    }
	}
	if (n < board_size && m < board_size)
	  updateboard(m, n, color);
	if (numbering) {
	  if (m <= board_size-bottom_row 
	      && m >= board_size-top_row
	      && n <= right_column-1
	      && n >= left_column-1) {
	    if (board_image[m][n] != 0) {
	      if (board_image[m][n] > 0)
		gprintf("*** %d at %d\n", movenum, board_image[m][n]);
	      else {
		if (label[m][n] > 0)
		  gprintf("*** %d at %c\n", movenum, label[m][n]);
		else 
		  gprintf("*** %d at %m\n", movenum, m, n);
	      }
	    }
	    else {
	      board_image[m][n] = movenum;
	      if ((new_number > 99 || (new_number == 0 && movenum > 99))
		  && !bigfonts) {
		fprintf(outfile, "defaultfont:=\"%s\";\n", bignumberstring);
		bigfonts = 1;
	      }
	      if (p[m][n] == BLACK)
		fprintf(outfile, "blackstone(%d, %d, \"%d\");\n", 
			n, board_size-1-m, new_number ? new_number : movenum);
	      else if (p[m][n] == WHITE)
		fprintf(outfile, "whitestone(%d, %d, \"%d\");\n", 
			n, board_size-1-m, new_number ? new_number : movenum);
	    }
	  }
	  else {
	    if ((m == board_size && n == board_size)
		|| (m == 19 && n == 19))
	      fprintf(stderr, "*** PASS at %d\n", movenum);
	    else 
	      gprintf("*** %m outside limits of displayed board at move %d\n",
		      m, n, movenum);
	  }
	}
	if (movenum == until || (m == untilm && n == untiln)) {
	  DEBUG("Move specified by until reached\n");
	  goto the_end;
	}
	movenum++;
	if (new_number && numbering)
	  new_number++;
	state = 0;
      }
      else {
	fprintf(stderr,
		"analyze: error parsing sgf file - state = 2, (c = '%c'\n", c);
	return 1;
      }
      break;
      
    case 3:
      if (c == ']') state = 1;
      break;
      
    case 4:
      if (c == 'A') {
	if ((c = getc(input)) != '[') {
	  fprintf(stderr,"error parsing sgf file - state = 4, c = '%c'\n", c);
	  abort();
	}
	n = getc(input)-'0'; /* handicap game */
	DEBUG("load_sgf : Handicap %d\n", n);
	sethand(n);
      }
      state = 0;
      break;

    case 5:
      if (c == 'Z') {
	if ((c = getc(input)) != '[') {
	  fprintf(stderr,"error parsing sgf file - state = 4, c = '%c'\n", c);
	  abort();
	}
	n = getc(input)-'0';
	if (n == 1)
	  n = 10 + getc(input)-'0';
	board_size = n;
	DEBUG("load_sgf : Board size %d\n", n);
	/* an "until" move was parsed assuming board size 19. Reparse */
	if (top_row == 19)
	  top_row = board_size;
	if (right_column == 19)
	  right_column = board_size;

	state = 3;
	goto reparse_untilstr;  /* crude, but effective ! */
      }
      state = 0;
      break;

    case 6:
      if (c == 'B') {
	color = BLACK;
	state = 7;
      }
      else if (c == 'W') {
	color = WHITE;
	state = 7;
      }
      else state = 1;
      break;

    case 7:
      if ('A' <= c && c <= 'Z') {
	if (c == 'A')
	  state = 6;
	else
	  state = 3;
      }
      else if (c == ';')
	state = 1;
      else if (c == '[')
	state = 8;
      else state = 0;
      break;
      
    case 8:
      n = c-'a';
      m = getc(input)-'a';
	
      DEBUG("load_sgf : Adding [%c%c] = %m\n", 'a'+n, 'a'+m, m, n);
      p[m][n] = color;
      c = getc(input);
      c = getc(input);
      if (c == '[')
	state = 8;
      else if (c == ';')
	state = 1;
      else state = 0;
      break;
    }
  }

 the_end:
  /* Now print the labels */
  fprintf(outfile, "defaultfont:=\"%s\";\n", italfontstring);
  for (m = 0; m < board_size; m++)
    for (n = 0; n < board_size; n++)
      if (label[m][n] > 0) {
	if (board_image[m][n] > 0)
	  gprintf("*** attempt to label a numbered position at %m\n", m, n);
	else {
	  if (board_image[m][n] == -BLACK) {
	    DEBUG("label %c at (%d,%d)\n", label[m][n], m, n);
	    fprintf(outfile, 
		    "label (\"%c\", %d*z1+%d*z2) withcolor white;\n",
		    label[m][n], n, board_size-1-m);
	  }
	  else {
	    DEBUG("label %c at (%d,%d)\n", label[m][n], m, n);
	    if (board_image[m][n] != -WHITE)
	      fprintf(outfile, "fill a shifted (%d*z1+%d*z2) withcolor white;\n",
		      n, board_size-1-m);
	    fprintf(outfile, "label (\"%c\", %d*z1+%d*z2);\n",
		    label[m][n], n, board_size-1-m);
	  }
	}
      }
  fprintf(outfile, "endfig;\nend;\n");
  DEBUG("End of load_sgf\n\n\n");
  return 0;
}