File: joseki.c

package info (click to toggle)
gnugo-dv 3.1.27-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,724 kB
  • ctags: 3,307
  • sloc: ansic: 46,051; perl: 1,303; sh: 710; makefile: 578; tcl: 401; lisp: 193
file content (419 lines) | stat: -rw-r--r-- 10,807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 * This is GNU GO, a Go program. Contact gnugo@gnu.org, or see   *
 * http://www.gnu.org/software/gnugo/ for more information.      *
 *                                                               *
 * Copyright 1999, 2000, 2001 by the Free Software Foundation.   *
 *                                                               *
 * This program is free software; you can redistribute it and/or *
 * modify it under the terms of the GNU General Public License   *
 * as published by the Free Software Foundation - version 2.     *
 *                                                               *
 * This program is distributed in the hope that it will be       *
 * useful, but WITHOUT ANY WARRANTY; without even the implied    *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       *
 * PURPOSE.  See the GNU General Public License in file COPYING  *
 * for more details.                                             *
 *                                                               *
 * You should have received a copy of the GNU General Public     *
 * License along with this program; if not, write to the Free    *
 * Software Foundation, Inc., 59 Temple Place - Suite 330,       *
 * Boston, MA 02111, USA.                                        *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* Convert joseki from sgf format to patterns.db format. */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>

#include "../sgf/sgftree.h"
#include "liberty.h"

#ifndef VARIADIC_MACROS
int trace_dummy;
#endif

#define USAGE "\
Usage : joseki prefix filename\n\
"

/* Joseki move types. */
#define STANDARD  0
#define URGENT    1
#define MINOR     2
#define TRICK     3
#define ANTISUJI  4
#define TENUKI_OK 5

/* Identify the type of joseki move.
 * FIXME: We might want the relax the requirement that this info comes
 *        as the very first character.
 */
static int
identify_move_type(char *text)
{
  if (!text)
    return STANDARD;
  
  switch ((int) *text) {
  case 'u':
  case 'U':
    return URGENT;
    break;
  case 'J':
  case 'S':
    return STANDARD;
    break;
  case 'j':
  case 's':
    return MINOR;
    break;
  case 'T':
    return TRICK;
    break;
  case 't':
    return TENUKI_OK;
    break;
  case '0':
  case 'a':
  case 'A':
    return ANTISUJI;
    break;
  }

  return STANDARD;
}

/* Copy the lines starting with a certain character to stdout. */
static void
write_selected_lines(char *text, char start_char)
{
  char *p;
  if (!text)
    return;
  while (1) {
    p = strchr(text, '\n');
    if (p)
      *p = 0;
    if (*text == start_char)
      printf("%s\n", text);
    if (p) {
      *p = '\n';
      text = p+1;
    }
    else
      break;
  }
}

/* Is there any line starting with a certain character? */
static int
selected_line_exists(char *text, char start_char)
{
  char *p;
  if (!text)
    return 0;
  while (1) {
    if (*text == start_char)
      return 1;
    p = strchr(text, '\n');
    if (p)
      text = p+1;
    else
      break;
  }
  return 0;
}

/* Write the main diagram or the constraint diagram. In the former
 * case, pass a NULL pointer for labels.
 */
static void
write_diagram(int movei, int movej, int color, int marki, int markj,
	      char labels[MAX_BOARD][MAX_BOARD])
{
  int i, j;
  
  for (i = -1; i <= marki; i++) {
    for (j = markj; j < board_size; j++) {
      if (i == -1)
	printf("-");
      else if (labels && labels[i][j])
	printf("%c", labels[i][j]);
      else if (i == movei && j == movej)
	printf("*");
      else if (BOARD(i, j) == color)
	printf("O");
      else if (BOARD(i, j) == OTHER_COLOR(color))
	printf("X");
      else
	printf(".");
    }
    if (i == -1)
      printf("+\n");
    else
      printf("|\n");
  }
}

/* Write the colon line of the pattern. */
static void
write_colon_line(int move_type, char *text)
{
  char *p;
  
  /* Locate a possible colon line in the sgf file comment. */
  if (!text)
    p = NULL;
  else if (*text == ':')
    p = text + 1;
  else {
    p = strstr(text, "\n:");
    if (p)
      p += 2;
  }

  printf(":8,sF");
  switch (move_type) {
  case URGENT:
    printf("U");
    break;
  case STANDARD:
    printf("J");
    break;
  case MINOR:
    printf("j");
    break;
  case TRICK:
    printf("T");
    break;
  case TENUKI_OK:
    printf("t");
    break;
  /* Antisuji moves handled elsewhere. */
  }

  if (p) {
    /* A little trick to guess whether the supplied colon line in the
     * sgf file begins with a classification.
     */
    if (strchr(p, '(')
	&& (!strchr(p, ',') || strchr(p, ',') > strchr(p, '(')))
      printf(",");
    while (*p != 0 && *p != '\n')
      fputc(*(p++), stdout);
  }
  printf("\n");
}


/* Write a pattern to stdout. */
static void
make_pattern(int movei, int movej, int color,
	     int marki, int markj, int multiple_marks,
	     char labels[MAX_BOARD][MAX_BOARD], char *text,
	     const char *prefix)
{
  static int pattern_number = 0;
  int move_type;
  
  pattern_number++;
  move_type = identify_move_type(text);
  
  printf("Pattern %s%d\n", prefix, pattern_number);

  /* Write comments. */
  write_selected_lines(text, '#');
  printf("\n");
  
  /* Write the main diagram. */
  write_diagram(movei, movej, color, marki, markj, NULL);
  printf("\n");

  /* Write the colon line. */
  write_colon_line(move_type, text);
  printf("\n");
  
  /* Write the constraint diagram if there are any labels, a
   * constraint line, or an action line.
   */
  if (labels
      || selected_line_exists(text, ';')
      || selected_line_exists(text, '>')) {
    write_diagram(movei, movej, color, marki, markj, labels);

    printf("\n");

    /* Write constraint and action lines. */
    write_selected_lines(text, ';');
    write_selected_lines(text, '>');
    if (move_type == ANTISUJI)
      printf(">antisuji(*);\n");
    printf("\n");
  }
  else if (move_type == ANTISUJI)
    printf(">antisuji(*);\n\n");

  printf("\n");
  
  /* Basic sanity checking. We do this at the end to simplify debugging. */
  if (multiple_marks)
    fprintf(stderr, "Warning: Multiple square marks in pattern %s%d\n",
	    prefix, pattern_number);

  if (is_suicide(POS(movei, movej), color)) {
    fprintf(stderr, "Error: Illegal move in pattern %s%d\n",
	    prefix, pattern_number);
    exit(EXIT_FAILURE);
  }  
}


/* Analyze the node properties in order to make a pattern. Then make
 * recursive calls for child node and siblings.
 */
static void
analyze_node(SGFNode *node, const char *prefix)
{
  SGFProperty *prop;
  int i, j;
  char labels[MAX_BOARD][MAX_BOARD];
  int label_found = 0;
  int movei = -1;
  int movej = -1;
  int color = EMPTY;
  int marki = -1;
  int markj = -1;
  int multiple_marks = 0;
  char *comment = NULL;

  /* Clear the labels array. */
  memset(labels, 0, MAX_BOARD * MAX_BOARD);
  
  /* Check the node properties for a move, a square mark, labels, and
   * a comment.
   */
  for (prop = node->props; prop; prop = prop->next) {
    switch (prop->name) {
    case SGFSQ: /* Square */
    case SGFMA: /* Mark */
      if (marki != -1)
	multiple_marks = 1;
      else
	get_moveXY(prop, &marki, &markj, board_size);
      break;
      
    case SGFW: /* White move */
      color = WHITE;
      get_moveXY(prop, &movei, &movej, board_size);
      break;
      
    case SGFB: /* Black move */
      color = BLACK;
      get_moveXY(prop, &movei, &movej, board_size);
      break;

    case SGFLB: /* Label, with value like "mh:A" */
      get_moveXY(prop, &i, &j, board_size);
      assert(i >= 0 && i < board_size && j >= 0 && j < board_size);
      assert(prop->value[2] == ':');
      labels[i][j] = prop->value[3];
      label_found = 1;
      break;

    case SGFC: /* Comment */
      comment = prop->value;
      break;
    }
  }

  /* If we have a move and a square mark, produce a pattern. */
  if (movei != -1 && marki != -1)
    make_pattern(movei, movej, color, marki, markj, multiple_marks,
		 (label_found ? labels : NULL), comment, prefix);

  /* Traverse child, if any. */
  if (node->child) {
    if (movei != -1)
      tryko(POS(movei, movej), color, NULL, EMPTY, NO_MOVE);
    analyze_node(node->child, prefix);
    if (movei != -1)
      popgo();
  }

  /* Traverse sibling, if any. */
  if (node->next)
    analyze_node(node->next, prefix);
}

int
main(int argc, char *argv[])
{
  const char *filename;
  const char *prefix;
  SGFNode *sgf;

  /* Check number of arguments. */
  if (argc != 3) {
    fprintf(stderr, USAGE);
    exit(EXIT_FAILURE);
  }

  prefix = argv[1];
  filename = argv[2];

  /* Read the sgf file into a tree in memory. */
  sgf = readsgffile(filename);
  if (!sgf) {
    fprintf(stderr, "%s: Couldn't open sgf file %s.\n", argv[0], filename);
    exit(EXIT_FAILURE);
  }

#define PREAMBLE "\
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #\n\
# This is GNU GO, a Go program. Contact gnugo@gnu.org, or see   #\n\
# http://www.gnu.org/software/gnugo/ for more information.      #\n\
#                                                               #\n\
# Copyright 1999, 2000, 2001 by the Free Software Foundation.   #\n\
#                                                               #\n\
# This program is free software; you can redistribute it and/or #\n\
# modify it under the terms of the GNU General Public License   #\n\
# as published by the Free Software Foundation - version 2.     #\n\
#                                                               #\n\
# This program is distributed in the hope that it will be       #\n\
# useful, but WITHOUT ANY WARRANTY; without even the implied    #\n\
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       #\n\
# PURPOSE.  See the GNU General Public License in file COPYING  #\n\
# for more details.                                             #\n\
#                                                               #\n\
# You should have received a copy of the GNU General Public     #\n\
# License along with this program; if not, write to the Free    #\n\
# Software Foundation, Inc., 59 Temple Place - Suite 330,       #\n\
# Boston, MA 02111, USA.                                        #\n\
#                                                               #\n\
# This file is automatically generated by joseki. Do not edit   #\n\
# It directly. Instead, edit the corresponding database.        #\n\
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #\n\
\n\n\
"

  printf(PREAMBLE);

  /* Call the engine to setup and clear the board. */
  clear_board();
  
  /* Walk through the tree and make patterns. */
  analyze_node(sgf, prefix);
    
  return 0;
}

      
/*
 * Local Variables:
 * tab-width: 8
 * c-basic-offset: 2
 * End:
 */