gnugo-devel
[Top][All Lists]
Advanced

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

Re: [gnugo-devel] gtp.c integrity patch


From: Teun Burgers
Subject: Re: [gnugo-devel] gtp.c integrity patch
Date: Mon, 15 Oct 2001 19:02:32 +0200

Gunnar Farneback wrote:
> 
> This patch restores the integrity of gtp.c and gtp.h after Teun's
> reorientation patches. It's not tested.

In fact it does not even apply to 3.1.10

> Dan may choose whether to
> include it before or after 3.1.10. I hope Teun can verify that this
> patch makes no changes to the reorientation functionality.

I don't think it does, though the initial version was more
straightforward.
Attached is a working version of the patch.

> For the record, gtp.c and gtp.h should not depend on the rest of the
> engine. If you're modifying them and find you would need to include
> gnugo.h, liberty.h, or some other GNU Go include file, it's a warning
> sign that you're doing something wrong.

We could also make a new library libgo.h with platform independent code.
It could contain for a start gtp.c, random.c and the basic reorientation
functions.

Teun
Index: interface/gtp.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/gtp.c,v
retrieving revision 1.7
diff -u -r1.7 gtp.c
--- interface/gtp.c     2001/10/10 19:11:15     1.7
+++ interface/gtp.c     2001/10/15 16:54:23
@@ -41,7 +41,6 @@
 #include <string.h>
 #include <ctype.h>
 #include <assert.h>
-#include <gg_utils.h>
 
 #include "gtp.h"
 
@@ -58,8 +57,11 @@
  * unnecessarily inconvenient.
  */
 static int gtp_boardsize = -1;
-static int gtp_orientation = 0;
 
+/* Vertex transformation hooks. */
+static gtp_transform_ptr vertex_transform_input_hook = NULL;
+static gtp_transform_ptr vertex_transform_output_hook = NULL;
+
 /* Read filehandle gtp_input linewise and interpret as GTP commands. */
 void
 gtp_main_loop(struct gtp_command commands[], FILE *gtp_input)
@@ -118,18 +120,16 @@
   gtp_boardsize = size;
 }
 
-/* Set the orientation used in coordinate conversions. */
+/* If you need to transform the coordinates on input or output, use
+ * this functions to set hook functions which are called any time
+ * coordinates are read or about to be written. In GNU Go this is used
+ * to simulate rotated boards in regression tests.
+ */
 void
-gtp_internal_set_orientation(int orient)
-{
-  gtp_orientation = orient;
-}
-
-/* Set the orientation used in coordinate conversions. */
-int
-gtp_internal_get_orientation(void)
+gtp_set_vertex_transform_hooks(gtp_transform_ptr in, gtp_transform_ptr out)
 {
-  return gtp_orientation;
+  vertex_transform_input_hook = in;
+  vertex_transform_output_hook = out;
 }
 
 /*
@@ -335,7 +335,9 @@
   if (*i < 0 || *i >= gtp_boardsize || *j < 0 || *j >= gtp_boardsize)
     return 0;
 
-  rotate(*i, *j, i, j, gtp_boardsize, gtp_orientation);
+  if (vertex_transform_input_hook != NULL)
+    (*vertex_transform_input_hook)(*i, *j, i, j);
+
   return n;
 }
 
@@ -406,7 +408,12 @@
             || movej[k] < 0 || movej[k] >= gtp_boardsize)
       gtp_printf("??");
     else {
-      inv_rotate(movei[k], movej[k], &ri, &rj, gtp_boardsize, gtp_orientation);
+      if (vertex_transform_output_hook != NULL)
+       (*vertex_transform_output_hook)(movei[k], movej[k], &ri, &rj);
+      else {
+       ri = movei[k];
+       rj = movej[k];
+      }
       gtp_printf("%c%d", 'A' + rj + (rj >= 8), gtp_boardsize - ri);
     }
   }
Index: interface/gtp.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/gtp.h,v
retrieving revision 1.6
diff -u -r1.6 gtp.h
--- interface/gtp.h     2001/10/09 23:05:14     1.6
+++ interface/gtp.h     2001/10/15 16:54:23
@@ -54,6 +54,9 @@
 /* Function pointer for callback functions. */
 typedef int (*gtp_fn_ptr)(char *s, int id);
 
+/* Function pointer for vertex transform functions. */
+typedef void (*gtp_transform_ptr)(int ai, int aj, int *bi, int *bj);
+
 /* Elements in the array of commands required by gtp_main_loop. */
 struct gtp_command {
   const char *name;
@@ -62,8 +65,8 @@
 
 void gtp_main_loop(struct gtp_command commands[], FILE *gtp_input);
 void gtp_internal_set_boardsize(int size);
-void gtp_internal_set_orientation(int orient);
-int gtp_internal_get_orientation(void);
+void gtp_set_vertex_transform_hooks(gtp_transform_ptr in,
+                                   gtp_transform_ptr out);
 void gtp_mprintf(const char *format, ...);
 void gtp_printf(const char *format, ...);
 void gtp_printid(int id, int status);
Index: interface/play_gtp.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/interface/play_gtp.c,v
retrieving revision 1.29
diff -u -r1.29 play_gtp.c
--- interface/play_gtp.c        2001/10/15 02:17:05     1.29
+++ interface/play_gtp.c        2001/10/15 16:54:35
@@ -37,6 +37,7 @@
 #include "liberty.h"
 #include "gtp.h"
 #include "random.h"
+#include <gg_utils.h>
 
 /* Internal state that's not part of the engine. */
 int color_to_move;
@@ -50,6 +51,9 @@
 static void gtp_print_code(int c);
 static void gtp_print_vertices2(int n, int *moves);
 static int report_uncertainty = 0;
+static int gtp_orientation = 0;
+static void rotate_on_input(int ai, int aj, int *bi, int *bj);
+static void rotate_on_output(int ai, int aj, int *bi, int *bj);
 
 /* For undo, we keep the starting position, and a stack
  * listing all moves made.
@@ -148,7 +152,7 @@
   {"combination_attack",      gtp_combination_attack},
   {"connect",                gtp_connect},
   {"countlib",                       gtp_countlib},
-  {"cputime",                            gtp_cputime},
+  {"cputime",                            gtp_cputime},
   {"debug_influence",         gtp_debug_influence},
   {"debug_move_influence",    gtp_debug_move_influence},
   {"decrease_depths",                gtp_decrease_depths},
@@ -355,7 +359,8 @@
     return gtp_failure(id, "unacceptable orientation");
 
   clear_board();
-  gtp_internal_set_orientation(orientation);
+  gtp_orientation = orientation;
+  gtp_set_vertex_transform_hooks(rotate_on_input, rotate_on_output);
   store_position(&starting_position);
   return gtp_success(id, "");
 }
@@ -370,7 +375,7 @@
 {
   UNUSED(s);
 
-  return gtp_success(id, "%d", gtp_internal_get_orientation());
+  return gtp_success(id, "%d", gtp_orientation);
 }
 
 /***************************
@@ -512,7 +517,7 @@
   char untilstring[GTP_BUFSIZE];
   SGFNode *sgf;
   Gameinfo gameinfo;
-  int nread, orient;
+  int nread;
   
   nread = sscanf(s, "%s %s", filename, untilstring);
   if (nread < 1)
@@ -524,12 +529,12 @@
   gameinfo_clear(&gameinfo, 19, 5.5); /* Probably unnecessary. */
   gameinfo_load_sgfheader(&gameinfo, sgf);
 
-  orient = gtp_internal_get_orientation();
   if (nread == 1)
-    color_to_move = gameinfo_play_sgftree_rot(&gameinfo, sgf, NULL, orient);
+    color_to_move = gameinfo_play_sgftree_rot(&gameinfo, sgf, NULL,
+                                             gtp_orientation);
   else
     color_to_move = gameinfo_play_sgftree_rot(&gameinfo, sgf, untilstring,
-                                              orient);
+                                              gtp_orientation);
 
   gnugo_force_to_globals(&gameinfo.position);
   movenum = gameinfo.move_number;
@@ -2415,6 +2420,15 @@
   gtp_print_vertices(n, movei, movej);
 }
 
+static void rotate_on_input(int ai, int aj, int *bi, int *bj)
+{
+  rotate(ai, aj, bi, bj, board_size, gtp_orientation);
+}
+
+static void rotate_on_output(int ai, int aj, int *bi, int *bj)
+{
+  inv_rotate(ai, aj, bi, bj, board_size, gtp_orientation);
+}
 
 /*
  * Local Variables:

reply via email to

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