File: sgf_utils.c

package info (click to toggle)
gnugo 3.8-13
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,656 kB
  • sloc: ansic: 56,447; perl: 3,771; lisp: 2,804; sh: 722; python: 682; makefile: 653; awk: 113; sed: 22
file content (288 lines) | stat: -rw-r--r-- 6,015 bytes parent folder | download | duplicates (6)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 * 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, 2002, 2003, 2004, 2005, 2006, 2007,   *
 * 2008 and 2009 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 - either version 3      *
 * or (at your option) any later version.                            *
 *                                                                   *
 * 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., 51 Franklin Street, Fifth Floor,       *
 * Boston, MA 02111, USA.                                            *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

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

#include "sgftree.h"

/* 
 * Return the integer X move.
 */

int
get_moveX(SGFProperty *property, int boardsize)
{
  int i;
  if (strlen(property->value) < 2)
    return -1;

  i = toupper((int) property->value[1]) - 'A';
  if (i >= boardsize)
    return -1;

  return i;
}

/* 
 * Return the integer Y move.
 */

int
get_moveY(SGFProperty *property, int boardsize)
{
  int j;
  if (strlen(property->value) < 2)
    return -1;

  j = toupper((int) property->value[0]) - 'A';
  if (j >= boardsize)
    return -1;

  return j;
}


/* Fills (*i, *j) from the property value, in GNU Go co-ords.
 * Note that GNU Go uses different conventions from sgf for
 * co-ordinates been called. 
 *
 * Returns 1 for a move, 0 for a pass.
 */

int
get_moveXY(SGFProperty *property, int *i, int *j, int boardsize)
{
  *i = get_moveX(property, boardsize);
  *j = get_moveY(property, boardsize);
  
  if (*i == -1 && *j == -1)
    return 0;

  return 1;
}


/* 
 * Debugging function to print properties as they are traversed.
 */

int
show_sgf_properties(SGFNode *node)
{
  SGFProperty *sgf_prop;
  int propcount;

  propcount = 0;

  printf("P: ");
  if (!node->props) {
    printf("None\n");
    return propcount;
  }
  else {
    sgf_prop = node->props;
    while (sgf_prop) {
      printf("%c%c ", sgf_prop->name & 0x00FF, (sgf_prop->name & 0xFF00)>>8);
      sgf_prop = sgf_prop->next;
      propcount++;
    }

    printf("(%d) ", propcount);
    if (node->next)
      printf("n");
    if (node->child)
      printf("c");
    printf("\n");
  }

  return propcount;
}


/*
 * Recursively traverse each node showing all properties.
 */

int
show_sgf_tree(SGFNode *node)
{
  int n = 0; /* number of nodes */
  
  n++;
  show_sgf_properties(node);

  /* must search depth first- siblings are equal! */
  if (node->child)
    n += show_sgf_tree(node->child);

  if (node->next)
    n += show_sgf_tree(node->next);
  
  return n;
}


/*
 * Determine if a node has a mark property in it.
 */

int
is_markup_node(SGFNode *node)
{
  SGFProperty *sgf_prop;
  
  /* If the node has no properties, there's nothing to do.
     This should have been checked by the caller, but it can't hurt. */
  if (!node->props)
    return 0;

  sgf_prop = node->props;
  while (sgf_prop) {
    switch (sgf_prop->name) {
    case SGFCR: 
    case SGFSQ: /* Square */
    case SGFTR: /* Triangle */
    case SGFMA: /* Mark */
    case SGFBM: /* bad move */
    case SGFDO: /* doubtful move */
    case SGFIT: /* interesting move */
    case SGFTE: /* good move */
      return 1;
      break;
    default:
      break;
    }
    sgf_prop = sgf_prop->next;
  }

  /* No markup property found. */
  return 0;
}


/*
 * Determine if the node has a move in it.
 */

int
is_move_node(SGFNode *node)
{
  SGFProperty *sgf_prop;
  
  /* If the node has no properties, there's nothing to do.
     This should have been checked by the caller, but it can't hurt. */
  if (!node->props)
    return 0;

  sgf_prop = node->props;
  while (sgf_prop) {
    switch (sgf_prop->name) {
    case SGFB: 
    case SGFW: 
      return 1;
      break;
    default:
      break;
    }
    sgf_prop = sgf_prop->next;
  }

  return 0;
}


/*
 * Determine if the node has a pass move in it.
 */

int
is_pass_node(SGFNode *node, int boardsize)
{
  SGFProperty *sgf_prop;
  int i, j;
  
  /* If the node has no properties, there's nothing to do.
     This should have been checked by the caller, but it can't hurt. */
  if (!node->props)
    return 0;

  sgf_prop = node->props;
  while (sgf_prop) {
    switch (sgf_prop->name) {
    case SGFB: 
    case SGFW: 
      return !get_moveXY(sgf_prop, &i, &j, boardsize);
      break;
    default:
      break;
    }
    sgf_prop = sgf_prop->next;
  }

  return 0;
}


/*
 * Determine whose move is in the node.
 */

int
find_move(SGFNode *node)
{
  SGFProperty *sgf_prop;
  
  /* If the node has no properties, there's nothing to do.
     This should have been checked by the caller, but it can't hurt. */
  if (!node->props)
    return 0;

  sgf_prop = node->props;
  while (sgf_prop) {
    switch (sgf_prop->name) {
    case SGFB: 
      return BLACK;
      break;
    case SGFW: 
      return WHITE;
      break;
    default:
      break;
    }
    sgf_prop = sgf_prop->next;
  }

  return EMPTY;
}


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