[net.games.go] encode.c, decode.c, go.h

tbray@mprvaxa.UUCP (Tim Bray) (01/30/84)

x <-- USENET insecticide

= go.h ============================================================

/* magic numbers */

#define CLEAR 0		/* 2-bit encodings */
#define WHITE 1
#define BLACK 2
#define ROWS 23		/* picture dimensions */
#define COLUMNS 128
#define TOP 2		/* A1 co-ordinates */
#define LEFT 5

#define WHITECHAR 'O'   /* black stone looks like this */
#define BLACKCHAR '@'   /* white stone   "     "    "  */
#define CLEARCHAR '.'	/* empty point   "     "    "  */
#define CLEARHANDI '+'	/* handicap point "     "    "  */

unsigned char picture[ROWS][COLUMNS] = 
	{
        "     A B C D E F G H I J K L M O P Q R S T",
        "     | | | | | | | | | | | | | | | | | | |",
        " 1 -                                       -",
        " 2 -                                       -",
        " 3 -                                       -",
        " 4 -                                       -",
        " 5 -                                       -",
        " 6 -                                       -",
        " 7 -                                       -",
        " 8 -                                       -",
        " 9 -                                       -",
        "10 -                                       -",
        "11 -                                       -",
        "12 -                                       -",
        "13 -                                       -",
        "14 -                                       -",
        "15 -                                       -",
        "16 -                                       -",
        "17 -                                       -",
        "18 -                                       -",
        "19 -                                       -",
        "     | | | | | | | | | | | | | | | | | | |",
        "     Captured stones: %c %d, %c %d "	/*last line looks like this */
	};

unsigned char rep[200],expand[362];

= end of go.h ============================================================

= encode.c ===============================================================


/* encode - take a picture of a go board and produce a compressed
 *          representation.
 * T. Bray, Jan. 29, 1984.  This software is hereby made freely available
 *          to everyone, who are collectively welcomed to do anything to
 *          it that will not frighten horses in the street.
 */
#include <stdio.h>
#include "go.h"

main()

    {
    int i,row,col,value;
    char *cp;

    for(i=0; i<ROWS; i++)
	{
	if (gets(picture[i]) == NULL)
	    {
	    fprintf(stderr, "Board contains too few lines.\n");
	    exit();
	    }
	}

    /* shuffle the picture into the intermediate 'expand' array 
     */

    row = col = 0;
    for(i=0; i<361; i++)
	{
	switch (picture[row + TOP][(col++ * 2) + LEFT])
	    {
	    case CLEARCHAR: 	
	    case CLEARHANDI:
				expand[i] = CLEAR; break;
	    case BLACKCHAR: 	expand[i] = BLACK; break;
	    case WHITECHAR: 	expand[i] = WHITE; break;
	    case 0:
		fprintf(stderr,
			"Board row number %d is too short - exiting.\n",row);
		exit();
	    default:
		fprintf(stderr,
			"Illegal character on board: %c - exiting.\n", 
			picture[row + 1][(col - 1) * 2 + 2]);
		exit();
	    }

	if (col == 19)
	    {
	    row++;
	    col = 0;
	    }
	}

    /* shuffle 'expand' into the 2 points/hex digit representation 
     */
    for(i=0; i<181; i++)
	sprintf(rep + i, "%.1x", (expand[i * 2] << 2) | expand[(i * 2) + 1]);

    /* grab the last line with the captured stone counts 
     */
    for(cp = (char *) picture[ROWS - 1]; *cp != ':'; cp++);
    for(; *cp != BLACKCHAR; cp++);
    cp += 2;
    for(i=0; *cp != ' '; i++)
	rep[182 + i] = *cp++;
    for(; *cp != WHITECHAR; cp++);
    cp += 2;
    for(i=0; *cp != ' '; i++)
	rep[186 + i] = *cp++;

    write(1, rep, 200);
    close(1);
    }

= end of encode.c ==================================================

= decode.c =========================================================

/* decode - take a compressed representation of a go board and
 *          produce a picture thereof
 * T. Bray, Jan. 29, 1984.  This software is hereby made freely available
 *          to everyone, who are collectively welcomed to do anything to
 *          it that will not frighten horses in the street.
 */
#include <stdio.h>
#include "go.h"

main()

    {
    int i,row,col,value;
    unsigned char mark();

    if (read(0, rep, 200) != 200)
	{
	fprintf(stderr, "Can't read 200 bytes of representation - exiting.\n");
	exit();
	}

    /* shuffle the compressed form into the the intermediate 'expand' array
     */
    for(i=0; i<181; i++)
        {
	static unsigned char holder[3] = "  ";
	holder[0] = rep[i];				/* inefficient   */
	sscanf(holder, "%x", &value);			/*  hex-to-ascii */
	expand[(i * 2) + 1] = value & 3;		/* get two high bits */
	expand[i * 2] = value >> 2;			/* two low bits */
	}

    /* build the picture from 'expand'
     */
    row = col = 0;
    for(i=0; i<361; i++)
	{
	picture[row + TOP][(col++ * 2) + LEFT] = mark(i, expand[i]);
	if (col == 19)
	    {
	    row++;
	    col = 0;
	    }
	}

    /* draw
     */
    for(i = 0; i < (ROWS - 1); i++)
	printf("%s\n", picture[i]);
    rep[185] = rep[189] = 0;
    printf(picture[ROWS - 1], BLACKCHAR, atoi(rep + 182), 
			      WHITECHAR, atoi(rep + 186));
    printf("\n");
    }

/* figure out what to put in a spot.  Necessary for correctly showing
 * handicap points
 */
unsigned char
mark(place, value)

    int place, value;
    {
    switch (value)
	{
	case BLACK:	return(BLACKCHAR);
	case WHITE:	return(WHITECHAR);

	default: /* gotta  be clear */
	   if (place ==  60 || place ==  66 || place ==  72 ||
	       place == 174 || place == 180 || place == 186 ||
	       place == 288 || place == 294 || place == 300)
		return(CLEARHANDI);
	   else
		return(CLEARCHAR);
	}
    }

= end of decode.h ======================================================