Games-Go-Sgf2Dg

 view release on metacpan or  search on metacpan

sgf2mpost/sgf2mpost.c  view on Meta::CPAN

      
    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;
}


/*
 * Place a "color" on the board at i,j, and remove
 * any captured stones.
 */

void updateboard(int i, int j, int color)
{
  int other = OTHER_COLOR(color);

  assert(i >= 0 && i < board_size && j >= 0 && j < board_size);
  if (p[i][j] != EMPTY) {
    gprintf("Stone overlay problem at %m!\n", i, j);
    gprintf("Try reducing the move range with -s and -e.\n");
    abort();
  }

  p[i][j] = color;

  if (1)
    DEBUG("Update board : %m = %d\n", i,j, color);

  if (i > 0 && p[i-1][j] == other)
    check_for_capture(i-1, j, other);
  if (i < board_size-1 && p[i+1][j] == other)
    check_for_capture(i+1, j, other);
  if (j > 0 && p[i][j-1] == other)
    check_for_capture(i, j-1, other);
  if (j < board_size-1 && p[i][j+1] == other)
    check_for_capture(i, j+1, other);

}

/* if string at m,n has no liberties, remove it from
 * the board. Return the number of stones captured.
 */

int check_for_capture(int m, int n, int color)
{
  char mx[MAX_BOARD][MAX_BOARD];
  int i,j;
  int finished = 0;
  int captured = 0;

  assert(p[m][n] == color);
  memset(mx, 0, sizeof(mx));
  
  /* mark the string: look for unmarked elements. We are
   * finished when we can't find any. If we notice a liberty,
   */
  mx[m][n] = 1;
  finished = 0;
  while (!finished) {
    finished = 1;
    for (i = 0; i < board_size; ++i)
      for (j = 0; j < board_size; ++j)
	if (p[i][j] == color && !mx[i][j])
	  if (i > 0 && mx[i-1][j]
	      || i < board_size-1 && mx[i+1][j]
	      || j > 0 && mx[i][j-1]
	      || j < board_size-1 && mx[i][j+1]) {
	    finished = 0;
	    mx[i][j] = 1;
	  }
  }
  /* Now see if there's a liberty. */
  for (i = 0; i < board_size; ++i)
    for (j = 0; j < board_size; ++j)
      if (p[i][j] == EMPTY)
	if (i > 0 && mx[i-1][j]
	    || i < board_size-1 && mx[i+1][j]
	    || j > 0 && mx[i][j-1]
	    || j < board_size-1 && mx[i][j+1])



( run in 1.408 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )