#include <setjmp.h>
#include "graphics.h"
#include "trie.h"
#include "globals.h"
#include <ctype.h>

/************************************

test wsc_*.dat file:

#..-...#...-..#
.+...=...=...+.
..+...-.-...+..
-..+...-...+..-
....+.....P....
.=...=...=A..=.
..-...-.-.R.-..
#..-..HIKES-..#
..-...-.-.N.-..
.=...=...=I..=.
....+.....P....
-..+...-...+..-
..+...-.-...+..
.+...=...=...+.
#..-...#...-..#
KLNAIRN 0 0

 ************************************/


static jmp_buf top;
static void parseargs(int ac, char *av[]);
static void initboard(void);

int main (int argc, char *argv[])
{
    srand (time (0));
    parseargs (argc, argv);

 /* Set up for doing screen output with no echoing of typein */
    initscr ();
/*
    raw ();
    noecho ();
*/
 /* Initialize the screen and the variables */
    setjmp (top);		/* Mark the stack */
/*
    erase ();
*/
    picture ();
    initboard ();
    initcommand ();
    exit(0);
    return(0);
}

void restart(void) {
    longjmp(top,0);
}

static void usage(char *prog);

static void parseargs(int ac, char *av[])
{
    int     i;

    for (i = 1; i < ac; i++) {
	if (*av[i] == '-')
	    switch (av[i][1]) {
		case 'l': 
		/* list words instead of playing scrabble */
		    i++;
		    listdictionary (i == ac ? "" : av[i]);
		    exit (0);
		case 's': 
		/* set random seed */
		    i++;
		    srand (atoi (av[i]));
		    break;
		default: 
		    usage (*av);
	    }
	else
	    usage (*av);
    }
}

static void usage(char *prog)
{
    fprintf (stderr, "Usage: %s [-l [prefix]]\n", prog);
    exit (1);
}

static char raw[17][17];

/* Initialize the board to be empty */
static void initboard(void)
{
    int     i,
            j, ch;
    for (i = 1; i <= LEN; ++i) {
	for (j = 1; j <= LEN; ++j) {
	    position (i, j);
	    removeletter ();
            ch = fgetc(stdin);
            if (!isalpha(ch)) {
              ch = '@';
	    }
	    raw[i][j] = ch;
	}
        ch = fgetc(stdin); /* skip newline */
    }

    /* Read board from .dat file */
    fillsock ();
    drawtiles (rack);
    /* Read rack from .dat file */
    drawtiles (yourrack);
    consecutivepasses = myscore = yourscore = 0;
    computermoving = anyrackempty = 0;
    for (i = 0; i < 17; i++) {
	acheck[i][0] = acheck[i][16] = acheck[0][i] = acheck[16][i] = 0;
        dcheck[i][0] = dcheck[i][16] = dcheck[0][i] = dcheck[16][i] = 0;
    }

    /* unfortunately, just placing the tiles on the board isn't
       good enough, because that does not correctly generate the
       crosscheck masks.  What we need to do is scan across and
       down the board, placing words one at a time, and modifying
       the place procedure to ignore the fact that words are
       not touching each other if we place them naively.  Reverse-
       engineering the order of play is not worth the effort, and
       a definitive match is not possible anyway */

    /* generate across words to be placed */
    for (i = 1; i <= LEN; ++i) {
        j = 1;
	for (j = 1; j <= LEN; ++j) {
          if ((isalpha(raw[i][j])) && (isalpha(raw[i][j+1]))) {
            /* print word at i,j */
            fprintf(stdout, "%c%02d Across: ", j+'A'-1, i);
            for (;;) {
              afill[i][j] = raw[i][j] | NEWBIT;
              fputc(raw[i][j++], stdout);
              if (!isalpha(raw[i][j])) {
                computechecks (afill, dcheck, dtrans);
                if (humanmove(0)) {
                  fprintf(stdout, "Woo hoo! 1\n");
		} else {
                  fprintf(stdout, "Boo hoo! 2\n");
		}
                break;
	      }
	    }
            fputc('\n', stdout);
	  } else j += 1;
          if (j > LEN) break;
	}
    }
    /* generate down words to be placed */
    for (j = 1; j <= LEN; ++j) {
        i = 1;
	for (i = 1; i <= LEN; ++i) {
          if ((isalpha(raw[i][j])) && (isalpha(raw[i+1][j]))) {
            /* print word at i,j */
            fprintf(stdout, "%c%02d Down: ", j+'A'-1, i);
            for (;;) {
              if (dfill[i][j] == 0) {
                dfill[i][j] = raw[i][j] | NEWBIT;
	      }
              fputc(raw[i++][j], stdout);
              if (!isalpha(raw[i][j])) {
                computechecks (dfill, acheck, atrans);
                if (humanmove(0)) {
                  fprintf(stdout, "Woo hoo! 3\n");
		} else {
                  fprintf(stdout, "Boo hoo! 4\n");
		}
                break;
	      }
	    }
            fputc('\n', stdout);
	  } else i += 1;
          if (i > LEN) break;
	}
    }
}