[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnugo-devel] sgf cleaning continued
From: |
Paul Pogonyshev |
Subject: |
[gnugo-devel] sgf cleaning continued |
Date: |
Wed, 11 Sep 2002 20:44:05 +0300 |
changes:
- fixed a bug in gunnar_3_9.3 (see sgftreeStartVariantFirst()),
changed some comments ;)
- gameinfo_play_sgftree_rot() now updates lastnode field. some modes
(at least ascii and load and analyze) work better now: when gnu go
is launched with -L option they add variations at proper places.
- changed parameter of gameinfo_play_sgftree_rot() from SGFNode * to
SGFTree * (tree root is always passed anyway). caused some changes
in play_gtp.c and debugboard/main.c.
- two new functions: sgftreeBack() and sgftreeForward() for
navigating through trees with lastnode field.
- play_ascii.c mode now uses sgftree.. functions instead of sgf..
ones (as in Gunnar's latest patch). so, no more curnode variable,
uses lastnode field instead.
- a couple of other minor changes in play_ascii.c (killed
ascii2pos(), changed parameters of init_sgf()).
Paul
Index: sgf/sgftree.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/sgf/sgftree.h,v
retrieving revision 1.11
diff -u -r1.11 sgftree.h
--- sgf/sgftree.h 10 Sep 2002 20:06:02 -0000 1.11
+++ sgf/sgftree.h 11 Sep 2002 17:29:39 -0000
@@ -134,6 +134,9 @@
void sgftree_clear(SGFTree *tree);
int sgftree_readfile(SGFTree *tree, const char *infilename);
+int sgftreeBack(SGFTree *tree);
+int sgftreeForward(SGFTree *tree);
+
void sgftreeAddPlay(SGFTree *tree, int color, int movex, int movey);
void sgftreeAddPlayLast(SGFTree *tree, int color, int movex, int movey);
void sgftreeAddStone(SGFTree *tree, int color, int movex, int movey);
Index: sgf/sgftree.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/sgf/sgftree.c,v
retrieving revision 1.10
diff -u -r1.10 sgftree.c
--- sgf/sgftree.c 10 Sep 2002 20:06:02 -0000 1.10
+++ sgf/sgftree.c 11 Sep 2002 17:32:05 -0000
@@ -37,14 +37,51 @@
SGFNode *savetree = tree->root;
tree->root = readsgffile(infilename);
- tree->lastnode = NULL;
if (tree->root == NULL) {
tree->root = savetree;
return 0;
}
+ tree->lastnode = NULL;
+ return 1;
+}
+
+
+/* Go back one node in the tree. If lastnode is NULL, go to the last
+ node (the one in main variant which has no children) */
+
+int
+sgftreeBack(SGFTree *tree)
+{
+ if (tree->lastnode) {
+ if (tree->lastnode->parent)
+ tree->lastnode = tree->lastnode->parent;
+ else
+ return 0;
+ }
+ else
+ while (sgftreeForward(tree));
return 1;
}
+
+/* Go forward one node in the tree. If lastnode is NULL, go to the
+ tree root */
+
+int
+sgftreeForward(SGFTree *tree)
+{
+ if (tree->lastnode) {
+ if (tree->lastnode->child)
+ tree->lastnode = tree->lastnode->child;
+ else
+ return 0;
+ }
+ else
+ tree->lastnode = tree->root;
+ return 1;
+}
+
+
/* ================================================================ */
/* High level functions */
/* ================================================================ */
@@ -54,6 +91,8 @@
* except if lastnode is NULL, then the current end of game is used.
*/
+/* FIXME: do we need node parameter here? it is never used. */
+
SGFNode *
sgftreeNodeCheck(SGFTree *tree, SGFNode *node)
{
@@ -228,7 +267,7 @@
/*
- * Start a new variant. Returns a pointer to the new node.
+ * Start a new variant.
*/
void
@@ -240,13 +279,13 @@
/*
- * Start a new variant as first child. Returns a pointer to the new node.
+ * Start a new variant as first child.
*/
void
sgftreeStartVariantFirst(SGFTree *tree)
{
- SGFNode *node = sgftreeNodeCheck(tree, node);
+ SGFNode *node = sgftreeNodeCheck(tree, NULL);
tree->lastnode = sgfStartVariantFirst(node);
}
Index: interface/play_gtp.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_gtp.c,v
retrieving revision 1.87
diff -u -r1.87 play_gtp.c
--- interface/play_gtp.c 9 Sep 2002 21:20:11 -0000 1.87
+++ interface/play_gtp.c 11 Sep 2002 17:32:10 -0000
@@ -618,7 +618,7 @@
{
char filename[GTP_BUFSIZE];
char untilstring[GTP_BUFSIZE];
- SGFNode *sgf;
+ SGFTree sgftree;
Gameinfo gameinfo;
int nread;
int color_to_move;
@@ -627,24 +627,24 @@
if (nread < 1)
return gtp_failure("missing filename");
- if ((sgf = readsgffile(filename)) == NULL)
+ if (!sgftree_readfile(&sgftree, filename))
return gtp_failure("cannot open or parse '%s'", filename);
gameinfo_clear(&gameinfo, 19, 5.5); /* Probably unnecessary. */
- gameinfo_load_sgfheader(&gameinfo, sgf);
+ gameinfo_load_sgfheader(&gameinfo, sgftree.root);
if (nread == 1)
- color_to_move = gameinfo_play_sgftree_rot(&gameinfo, sgf, NULL,
+ color_to_move = gameinfo_play_sgftree_rot(&gameinfo, &sgftree, NULL,
gtp_orientation);
else
- color_to_move = gameinfo_play_sgftree_rot(&gameinfo, sgf, untilstring,
+ color_to_move = gameinfo_play_sgftree_rot(&gameinfo, &sgftree, untilstring,
gtp_orientation);
handicap = gameinfo.handicap;
gtp_internal_set_boardsize(board_size);
reset_engine();
- sgfFreeNode(sgf);
+ sgfFreeNode(sgftree.root);
gtp_start_response(GTP_SUCCESS);
gtp_mprintf("%C", color_to_move);
Index: interface/play_gmp.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_gmp.c,v
retrieving revision 1.11
diff -u -r1.11 play_gmp.c
--- interface/play_gmp.c 10 Sep 2002 20:06:02 -0000 1.11
+++ interface/play_gmp.c 11 Sep 2002 17:34:58 -0000
@@ -145,8 +145,7 @@
break;
}
sgftreeAddComment(&sgftree, "undone");
- /* FIXME: Add a function in sgftree.c to do this. */
- sgftree.lastnode = sgftree.lastnode->parent;
+ sgftreeBack(&sgftree);
to_move = OTHER_COLOR(to_move);
}
continue;
Index: interface/play_ascii.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_ascii.c,v
retrieving revision 1.25
diff -u -r1.25 play_ascii.c
--- interface/play_ascii.c 10 Sep 2002 20:06:02 -0000 1.25
+++ interface/play_ascii.c 11 Sep 2002 17:35:00 -0000
@@ -47,7 +47,6 @@
static int showdead = 0;
static int emacs = 0;
SGFTree sgftree;
-SGFNode *curnode = 0;
static int last_move_i; /* The position of the last move */
static int last_move_j; /* -""- */
@@ -60,7 +59,6 @@
static void showcapture(char *line);
static void showdefense(char *line);
static void ascii_goto(Gameinfo *gameinfo, char *line);
-static int ascii2pos(char *line, int *i, int *j);
/* If sgf game info is written can't reset parameters like handicap, etc. */
static int sgf_initialized;
@@ -416,16 +414,16 @@
*/
static void
-init_sgf(Gameinfo *ginfo, SGFNode *root)
+init_sgf(Gameinfo *ginfo)
{
if (sgf_initialized)
return;
sgf_initialized = 1;
- sgf_write_header(root, 1, random_seed, komi, level, chinese_rules);
- sgfOverwritePropertyInt(root, "HA", ginfo->handicap);
+ sgf_write_header(sgftree.root, 1, random_seed, komi, level, chinese_rules);
+ sgfOverwritePropertyInt(sgftree.root, "HA", ginfo->handicap);
if (ginfo->handicap > 0)
- gnugo_recordboard(root);
+ gnugo_recordboard(sgftree.root);
}
@@ -439,7 +437,7 @@
int i, j;
int move_val;
- init_sgf(gameinfo, sgftree.root);
+ init_sgf(gameinfo);
/* Generate computer move. */
move_val = gnugo_genmove(&i, &j, gameinfo->to_move);
@@ -459,8 +457,8 @@
*passes = 0;
gnugo_play_move(i, j, gameinfo->to_move);
- sgffile_add_debuginfo(curnode, move_val);
- curnode = sgfAddPlay(curnode, gameinfo->to_move, i, j);
+ sgffile_add_debuginfo(sgftree.lastnode, move_val);
+ sgftreeAddPlay(&sgftree, gameinfo->to_move, i, j);
sgffile_output(&sgftree);
gameinfo->to_move = OTHER_COLOR(gameinfo->to_move);
@@ -476,7 +474,7 @@
{
int i, j;
- if (!ascii2pos(command, &i, &j)) {
+ if (!string_to_location(board_size, command, &i, &j)) {
printf("\nInvalid move: %s\n", command);
return;
}
@@ -488,10 +486,10 @@
*passes = 0;
TRACE("\nyour move: %m\n\n", i, j);
- init_sgf(gameinfo, sgftree.root);
+ init_sgf(gameinfo);
gnugo_play_move(i, j, gameinfo->to_move);
- sgffile_add_debuginfo(curnode, 0);
- curnode = sgfAddPlay(curnode, gameinfo->to_move, i, j);
+ sgffile_add_debuginfo(sgftree.lastnode, 0);
+ sgftreeAddPlay(&sgftree, gameinfo->to_move, i, j);
sgffile_output(&sgftree);
last_move_i = i;
@@ -504,7 +502,7 @@
if (force) {
gameinfo->computer_player = OTHER_COLOR(gameinfo->computer_player);
gameinfo->to_move = OTHER_COLOR(gameinfo->to_move);
- sgfAddComment(curnode, "forced");
+ sgftreeAddComment(&sgftree, "forced");
return;
}
gameinfo->to_move = OTHER_COLOR(gameinfo->to_move);
@@ -520,16 +518,16 @@
do_pass(Gameinfo *gameinfo, int *passes, int force)
{
(*passes)++;
- init_sgf(gameinfo, sgftree.root);
+ init_sgf(gameinfo);
gnugo_play_move(-1, -1, gameinfo->to_move);
- sgffile_add_debuginfo(curnode, 0);
- curnode = sgfAddPlay(curnode, gameinfo->to_move, -1, -1);
+ sgffile_add_debuginfo(sgftree.lastnode, 0);
+ sgftreeAddPlay(&sgftree, gameinfo->to_move, -1, -1);
sgffile_output(&sgftree);
gameinfo->to_move = OTHER_COLOR(gameinfo->to_move);
if (force) {
gameinfo->computer_player = OTHER_COLOR(gameinfo->computer_player);
- sgfAddComment(curnode, "forced");
+ sgftreeAddComment(&sgftree, "forced");
return;
}
computer_move(gameinfo, passes);
@@ -569,9 +567,8 @@
if (filename) {
gameinfo_load_sgfheader(gameinfo, sgftree.root);
- gameinfo->to_move = gameinfo_play_sgftree(gameinfo, sgftree.root, until);
+ gameinfo->to_move = gameinfo_play_sgftree(gameinfo, &sgftree, until);
sgf_initialized = 1;
- curnode = sgftreeNodeCheck(&sgftree, 0);
}
else {
if (sgfGetIntProperty(sgftree.root, "SZ", &sz))
@@ -583,7 +580,6 @@
gameinfo->to_move = WHITE;
}
sgf_initialized = 0;
- curnode = sgftree.root;
}
printf("\nBeginning ASCII mode game.\n\n");
@@ -617,7 +613,7 @@
sgftreeWriteResult(&sgftree,
gameinfo->to_move == WHITE ? -1000.0 : 1000.0,
1);
- sgffile_output(&sgftree);
+ sgffile_output(&sgftree);
case END:
case EXIT:
case QUIT:
@@ -793,33 +789,29 @@
case UNDO:
case CMD_BACK:
if (gnugo_undo_move(1)) {
- sgfAddComment(curnode, "undone");
- curnode = curnode->parent;
+ sgftreeAddComment(&sgftree, "undone");
+ sgftreeBack(&sgftree);
gameinfo->to_move = OTHER_COLOR(gameinfo->to_move);
}
else
printf("\nCan't undo.\n");
break;
case CMD_FORWARD:
- if (curnode->child) {
- gameinfo->to_move = gnugo_play_sgfnode(curnode->child,
+ if (sgftreeForward(&sgftree))
+ gameinfo->to_move = gnugo_play_sgfnode(sgftree.lastnode,
gameinfo->to_move);
- curnode = curnode->child;
- }
else
printf("\nEnd of game tree.\n");
break;
case CMD_LAST:
- while (curnode->child) {
- gameinfo->to_move = gnugo_play_sgfnode(curnode->child,
+ while (sgftreeForward(&sgftree))
+ gameinfo->to_move = gnugo_play_sgfnode(sgftree.lastnode,
gameinfo->to_move);
- curnode = curnode->child;
- }
break;
case COMMENT:
printf("\nEnter comment. Press ENTER when ready.\n");
fgets(line, 80, stdin);
- sgfAddComment(curnode, line);
+ sgftreeAddComment(&sgftree, line);
break;
case SCORE:
showscore = !showscore;
@@ -873,7 +865,7 @@
/* discard newline */
tmpstring[strlen(tmpstring)-1] = 0;
/* make sure we are saving proper handicap */
- init_sgf(gameinfo, sgftree.root);
+ init_sgf(gameinfo);
writesgf(sgftree.root, tmpstring);
printf("You may resume the game");
printf(" with -l %s --mode ascii\n", tmpstring);
@@ -895,9 +887,8 @@
/* to avoid changing handicap etc. */
sgf_initialized = 1;
gameinfo_load_sgfheader(gameinfo, sgftree.root);
- gameinfo_play_sgftree(gameinfo, sgftree.root, NULL);
+ gameinfo_play_sgftree(gameinfo, &sgftree, NULL);
sgfOverwritePropertyInt(sgftree.root, "HA", gameinfo->handicap);
- curnode = sgftreeNodeCheck(&sgftree, 0);
}
else
printf("Please specify a filename\n");
@@ -941,7 +932,7 @@
if (tmpstring) {
/* discard newline */
tmpstring[strlen(tmpstring)-1] = 0;
- init_sgf(gameinfo, sgftree.root);
+ init_sgf(gameinfo);
writesgf(sgftree.root, tmpstring);
}
else
@@ -1036,7 +1027,7 @@
ascii_showboard();
}
else {
- if (!ascii2pos(line, &i, &j) || BOARD(i, j) == EMPTY)
+ if (!string_to_location(board_size, line, &i, &j) || BOARD(i, j) ==
EMPTY)
printf("\ninvalid!\n");
else {
int status = dragon_status(POS(i, j));
@@ -1055,7 +1046,7 @@
{
int i, j, x, y;
if (line)
- if (!ascii2pos(line, &i, &j) || BOARD(i, j) == EMPTY) {
+ if (!string_to_location(board_size, line, &i, &j) || BOARD(i, j) == EMPTY)
{
printf("\ninvalid point!\n");
return;
}
@@ -1071,7 +1062,7 @@
{
int i, j, x, y;
if (line)
- if (!ascii2pos(line, &i, &j) || BOARD(i, j) == EMPTY) {
+ if (!string_to_location(board_size, line, &i, &j) || BOARD(i, j) == EMPTY)
{
printf("\ninvalid point!\n");
return;
}
@@ -1090,50 +1081,18 @@
static void
ascii_goto(Gameinfo *gameinfo, char *line)
{
- int movenumber = 0;
+
if (!line)
return;
if (!strncmp(line, "last", 4))
- movenumber = 9999;
+ movenumber = "9999";
else {
if (!strncmp(line, "first", 4))
- movenumber = 1;
- else
- sscanf(line, "%i", &movenumber);
+ movenumber = "1";
}
- printf("goto %i\n", movenumber);
- curnode = sgftree.root;
- gameinfo->to_move = gnugo_play_sgftree(curnode, &movenumber, &curnode);
- return;
-}
-
-
-/* Convert a coordinate pair from ascii text to two integers.
- * FIXME: Check that this function is indeed equivalent to
- * string_to_location() and then replace it.
- */
-static int
-ascii2pos(char *line, int *i, int *j)
-{
- int d;
- char c;
- if (sscanf(line, "%c%d", &c, &d) != 2)
- return 0;
-
- /* No 'I' in the coordinate system. */
- if (tolower((int) c) == 'i')
- return 0;
-
- *j = tolower((int) c) - 'a';
- if (tolower((int) c) > 'i')
- --*j;
-
- *i = board_size - d;
-
- if (*i < 0 || *i >= board_size || *j < 0 || *j >= board_size)
- return 0;
-
- return 1;
+ printf("goto %s\n", movenumber);
+ gameinfo_play_sgftree(gameinfo, &sgftree, movenumber);
}
Index: interface/main.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/main.c,v
retrieving revision 1.48
diff -u -r1.48 main.c
--- interface/main.c 10 Sep 2002 20:09:52 -0000 1.48
+++ interface/main.c 11 Sep 2002 17:35:03 -0000
@@ -874,8 +874,8 @@
exit(EXIT_FAILURE);
}
- gameinfo_play_sgftree_rot(&gameinfo, sgftree.root,
- untilstring, orientation);
+ gameinfo_play_sgftree_rot(&gameinfo, &sgftree, untilstring,
+ orientation);
}
else
/* Initialize and empty sgf tree if there was no infile. */
Index: interface/debugboard/main.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/debugboard/main.c,v
retrieving revision 1.8
diff -u -r1.8 main.c
--- interface/debugboard/main.c 7 Mar 2002 05:35:28 -0000 1.8
+++ interface/debugboard/main.c 11 Sep 2002 17:35:04 -0000
@@ -71,7 +71,7 @@
Gameinfo gameinfo;
-SGFNode *sgf_root;
+SGFTree sgftree;
int current_color = WHITE;
@@ -100,7 +100,7 @@
/* Try to open the infile. */
infilename = argv[1];
- if ((sgf_root = readsgffile(infilename)) == NULL) {
+ if (!sgftree_readfile(&sgftree, infilename)) {
fprintf(stderr, "Cannot open or parse '%s'\n", infilename);
exit(1);
}
@@ -118,7 +118,7 @@
josekidb = 1;
gameinfo_clear(&gameinfo, 19, 5.5);
- next = gameinfo_play_sgftree(&gameinfo, sgf_root, until);
+ next = gameinfo_play_sgftree(&gameinfo, &sgftree, until);
/* Check if there is enough screen space. */
Index: engine/interface.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/interface.c,v
retrieving revision 1.28
diff -u -r1.28 interface.c
--- engine/interface.c 10 Sep 2002 20:06:01 -0000 1.28
+++ engine/interface.c 11 Sep 2002 17:35:05 -0000
@@ -477,7 +477,7 @@
*/
int
-gameinfo_play_sgftree_rot(Gameinfo *gameinfo, SGFNode *head,
+gameinfo_play_sgftree_rot(Gameinfo *gameinfo, SGFTree *tree,
const char *untilstr, int orientation)
{
int bs, handicap;
@@ -488,15 +488,13 @@
int until = 9999;
int addstone = 0; /* handicap stone detector */
- SGFNode *node;
-
- if (!sgfGetFloatProperty(head, "KM", &komi)) {
+ if (!sgfGetFloatProperty(tree->root, "KM", &komi)) {
if (gameinfo->handicap == 0)
komi = 5.5;
else
komi = 0.5;
}
- if (!sgfGetIntProperty(head, "SZ", &bs))
+ if (!sgfGetIntProperty(tree->root, "SZ", &bs))
bs = 19;
gnugo_clear_board(bs);
gnugo_set_komi(komi);
@@ -518,7 +516,7 @@
}
}
- if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) {
+ if (sgfGetIntProperty(tree->root, "HA", &handicap) && handicap > 1) {
gameinfo->handicap = handicap;
next = WHITE;
}
@@ -529,11 +527,11 @@
*
* The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc]
*/
- for (node = head; node; node = node->child) {
+ for (tree->lastnode = NULL; sgftreeForward(tree);) {
SGFProperty *prop;
int i, j;
- for (prop = node->props; prop; prop = prop->next) {
+ for (prop = tree->lastnode->props; prop; prop = prop->next) {
DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n",
prop->name & 0xff, (prop->name >> 8), prop->value);
switch (prop->name) {
@@ -561,7 +559,7 @@
/* following really should not be needed for proper sgf file */
if (movenum != 0 && !addstone) {
gnugo_sethand(gameinfo->handicap, 0);
- sgfOverwritePropertyInt(head, "HA", gameinfo->handicap);
+ sgfOverwritePropertyInt(tree->root, "HA", gameinfo->handicap);
}
/* Due to a bad comment in the SGF FF3 definition (in the
@@ -583,17 +581,21 @@
/* following really should not be needed for proper sgf file */
if (movenum != 0 && !addstone) {
gnugo_sethand(gameinfo->handicap, 0);
- sgfOverwritePropertyInt(head, "HA", gameinfo->handicap);
+ sgfOverwritePropertyInt(tree->root, "HA", gameinfo->handicap);
}
if (movenum == until - 1) {
gameinfo->to_move = next;
+ /* go back so that variant will be added to the proper node */
+ sgftreeBack(tree);
return next;
}
if (get_moveXY(prop, &i, &j, board_size))
if (i == untilm && j == untiln) {
gameinfo->to_move = next;
+ /* go back so that variant will be added to the proper node */
+ sgftreeBack(tree);
return next;
}
@@ -645,9 +647,9 @@
/* Same as previous function, using standard orientation */
int
-gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr)
+gameinfo_play_sgftree(Gameinfo *gameinfo, SGFTree *tree, const char *untilstr)
{
- return gameinfo_play_sgftree_rot(gameinfo, head, untilstr, 0);
+ return gameinfo_play_sgftree_rot(gameinfo, tree, untilstr, 0);
}
Index: engine/gnugo.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/gnugo.h,v
retrieving revision 1.65
diff -u -r1.65 gnugo.h
--- engine/gnugo.h 10 Sep 2002 20:09:52 -0000 1.65
+++ engine/gnugo.h 11 Sep 2002 17:35:07 -0000
@@ -170,9 +170,9 @@
void gameinfo_print(Gameinfo *ginfo);
void gameinfo_load_sgfheader(Gameinfo *gameinfo, SGFNode *head);
void gameinfo_play_move(Gameinfo *ginfo, int i, int j, int color);
-int gameinfo_play_sgftree_rot(Gameinfo *gameinfo, SGFNode *head,
+int gameinfo_play_sgftree_rot(Gameinfo *gameinfo, SGFTree *tree,
const char *untilstr, int orientation);
-int gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head,
+int gameinfo_play_sgftree(Gameinfo *gameinfo, SGFTree *tree,
const char *untilstr);
- [gnugo-devel] sgf cleaning continued,
Paul Pogonyshev <=