gnugo-devel
[Top][All Lists]
Advanced

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

[gnugo-devel] code cleaning


From: Paul Pogonyshev
Subject: [gnugo-devel] code cleaning
Date: Fri, 18 Oct 2002 22:17:40 +0300

this patch does some code cleaning. here is the complete list:
  - old tree is now freed on succesful sgf loading (no or less memory
    leaks in play_ascii.c).
  - trees are alse freed when sgf tracing is finished in gtp mode.
  - sgffile_printsgf() really prints the position if color_to_play is
    EMPTY (doesn't really matter since it is never called that way
    now).
  - board is cleared in fixed_handicap command in case the handicap is
    invalid (say, tried to place 8 stones on a 16x16 board).
  - several comments are fixed in play_gtp.c.
  - some parts of play_ascii.c are reorganized (if started with -l
    option, don't load that file _each_ time a new game is started).

Paul


Index: gnugo/sgf/sgftree.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/sgf/sgftree.c,v
retrieving revision 1.12
diff -u -r1.12 sgftree.c
--- gnugo/sgf/sgftree.c 16 Sep 2002 08:37:08 -0000      1.12
+++ gnugo/sgf/sgftree.c 16 Oct 2002 19:34:43 -0000
@@ -41,6 +41,8 @@
     tree->root = savetree;
     return 0;
   }
+  
+  sgfFreeNode(savetree);
   tree->lastnode = NULL;
   return 1;
 }
Index: gnugo/engine/sgffile.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/sgffile.c,v
retrieving revision 1.23
diff -u -r1.23 sgffile.c
--- gnugo/engine/sgffile.c      22 Sep 2002 11:50:05 -0000      1.23
+++ gnugo/engine/sgffile.c      16 Oct 2002 19:34:44 -0000
@@ -134,10 +134,12 @@
 void 
 sgffile_enddump(const char *filename)
 {
-  /* check if we have a valid filename */
-  if (filename && *filename)
+  /* Check if we have a valid filename and a tree. */
+  if (filename && *filename && sgf_dumptree) {
     writesgf(sgf_dumptree->root, filename);
-  sgf_dumptree = NULL;
+    sgfFreeNode(sgf_dumptree->root);
+    sgf_dumptree = NULL;
+  }
 }
 
 
@@ -167,18 +169,17 @@
   
   sgffile_printboard(&sgftree);
   
-  if (color_to_play == EMPTY)
-    return;
-
-  sgfAddProperty(sgftree.lastnode, "PL", (color_to_play == WHITE ? "W" : "B"));
-
-  for (m = 0; m < board_size; ++m)
-    for (n = 0; n < board_size; ++n)
-      if (BOARD(m, n) == EMPTY && !is_legal(POS(m, n), color_to_play)) {
-       gg_snprintf(pos, 3, "%c%c", 'a' + n, 'a' + m);
-       sgfAddProperty(sgftree.lastnode, "IL", pos);
-      }
-
+  if (color_to_play != EMPTY) {
+    sgfAddProperty(sgftree.lastnode, "PL",
+      (color_to_play == WHITE ? "W" : "B"));
+
+    for (m = 0; m < board_size; ++m)
+      for (n = 0; n < board_size; ++n)
+        if (BOARD(m, n) == EMPTY && !is_legal(POS(m, n), color_to_play)) {
+         gg_snprintf(pos, 3, "%c%c", 'a' + n, 'a' + m);
+         sgfAddProperty(sgftree.lastnode, "IL", pos);
+       }
+  }
 
   writesgf(sgftree.root, filename);
 }
Index: gnugo/interface/play_gtp.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_gtp.c,v
retrieving revision 1.96
diff -u -r1.96 play_gtp.c
--- gnugo/interface/play_gtp.c  14 Oct 2002 17:02:57 -0000      1.96
+++ gnugo/interface/play_gtp.c  16 Oct 2002 19:34:52 -0000
@@ -579,7 +579,7 @@
 }
 
 
-/* Function:  Play a black stone at the given vertex.
+/* Function:  Play a stone of the given color at the given vertex.
  * Arguments: vertex
  * Fails:     invalid vertex, illegal move
  * Returns:   nothing
@@ -628,8 +628,10 @@
   if (this_handicap < 2 && (gtp_version > 1 || this_handicap != 0))
     return gtp_failure("invalid handicap");
 
-  if (place_fixed_handicap(this_handicap) != this_handicap)
+  if (place_fixed_handicap(this_handicap) != this_handicap) {
+    clear_board();
     return gtp_failure("invalid handicap");
+  }
 
   handicap = this_handicap;
 
@@ -767,7 +769,8 @@
   nread = sscanf(s, "%s %s", filename, untilstring);
   if (nread < 1)
     return gtp_failure("missing filename");
-  
+
+  sgftree_clear(&sgftree);
   if (!sgftree_readfile(&sgftree, filename))
     return gtp_failure("cannot open or parse '%s'", filename);
 
@@ -999,7 +1002,7 @@
 }
 
 
-/* Function:  Undo a trymove.
+/* Function:  Undo a trymove or tryko.
  * Arguments: none
  * Fails:     stack empty
  * Returns:   nothing
@@ -2031,7 +2034,7 @@
   return GTP_OK;
 }
 
-/* Function : Generate a list of the best moves for White with weights
+/* Function : Generate a list of the best moves for white with weights
  * Arguments: none
  * Fails:   : never
  * Returns  : list of moves with weights
@@ -2052,6 +2055,12 @@
   return gtp_finish_response();
 }
 
+/* Function : Generate a list of the best moves for black with weights
+ * Arguments: none
+ * Fails:   : never
+ * Returns  : list of moves with weights
+ */
+
 static int
 gtp_top_moves_black(char *s)
 {
@@ -2143,7 +2152,7 @@
  *
  * Status:    GTP version 2 standard command.
  *
- * FIXME: This command stores the time settings but noone ever takes notice.
+ * FIXME: This command stores the time settings but no one ever takes notice.
  */
 
 static int
@@ -3122,10 +3131,6 @@
  *
  * Warning: You had better know what you're doing if you try to use this
  *          command.
- *
- * FIXME: We should free the memory from the sgf tree, now that we are
- *        done. This would be simpler if there were an sgftree
- *        function for this. (sgfFreeNode works on an sgfnode.)
  */
 static int
 gtp_finish_sgftrace(char *s)
Index: gnugo/interface/play_ascii.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_ascii.c,v
retrieving revision 1.29
diff -u -r1.29 play_ascii.c
--- gnugo/interface/play_ascii.c        25 Sep 2002 13:22:20 -0000      1.29
+++ gnugo/interface/play_ascii.c        16 Oct 2002 19:34:53 -0000
@@ -561,29 +561,28 @@
   setbuf(stdout, NULL); /* else set it to completely UNBUFFERED */
 #endif
   
-  while (state == 1) {
+  sgftree = *tree;
 
-    sgftree = *tree;
-    
-    /* No score is estimated yet. */
-    current_score_estimate = NO_SCORE;
-    
-    if (filename) {
-      gameinfo_load_sgfheader(gameinfo, sgftree.root);
-      gameinfo->to_move = gameinfo_play_sgftree(gameinfo, &sgftree, until);
-      sgf_initialized = 1;
-    }
+  if (filename) {
+    gameinfo_load_sgfheader(gameinfo, sgftree.root);
+    gameinfo->to_move = gameinfo_play_sgftree(gameinfo, &sgftree, until);
+    sgf_initialized = 1;
+  }
+  else {
+    if (sgfGetIntProperty(sgftree.root, "SZ", &sz)) 
+      gnugo_clear_board(sz);
+    if (gameinfo->handicap == 0)
+      gameinfo->to_move = BLACK;
     else {
-      if (sgfGetIntProperty(sgftree.root, "SZ", &sz)) 
-       gnugo_clear_board(sz);
-      if (gameinfo->handicap == 0)
-       gameinfo->to_move = BLACK;
-      else {
-       gameinfo->handicap = gnugo_placehand(gameinfo->handicap);
-       gameinfo->to_move = WHITE;
-      }
-      sgf_initialized = 0;
+      gameinfo->handicap = gnugo_placehand(gameinfo->handicap);
+      gameinfo->to_move = WHITE;
     }
+    sgf_initialized = 0;
+  }
+
+  while (state == 1) {
+    /* No score is estimated yet. */
+    current_score_estimate = NO_SCORE;
     
     printf("\nBeginning ASCII mode game.\n\n");
     gameinfo_print(gameinfo);
@@ -926,6 +925,9 @@
       gnugo_who_wins(gameinfo->computer_player, stdout);
     printf("\nIf you disagree, we may count the game together.\n");
     printf("You may optionally save the game as an SGF file.\n");
+
+    sgftreeWriteResult(&sgftree, estimate_score(NULL, NULL), 1);
+
     state = 0;
     while (state == 0) {
       printf("\n");
@@ -967,12 +969,19 @@
        state = 0;
       }
     }
-    sgftreeWriteResult(&sgftree, estimate_score(NULL, NULL), 1);
     sgffile_output(&sgftree);
     passes = 0;
     showdead = 0;
+    
     /* Play a different game next time. */
     update_random_seed();
+
+    /* Free the sgf tree and prepare for a new game. */
+    sgfFreeNode(sgftree.root);
+    sgftree_clear(&sgftree);
+    sgftreeCreateHeaderNode(&sgftree, board_size, komi);
+
+    gameinfo_clear(gameinfo, board_size, komi);
   }
   printf("\nThanks for playing GNU Go.\n\n");
 }
Index: gnugo/interface/debugboard/main.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/debugboard/main.c,v
retrieving revision 1.10
diff -u -r1.10 main.c
--- gnugo/interface/debugboard/main.c   16 Sep 2002 07:27:49 -0000      1.10
+++ gnugo/interface/debugboard/main.c   16 Oct 2002 19:34:54 -0000
@@ -100,6 +100,7 @@
 
   /* Try to open the infile. */
   infilename = argv[1];
+  sgftree_clear(&sgftree);
   if (!sgftree_readfile(&sgftree, infilename)) {
     fprintf(stderr, "Cannot open or parse '%s'\n", infilename);
     exit(1);





reply via email to

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