Good morning,

I have been enjoying gnugo for a while. I especially appreciate the ascii mode.
Unfortunately, the goban in ascii mode is somehow a bit hard to read.

Therefore, to improve the readibility, I have patched the code to add the 
support of colors using the ansi escape codes. 

I have attached my patch to this mail in case it could be useful.

Comments are also welcome :)

I am also considering to contribute more when having more time.

Have a nice day!
Regards.

Stephane WEISS
diff -rupN gnugo/engine/globals.c gnugo_patched/engine/globals.c
--- gnugo/engine/globals.c	2014-06-11 13:40:19.221769359 +0200
+++ gnugo_patched/engine/globals.c	2014-06-11 14:06:41.716529583 +0200
@@ -98,6 +98,7 @@ int output_flags      = OUTPUT_DEFAULT;
 int metamachine       = 0;  /* use metamachine_genmove */
 int oracle_exists     = 0;  /* oracle is available for consultation   */
 int autolevel_on      = 0;  /* Adjust level in GMP or ASCII mode. */
+int ansi_escape_codes = 0;  /* Add the support for color display through ansi escape codes. */
 
 int disable_threat_computation = 0;
 int disable_endgame_patterns   = 0;
diff -rupN gnugo/engine/gnugo.h gnugo_patched/engine/gnugo.h
--- gnugo/engine/gnugo.h	2014-06-11 13:40:19.225102675 +0200
+++ gnugo_patched/engine/gnugo.h	2014-06-11 14:06:51.673142310 +0200
@@ -221,6 +221,7 @@ extern int mirror_stones_limit;      /*
 extern int gtp_version;              /* version of Go Text Protocol */
 extern int use_monte_carlo_genmove;  /* use Monte Carlo move generation */
 extern int mc_games_per_level;       /* number of Monte Carlo simulations per level */
+extern int ansi_escape_codes;	     /* support colors in terminal through ansi escape codes */
 
 /* Mandatory values of reading parameters. Normally -1, if set
  * these override the values derived from the level. */
diff -rupN gnugo/interface/main.c gnugo_patched/interface/main.c
--- gnugo/interface/main.c	2014-06-11 13:40:19.318435502 +0200
+++ gnugo_patched/interface/main.c	2014-06-11 14:06:13.313350123 +0200
@@ -161,7 +161,8 @@ enum {OPT_BOARDSIZE = 127,
       OPT_MC_GAMES_PER_LEVEL,
       OPT_MC_PATTERNS,
       OPT_MC_LIST_PATTERNS,
-      OPT_MC_LOAD_PATTERNS
+      OPT_MC_LOAD_PATTERNS,
+      OPT_ANSI_ESCAPE_CODES
 };
 
 /* names of playing modes */
@@ -312,6 +313,7 @@ static struct gg_option const long_optio
   {"mc-patterns",    required_argument, 0, OPT_MC_PATTERNS},
   {"mc-list-patterns", no_argument,     0, OPT_MC_LIST_PATTERNS},
   {"mc-load-patterns", required_argument, 0, OPT_MC_LOAD_PATTERNS},
+  {"ansi-escape-codes", no_argument, 0, OPT_ANSI_ESCAPE_CODES},
   {NULL, 0, NULL, 0}
 };
 
@@ -933,6 +935,10 @@ main(int argc, char *argv[])
         mirror_stones_limit = atoi(gg_optarg);
         break;
 
+      case OPT_ANSI_ESCAPE_CODES:
+	ansi_escape_codes = 1;
+	break;
+
       case 'v':
 	show_version();
 	show_copyright();
@@ -1545,6 +1551,7 @@ Experimental options:\n\
    --standard-connections\n\
    --standard-semeai\n\
    --oracle                Read the documentation\n\
+   --ansi-escape-codes     enable colors in ascii mode through ansi escape codes\n\
 \n\
 Cache size (higher=more memory usage, faster unless swapping occurs):\n\
    -M, --cache-size <megabytes>  RAM cache for read results (default %4.1f Mb)\n\
diff -rupN gnugo/interface/play_ascii.c gnugo_patched/interface/play_ascii.c
--- gnugo/interface/play_ascii.c	2014-06-11 13:40:19.318435502 +0200
+++ gnugo_patched/interface/play_ascii.c	2014-06-11 14:05:57.800100832 +0200
@@ -38,6 +38,20 @@
 #include "sgftree.h"
 #include "gg_utils.h"
 
+
+#define BSTONE_SYM "X"
+#define DEAD_BSTONE_SYM "x"
+#define WSTONE_SYM "O"
+#define DEAD_WSTONE_SYM "o"
+#define EOC "\033[0m";
+
+static char *BSTONE;
+static char *WSTONE;
+static char *DEAD_BSTONE;
+static char *DEAD_WSTONE;
+static char *HL_BSTONE;
+static char *HL_WSTONE;
+
 #define DEBUG_COMMANDS "\
 capture <pos>    try to capture indicated group\n\
 dead             Toggle display of dead stones\n\
@@ -99,6 +113,28 @@ make_letterbar(int boardsize, char *lett
 }
 
 
+/* adding support for color in terminal using  */
+static void
+init_ansi_stones(){
+
+if (ansi_escape_codes == 1){
+	BSTONE="\033[31;1m" BSTONE_SYM  EOC;
+	WSTONE="\033[34;1m" WSTONE_SYM EOC;
+	DEAD_BSTONE="\033[31;1m" DEAD_BSTONE_SYM EOC;
+	DEAD_WSTONE="\033[34;1m" DEAD_WSTONE_SYM EOC;
+	HL_BSTONE="\033[31;1;5m " BSTONE_SYM " " EOC;
+	HL_WSTONE="\033[34;1;5m " WSTONE_SYM " " EOC;
+}else{
+	BSTONE=BSTONE_SYM;
+	WSTONE=WSTONE_SYM;
+	DEAD_BSTONE=DEAD_BSTONE_SYM;
+	DEAD_WSTONE=DEAD_WSTONE_SYM;
+	HL_BSTONE="(" BSTONE_SYM ")";
+	HL_WSTONE="(" WSTONE_SYM ")";
+}
+}
+
+
 /* This array contains +'s and -'s for the empty board positions.
  * hspot_size contains the board size that the grid has been
  * initialized to.
@@ -202,10 +238,8 @@ ascii_showboard(void)
   set_handicap_spots(board_size);
 
   printf("\n");
-  printf("    White (O) has captured %d stone%s\n", black_captured,
-	 black_captured == 1 ? "" : "s");
-  printf("    Black (X) has captured %d stone%s\n", white_captured,
-	 white_captured == 1 ? "" : "s");
+  printf("    White (%s) has captured %d pieces\n", WSTONE, black_captured);
+  printf("    Black (%s) has captured %d pieces\n", BSTONE, white_captured);
   if (showscore) {
     if (current_score_estimate == NO_SCORE)
       printf("    No score estimate is available yet.\n");
@@ -248,20 +282,20 @@ ascii_showboard(void)
 	  printf(" %c", hspots[i][j]);
 	  last_pos_was_move = 0;
 	  break;
-	case BLACK:
-	  printf(" %c", dead ? 'x' : 'X');
+	case BLACK: 
+	  printf(" %s",  dead ? DEAD_BSTONE : BSTONE );
 	  last_pos_was_move = 0;
 	  break;
 	case WHITE:
-	  printf(" %c", dead ? 'o' : 'O');
+	  printf(" %s", dead ? DEAD_WSTONE : WSTONE );
 	  last_pos_was_move = 0;
 	  break;
 	case BLACK+128:
-	  printf("(%c)", 'X');
+	  printf("%s", HL_BSTONE);
 	  last_pos_was_move = 256;
 	  break;
 	case WHITE+128:
-	  printf("(%c)", 'O');
+	  printf("%s", HL_WSTONE);
 	  last_pos_was_move = 256;
 	  break;
 	case EMPTY+256:
@@ -269,11 +303,11 @@ ascii_showboard(void)
 	  last_pos_was_move = 0;
 	  break;
 	case BLACK+256:
-	  printf("%c", dead ? 'x' : 'X');
+	  printf("%s", dead ? DEAD_BSTONE : BSTONE );
 	  last_pos_was_move = 0;
 	  break;
 	case WHITE+256:
-	  printf("%c", dead ? 'o' : 'O');
+	  printf("%s", dead ? DEAD_WSTONE : WSTONE );
 	  last_pos_was_move = 0;
 	  break;
 	default: 
@@ -612,6 +646,8 @@ play_ascii(SGFTree *tree, Gameinfo *game
     sgf_initialized = 0;
   }
 
+  init_ansi_stones();
+
   do_play_ascii(gameinfo);
   printf("\nThanks! for playing GNU Go.\n\n");
 
_______________________________________________
gnugo-devel mailing list
gnugo-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/gnugo-devel

Reply via email to