/*

Level element       Character	ASCII Code
Wall	                #	   0x23
Player	                @	   0x40
Player on goal square	+	   0x2b
Box	                $	   0x24
Box on goal square	*	   0x2a
Goal square	        .	   0x2e
Floor	              (Space)	   0x20
 */

#include <stdio.h>
#include <stdlib.h>

char map[64][64];

int main(int argc, char **argv)
{
  int game = 0;
  int width, height;
  int c;
  int x, y;
  int icons;

  c = fgetc(stdin);
  if (c == EOF) exit(1);
  fprintf(stdout, "// Both of these are +2 greater than play area due to boundary wall:\n");
  fprintf(stdout, "#define WIDTH(game) (strlen(game[0]))\n");
  fprintf(stdout, "#define HEIGHT(game) (sizeof(game)/sizeof(game[0]))\n\n");
  fprintf(stdout, "#ifndef NULL\n#define NULL 0\n#endif\n\n");
  for (;;) { // all problems
    icons = 0;
    width = 0; height = 0;
    for (y = 0; y < 64; y++) {
      for (x = 0; x < 64; x++) {
	map[x][y] = ' ';
      }
    }
    x = 0; y = 0;
    if (c == ';') {
      do { c = fgetc(stdin); /*fputc(c, stderr);*/ } while (c != '\n');
    }
    for (;;) { // all lines in one problem
      x = 0;
      for (;;) { // one line
        if (c == '\r') { x = 0; c = fgetc(stdin); /*fputc(c, stderr);*/ continue; }
        if ((c == '\n') && (x == 0)) { x = 0; c = fgetc(stdin); /*fputc(c, stderr); */continue; }
        if ((c == EOF) || (c == '\n') || (c == ';')) break;
        map[x++][y] = c; if (x >= 64) { fprintf(stderr, "Error: long line forced exit\n"); exit(1);}
        if ((c != '+') && (c != '|') && (c != '-') && (c != ' ')) icons += 1;
        if ((c == '?') || (c == '*')) icons += 1; // X on Y  Oops - reused '+' by accident
	c = fgetc(stdin);
      }
      if (x > width) width = x;
      y++;
      if (y > height) height = y;
      if ((c == EOF) || (c == ';')) {
	if ((height-2 <= 16) && (width-2 <= 16) && (icons <= 38)) {
          fprintf(stdout, "// Problem is %d across by %d down (excluding boundaries) and needs %d icons\n\n", width-2, height-3, icons);
          fprintf(stdout, "const char const *game%0d[%d] __attribute__ ((section(\".text\"))) = {\n", game, height-1);
	  game += 1;
          for (y = 0; y < height-1; y++) {
	    fprintf(stdout, "  \"");
	    for (x = 0; x < width; x++) {
	      fputc(map[x][y], stdout);
	    }
	    if (y ==  height-2) fprintf(stdout, "\"\n};\n"); else fprintf(stdout, "\",\n");
          }
          fputc('\n', stdout);
	}
        x = y = width = height = icons = 0;
        if (c == EOF) {
	  int i;
          fprintf(stdout, "#define NUM_GAMES %d\nconst char const * const *game[NUM_GAMES+1] __attribute__ ((section(\".text\"))) = {\n",game);
	  for (i = 0; i < game; i++) {
	    fprintf(stdout, "  game%0d,\n", i);
	  }
          fprintf(stdout, "  NULL\n};\n");
	  exit(0);
	}
	fflush(stdout);
	break; // go on to next block
      }
    }
  }
}
