| 1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ |
| 2 | * This is GNU Go, a Go program. Contact gnugo@gnu.org, or see * |
| 3 | * http://www.gnu.org/software/gnugo/ for more information. * |
| 4 | * * |
| 5 | * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, * |
| 6 | * 2008 and 2009 by the Free Software Foundation. * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or * |
| 9 | * modify it under the terms of the GNU General Public License as * |
| 10 | * published by the Free Software Foundation - version 3 or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License in file COPYING for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public * |
| 19 | * License along with this program; if not, write to the Free * |
| 20 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * |
| 21 | * Boston, MA 02111, USA. * |
| 22 | \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 23 | |
| 24 | #ifndef _SGFTREE_H_ |
| 25 | #define _SGFTREE_H_ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | #include "sgf_properties.h" |
| 30 | |
| 31 | |
| 32 | #ifndef _BOARD_H_ |
| 33 | /* |
| 34 | * NOTE: These MUST coincide with the definitions for the engine that we |
| 35 | * are using. In this case they are defined in engine/gnugo.h. |
| 36 | * |
| 37 | * The reason that we put them here within the #ifndef clause is because |
| 38 | * we want to decouple the GNU Go engine from SGF library, but we don't |
| 39 | * want to redefine these symbols if we include this file into board.h. |
| 40 | */ |
| 41 | |
| 42 | #define EMPTY 0 |
| 43 | #define WHITE 1 |
| 44 | #define BLACK 2 |
| 45 | #endif |
| 46 | |
| 47 | |
| 48 | void *xalloc(unsigned int); |
| 49 | |
| 50 | /* |
| 51 | * A property of an SGF node. An SGF node is described by a linked |
| 52 | * list of these. |
| 53 | */ |
| 54 | |
| 55 | typedef struct SGFProperty_t { |
| 56 | struct SGFProperty_t *next; |
| 57 | short name; |
| 58 | char *value; |
| 59 | } SGFProperty; |
| 60 | |
| 61 | |
| 62 | typedef struct SGFNode_t { |
| 63 | SGFProperty *props; |
| 64 | struct SGFNode_t *parent; |
| 65 | struct SGFNode_t *child; |
| 66 | struct SGFNode_t *next; |
| 67 | } SGFNode; |
| 68 | |
| 69 | |
| 70 | /* low level functions */ |
| 71 | SGFNode *sgfPrev(SGFNode *node); |
| 72 | SGFNode *sgfRoot(SGFNode *node); |
| 73 | SGFNode *sgfNewNode(void); |
| 74 | void sgfFreeNode(SGFNode *node); |
| 75 | |
| 76 | int sgfGetIntProperty(SGFNode *node, const char *name, int *value); |
| 77 | int sgfGetFloatProperty(SGFNode *node, const char *name, float *value); |
| 78 | int sgfGetCharProperty(SGFNode *node, const char *name, char **value); |
| 79 | void sgfAddProperty(SGFNode *node, const char *name, const char *value); |
| 80 | void sgfAddPropertyInt(SGFNode *node, const char *name, long val); |
| 81 | void sgfAddPropertyFloat(SGFNode *node, const char *name, float val); |
| 82 | void sgfOverwriteProperty(SGFNode *node, const char *name, const char *text); |
| 83 | void sgfOverwritePropertyFloat(SGFNode *node, const char *name, float val); |
| 84 | void sgfOverwritePropertyInt(SGFNode *node, const char *name, int val); |
| 85 | void *xrealloc(void *pt, unsigned int size); |
| 86 | SGFProperty *sgfMkProperty(const char *name, const char *value, |
| 87 | SGFNode *node, SGFProperty *last); |
| 88 | void sgfFreeProperty(SGFProperty *prop); |
| 89 | |
| 90 | SGFNode *sgfAddStone(SGFNode *node, int color, int movex, int movey); |
| 91 | SGFNode *sgfAddPlay(SGFNode *node, int who, int movex, int movey); |
| 92 | SGFNode *sgfAddPlayLast(SGFNode *node, int who, int movex, int movey); |
| 93 | |
| 94 | void sgfWriteResult(SGFNode *node, float score, int overwrite); |
| 95 | void sgf_write_header(SGFNode *root, int overwrite, int seed, float komi, |
| 96 | int handicap, int level, int rules); |
| 97 | |
| 98 | SGFNode *sgfLabel(SGFNode *node, const char *label, int i, int j); |
| 99 | SGFNode *sgfLabelInt(SGFNode *node, int num, int i, int j); |
| 100 | SGFNode *sgfCircle(SGFNode *node, int i, int j); |
| 101 | SGFNode *sgfSquare(SGFNode *node, int i, int j); |
| 102 | SGFNode *sgfTriangle(SGFNode *node, int i, int j); |
| 103 | SGFNode *sgfMark(SGFNode *node, int i, int j); |
| 104 | SGFNode *sgfAddComment(SGFNode *node, const char *comment); |
| 105 | SGFNode *sgfBoardText(SGFNode *node, int i, int j, const char *text); |
| 106 | SGFNode *sgfBoardChar(SGFNode *node, int i, int j, char c); |
| 107 | SGFNode *sgfBoardNumber(SGFNode *node, int i, int j, int number); |
| 108 | SGFNode *sgfStartVariant(SGFNode *node); |
| 109 | SGFNode *sgfStartVariantFirst(SGFNode *node); |
| 110 | SGFNode *sgfAddChild(SGFNode *node); |
| 111 | |
| 112 | SGFNode *sgfCreateHeaderNode(int boardsize, float komi, int handicap); |
| 113 | |
| 114 | /* Read SGF tree from file. */ |
| 115 | SGFNode *readsgffile(const char *filename); |
| 116 | /* Specific solution for fuseki */ |
| 117 | SGFNode *readsgffilefuseki(const char *filename, int moves_per_game); |
| 118 | |
| 119 | /* Write SGF tree to a file. */ |
| 120 | int writesgf(SGFNode *root, const char *filename); |
| 121 | |
| 122 | |
| 123 | /* ---------------------------------------------------------------- */ |
| 124 | /* --- SGFTree --- */ |
| 125 | /* ---------------------------------------------------------------- */ |
| 126 | |
| 127 | |
| 128 | typedef struct SGFTree_t { |
| 129 | SGFNode *root; |
| 130 | SGFNode *lastnode; |
| 131 | } SGFTree; |
| 132 | |
| 133 | |
| 134 | void sgftree_clear(SGFTree *tree); |
| 135 | int sgftree_readfile(SGFTree *tree, const char *infilename); |
| 136 | |
| 137 | int sgftreeBack(SGFTree *tree); |
| 138 | int sgftreeForward(SGFTree *tree); |
| 139 | |
| 140 | void sgftreeAddPlay(SGFTree *tree, int color, int movex, int movey); |
| 141 | void sgftreeAddPlayLast(SGFTree *tree, int color, int movex, int movey); |
| 142 | void sgftreeAddStone(SGFTree *tree, int color, int movex, int movey); |
| 143 | void sgftreeWriteResult(SGFTree *tree, float score, int overwrite); |
| 144 | SGFNode *sgftreeNodeCheck(SGFTree *tree); |
| 145 | |
| 146 | void sgftreeCircle(SGFTree *tree, int i, int j); |
| 147 | void sgftreeSquare(SGFTree *tree, int i, int j); |
| 148 | void sgftreeTriangle(SGFTree *tree, int i, int j); |
| 149 | void sgftreeMark(SGFTree *tree, int i, int j); |
| 150 | void sgftreeAddComment(SGFTree *tree, const char *comment); |
| 151 | void sgftreeBoardText(SGFTree *tree, int i, int j, const char *text); |
| 152 | void sgftreeBoardChar(SGFTree *tree, int i, int j, char c); |
| 153 | void sgftreeBoardNumber(SGFTree *tree, int i, int j, int number); |
| 154 | void sgftreeStartVariant(SGFTree *tree); |
| 155 | void sgftreeStartVariantFirst(SGFTree *tree); |
| 156 | void sgftreeCreateHeaderNode(SGFTree *tree, int boardsize, float komi, |
| 157 | int handicap); |
| 158 | void sgftreeSetLastNode(SGFTree *tree, SGFNode *lastnode); |
| 159 | |
| 160 | |
| 161 | /* ---------------------------------------------------------------- */ |
| 162 | /* --- Utilities --- */ |
| 163 | /* ---------------------------------------------------------------- */ |
| 164 | |
| 165 | |
| 166 | int get_moveX(SGFProperty *property, int boardsize); |
| 167 | int get_moveY(SGFProperty *property, int boardsize); |
| 168 | int get_moveXY(SGFProperty *property, int *i, int *j, int boardsize); |
| 169 | |
| 170 | int show_sgf_properties(SGFNode *node); |
| 171 | int show_sgf_tree(SGFNode *node); |
| 172 | int is_markup_node(SGFNode *node); |
| 173 | int is_move_node(SGFNode *node); |
| 174 | int is_pass_node(SGFNode *node, int boardsize); |
| 175 | int find_move(SGFNode *node); |
| 176 | |
| 177 | |
| 178 | #endif |
| 179 | |