gnugo-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnugo-devel] play_test.c and sgf output


From: Paul Pogonyshev
Subject: [gnugo-devel] play_test.c and sgf output
Date: Fri, 6 Sep 2002 23:22:55 +0300

this patch is an extension to sgf_3_8.1 and replaces it.

changes:
  - play_test.c now outputs sgf files using trees.
  - changed parameters of play_replay()
  - uncommented previously commented lines (should be commented only
    in my local version).

tomorrow i'll check if any code parts still use old way to write sgf.
if there's no such code, i'll remove old functions.



Index: interface/play_test.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_test.c,v
retrieving revision 1.10
diff -u -r1.10 play_test.c
--- interface/play_test.c       4 Sep 2002 07:23:46 -0000       1.10
+++ interface/play_test.c       6 Sep 2002 20:13:45 -0000
@@ -39,65 +39,56 @@
 /* --------------------------------------------------------------*/
 
 void
-play_replay(SGFNode *sgf_head, int color_to_replay)
+play_replay(Gameinfo *gameinfo, int color_to_replay)
 {
-  Gameinfo  gameinfo;
   int tmpi;
   float tmpf;
   char *tmpc = NULL;
 
-  SGFNode *node;
+  SGFTree tree = gameinfo->game_record;
+  SGFNode *node = tree.root;
 
   /* Get the board size. */
-  if (!sgfGetIntProperty(sgf_head, "SZ", &tmpi)) {
+  if (!sgfGetIntProperty(node, "SZ", &tmpi)) {
     fprintf(stderr, "Couldn't find the size (SZ) attribute!\n");
     exit(EXIT_FAILURE);
   }
-  gameinfo_clear(&gameinfo, tmpi, 5.5);
-   
-  /* Get the number of handicap stones. */
-  if (sgfGetIntProperty(sgf_head, "HA", &tmpi)) {
-    /* Handicap stones should appear as AW,AB properties in the sgf file. */
-    gameinfo.handicap = tmpi;
-  }
+
+  gnugo_clear_board(tmpi);
 
   /* Get the komi. */
-  if (sgfGetFloatProperty(sgf_head, "KM", &tmpf))
+  if (sgfGetFloatProperty(node, "KM", &tmpf))
     komi = tmpf;
 
   if (!quiet) {
-    if (sgfGetCharProperty(sgf_head, "RU", &tmpc))
+    if (sgfGetCharProperty(node, "RU", &tmpc))
       printf("Ruleset:      %s\n", tmpc);
-    if (sgfGetCharProperty(sgf_head, "GN", &tmpc))
+    if (sgfGetCharProperty(node, "GN", &tmpc))
       printf("Game Name:    %s\n", tmpc);
-    if (sgfGetCharProperty(sgf_head, "DT", &tmpc))
+    if (sgfGetCharProperty(node, "DT", &tmpc))
       printf("Game Date:    %s\n", tmpc);
-    if (sgfGetCharProperty(sgf_head, "GC", &tmpc))
+    if (sgfGetCharProperty(node, "GC", &tmpc))
       printf("Game Comment: %s\n", tmpc);
-    if (sgfGetCharProperty(sgf_head, "US", &tmpc))
+    if (sgfGetCharProperty(node, "US", &tmpc))
       printf("Game User:    %s\n", tmpc);
-    if (sgfGetCharProperty(sgf_head, "PB", &tmpc))
+    if (sgfGetCharProperty(node, "PB", &tmpc))
       printf("Black Player: %s\n", tmpc);
-    if (sgfGetCharProperty(sgf_head, "PW", &tmpc))
+    if (sgfGetCharProperty(node, "PW", &tmpc))
       printf("White Player: %s\n", tmpc);
 
-    gameinfo_print(&gameinfo);
+    gameinfo_print(gameinfo);
   }
 
-  sgffile_write_gameinfo(&gameinfo, "replay game");         
-   
   /*
    * Now actually run through the file.  This is the interesting part.
    * We need to traverse the SGF tree, and every time we encounter a node
    * we need to check what move GNU Go would make, and see if it is OK. 
    */
-  node = sgf_head;
   while (node) {
     replay_node(node, color_to_replay);
+    sgffile_output(tree.root);
     node = node->child;
   }
-
-   sgffile_close_file();
 }
 
 
@@ -117,6 +108,8 @@
   int m, n; /* Move from file. */
   int i, j; /* Move generated by GNU Go. */
 
+  char buf[127];
+
   /* Handle any AB / AW properties, and note presence
    * of move properties.
    */
@@ -127,15 +120,11 @@
       /* add black */
       gnugo_add_stone(get_moveX(sgf_prop, boardsize),
                      get_moveY(sgf_prop, boardsize), BLACK);
-      sgffile_put_stone(get_moveX(sgf_prop, boardsize),
-                       get_moveY(sgf_prop, boardsize), BLACK);
       break;
     case SGFAW:
       /* add white */
       gnugo_add_stone(get_moveX(sgf_prop, boardsize),
                      get_moveY(sgf_prop, boardsize), WHITE);
-      sgffile_put_stone(get_moveX(sgf_prop, boardsize),
-                       get_moveY(sgf_prop, boardsize), WHITE);
       break;
     case SGFB:
     case SGFW:
@@ -168,21 +157,24 @@
       printf("\n");
     }
     if (i != m || j != n) {
-      char buf[127];
-      gg_snprintf(buf, 127, "GNU Go plays %s(%.2f) - Game move %s(%.2f)",
+      gg_snprintf(buf, 127, "GNU Go plays %s (%.2f) - Game move %s (%.2f)",
                  location_to_string(POS(i, j)),
                  gnugo_is_pass(i, j) ? 0 : potential_moves[i][j],
                  location_to_string(POS(m, n)),
                  gnugo_is_pass(m, n)
                  && potential_moves[m][n] < 0.0 ? 0 : potential_moves[m][n]);
-      sgffile_write_comment(buf);
-      sgffile_write_circle_mark(i, j);
+      sgfCircle(node, i, j);
     }
+    else
+      gg_snprintf(buf, 127, "GNU Go plays the same move %s (%.2f)",
+                 location_to_string(POS(i, j)),
+                 gnugo_is_pass(i, j) ? 0 : potential_moves[i][j]);
+    sgfAddComment(node, buf);
+    sgffile_debuginfo(node, 0);
   }
 
   /* Finally, do play the move from the file. */
   gnugo_play_move(m, n, color);
-  sgffile_move_made(m, n, color, 0);
 }
 
 
Index: interface/play_solo.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_solo.c,v
retrieving revision 1.15
diff -u -r1.15 play_solo.c
--- interface/play_solo.c       4 Sep 2002 07:23:46 -0000       1.15
+++ interface/play_solo.c       6 Sep 2002 20:13:46 -0000
@@ -34,6 +34,8 @@
 #include "random.h"
 #include "gg_utils.h"
 
+SGFTree sgftree;
+SGFNode *curnode;
 
 void
 play_solo(Gameinfo *gameinfo, int moves)
@@ -56,7 +58,11 @@
   int n = 6 + 2*gg_rand()%5;
   int i, j;
 
-  sgffile_write_gameinfo(gameinfo, "solo");
+  gnugo_set_komi(5.5);
+
+  sgftree_clear(&sgftree);
+  curnode = sgftreeCreateHeaderNode(&sgftree, gnugo_get_boardsize(), 
gnugo_get_komi());
+  sgf_write_header(sgftree.root, 1, random_seed, 5.5, level, chinese_rules);
  
   /* Generate some random moves. */
   if (boardsize > 6) {
@@ -65,8 +71,11 @@
        i = (gg_rand() % 4) + (gg_rand() % (boardsize - 4));
        j = (gg_rand() % 4) + (gg_rand() % (boardsize - 4));
       } while (!gnugo_is_legal(i, j, gameinfo->to_move));
-      
-      gameinfo_play_move(gameinfo, i, j, gameinfo->to_move);
+
+      gnugo_play_move(i, j, gameinfo->to_move);
+      curnode = sgfAddPlay(curnode, gameinfo->to_move, i, j);
+      sgfAddComment(curnode, "random move");
+      gameinfo->to_move = OTHER_COLOR(gameinfo->to_move);
     } while (--n > 0);
   }
   
@@ -75,7 +84,12 @@
   while (passes < 2 && --moves >= 0 && !time_to_die) {
     reset_owl_node_counter();
     move_val = gnugo_genmove(&i, &j, gameinfo->to_move);
-    gameinfo_play_move(gameinfo, i, j, gameinfo->to_move);
+
+    gnugo_play_move(i, j, gameinfo->to_move);
+    sgffile_debuginfo(curnode, move_val);
+    curnode = sgfAddPlay(curnode, gameinfo->to_move, i, j);
+    sgffile_output(sgftree.root);
+    gameinfo->to_move = OTHER_COLOR(gameinfo->to_move);
 
     if (move_val < 0) {
       ++passes;
@@ -100,6 +114,10 @@
   /* Two passes and it's over. (EMPTY == BOTH) */
   gnugo_who_wins(EMPTY, stdout);
 
+  score = gnugo_estimate_score(&lower_bound, &upper_bound);
+  sgfWriteResult(sgftree.root, score, 1);
+  sgffile_output(sgftree.root);
+
 #if 0
   if (t2 == t1)
     printf("%.3f moves played\n", (double) (save_moves-moves));
@@ -136,31 +154,23 @@
   int i, j;
   int next;
   int r;
+  int move_val;
   
   next = gameinfo->to_move;
-  gameinfo->computer_player = next;
-  sgffile_write_gameinfo(gameinfo, "load and analyze");
+  sgftree = gameinfo->game_record;
 
-  if (benchmark) {
-    for (r = 0; r < benchmark; ++r) {
-      genmove(&i, &j, next);
-      next = OTHER_COLOR(next);
-    }
-  }
-  else {
-    genmove(&i, &j, next);
-    
-    if (is_pass(POS(i, j))) {
-      gprintf("%s move: PASS!\n", next == WHITE ? "white (O)" : "black (X)");
-      sgffile_move_made(i, j, next, 0);
-    }
-    else {
-      gprintf("%s move %m\n", next == WHITE ? "white (O)" : "black (X)",
-             i, j);
-      gnugo_play_move(i, j, next);
-      sgffile_move_made(i, j, next, 0);
-    }
-  }
+  move_val = gnugo_genmove(&i, &j, next);
+
+  if (is_pass(POS(i, j)))
+    gprintf("%s move: PASS!\n", next == WHITE ? "white (O)" : "black (X)");
+  else
+    gprintf("%s move %m\n", next == WHITE ? "white (O)" : "black (X)",
+      i, j);
+  curnode = sgftreeNodeCheck(&sgftree, 0);
+  curnode = sgfAddPlay(curnode, next, i, j);
+  sgfAddComment(curnode, "load and analyze mode");
+  sgffile_debuginfo(curnode, move_val);
+  sgffile_output(sgftree.root);
 }
 
 
@@ -194,9 +204,9 @@
   sgftreeSetLastNode(&score_tree, node);
   sgftree_printboard(&score_tree);
   
-  sgffile_write_gameinfo(gameinfo, "load and score");
   next = gameinfo->to_move;
   sgffile_printboard(next);
+  node = node->child;
   doing_scoring = 1;
   reset_engine();
   
@@ -215,13 +225,13 @@
                next == WHITE ? "white (O)" : "black (X)");
       }
       play_move(POS(i, j), next);
-      sgffile_move_made(i, j, next, move_val);
-      sgftreeAddPlay(&score_tree, NULL, next, i, j);
+      sgffile_debuginfo(node, move_val);
+      node = sgfAddPlay(node, next, i, j);
+      sgffile_output(score_tree.root);
       next = OTHER_COLOR(next);
     } while (movenum <= until && pass < 2);
 
     if (pass >= 2) {
-      node = score_tree.lastnode;
       /* Calculate the score */
       if (!strcmp(scoringmode, "aftermath"))
        score = aftermath_compute_score(next, komi, &score_tree);
@@ -265,26 +275,23 @@
        }
       }
       sgfWriteResult(score_tree.root, score, 1);
+      sgffile_output(score_tree.root);
     }
   }
   doing_scoring = 0;
 
 
-  if (!strcmp(scoringmode, "aftermath")) {
-    if (gameinfo->outfilename)
-      writesgf(score_tree.root, gameinfo->outfilename);
-    return;
+  if (strcmp(scoringmode, "aftermath")) {
+    /* Before we call estimate_score() we must make sure that the dragon
+     * status is computed. Therefore the call to examine_position().
+     */
+    examine_position(next, EXAMINE_ALL);
+    score = estimate_score(NULL, NULL);
+
+    fprintf(stdout, "\n%s seems to win by %1.1f points\n",
+      score < 0 ? "B" : "W",
+      score < 0 ? -score : score);
   }
-
-  /* Before we call estimate_score() we must make sure that the dragon
-   * status is computed. Therefore the call to examine_position().
-   */
-  examine_position(next, EXAMINE_ALL);
-  score = estimate_score(NULL, NULL);
-
-  fprintf(stdout, "\n%s seems to win by %1.1f points\n",
-         score < 0 ? "B" : "W",
-         score < 0 ? -score : score);
 }
 
 
Index: interface/play_ascii.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_ascii.c,v
retrieving revision 1.23
diff -u -r1.23 play_ascii.c
--- interface/play_ascii.c      3 Sep 2002 00:28:12 -0000       1.23
+++ interface/play_ascii.c      6 Sep 2002 20:13:49 -0000
@@ -617,6 +617,7 @@
          sgftreeWriteResult(&sgftree,
                             gameinfo->to_move == WHITE ? -1000.0 : 1000.0,
                             1);
+         sgffile_output(sgftree.root);
        case END:
        case EXIT:
        case QUIT:
@@ -940,7 +941,7 @@
        if (tmpstring) {
          /* discard newline */
          tmpstring[strlen(tmpstring)-1] = 0;
-         init_sgf(gameinfo,sgftree.root);
+          init_sgf(gameinfo,sgftree.root);
          writesgf(sgftree.root, tmpstring);
        }
        else
@@ -963,6 +964,8 @@
        state = 0;
       }
     }
+    sgftreeWriteResult(&sgftree, estimate_score(NULL, NULL), 1);
+    sgffile_output(sgftree.root);
     passes = 0;
     showdead = 0;
     /* Play a different game next time. */
Index: interface/main.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/main.c,v
retrieving revision 1.45
diff -u -r1.45 main.c
--- interface/main.c    3 Sep 2002 00:28:12 -0000       1.45
+++ interface/main.c    6 Sep 2002 20:13:56 -0000
@@ -881,9 +881,8 @@
     gameinfo_play_sgftree_rot(&gameinfo, sgftree.root,
                              untilstring, orientation);
   }
-
+  else
   /* Initialize and empty sgf tree if there was no infile. */
-  if (!sgftree.root)
     sgftreeCreateHeaderNode(&sgftree, board_size, komi);
 
   /* Set the game_record to be identical to the loaded one or the
@@ -920,7 +919,7 @@
       if (!sgffile_open_file(outfile)) {
        fprintf(stderr, "Error: could not open '%s'\n", gg_optarg);
        exit(EXIT_FAILURE);
-      }
+      };
   }
   
   switch (playmode) {
@@ -933,18 +932,18 @@
     break;
     
   case MODE_REPLAY:    
-    if (!sgftree.root) {
+    if (!infilename) {
       fprintf(stderr, "You must use -l infile with replay mode.\n");
       exit(EXIT_FAILURE);
     }
-    play_replay(sgftree.root, replay_color);
+    play_replay(&gameinfo, replay_color);
     break;
     
   case MODE_LOAD_AND_ANALYZE:
     if (mandated_color != EMPTY)
       gameinfo.to_move = mandated_color;
     
-    if (!sgftree.root) {
+    if (!infilename) {
       fprintf(stderr, "You must use -l infile with load and analyze mode.\n");
       exit(EXIT_FAILURE);
     }
@@ -952,7 +951,7 @@
     break;
     
   case MODE_LOAD_AND_SCORE:
-    if (!sgftree.root) {
+    if (!infilename) {
       fprintf(stderr, "gnugo: --score must be used with -l\n");
       exit(EXIT_FAILURE);
     }
@@ -960,7 +959,7 @@
     break;
     
   case MODE_LOAD_AND_PRINT:
-    if (!sgftree.root) {
+    if (!infilename) {
       fprintf(stderr, "gnugo: --printsgf must be used with -l\n");
       exit(EXIT_FAILURE);
     }
@@ -1299,7 +1298,7 @@
    --score estimate        estimate score at loaded position\n\
    --score finish          generate moves to finish game, then score\n\
    --score aftermath       generate moves to finish, use best algorithm\n\
-   --score aftermath --capture-all-dead --chinese rules   Tromp-Taylor score\n\
+   --score aftermath --capture-all-dead --chinese-rules   Tromp-Taylor score\n\
 \n\
 Cache size (higher=more memory usage, faster unless swapping occurs):\n\
    -M, --cache-size <megabytes>  RAM cache for hashing (default %4.1f Mb)\n\
@@ -1365,7 +1364,7 @@
    -O, --output-flags <flags>    optional output (use with -o)\n\
                     d: mark dead and critical dragons\n\
                     v: show values of considered moves\n\
-                    specify either no flags (default), 'd', 'v' or 'dv'\n\
+                    specify either 'd', 'v' or 'dv' (nothing by default)\n\
    --showtime                    print timing diagnostic\n\
    --replay <color>              replay game. Use with -o.\n\
    --showscore                   print estimated score\n\
Index: interface/interface.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/interface.h,v
retrieving revision 1.9
diff -u -r1.9 interface.h
--- interface/interface.h       7 Mar 2002 05:35:28 -0000       1.9
+++ interface/interface.h       6 Sep 2002 20:13:56 -0000
@@ -37,7 +37,7 @@
 void play_gtp(FILE *gtp_input, int gtp_initial_orientation);
 void play_gmp(Gameinfo *gameinfo);
 void play_solo(Gameinfo *gameinfo, int benchmark);
-void play_replay(SGFNode *sgf_head, int color_to_test);
+void play_replay(Gameinfo *gameinfo, int color_to_test);
 
 void load_and_analyze_sgf_file(Gameinfo *gameinfo, int benchmark);
 void load_and_score_sgf_file(SGFTree *tree, Gameinfo *gameinfo,





reply via email to

[Prev in Thread] Current Thread [Next in Thread]