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

static jmp_buf top;

main (argc,argv) 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 ();

}

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

static parseargs(ac, av) 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 usage (prog) char *prog;
{
    fprintf (stderr, "Usage: %s [-l [prefix]]\n", prog);
    exit (1);
}


/* Initialize the board to be empty */
static initboard ()
{
    int     i,
            j;
    for (i = 1; i <= LEN; ++i)
	for (j = 1; j <= LEN; ++j) {
	    position (i, j);
	    removeletter ();
	    afill[i][j] = dfill[i][j] = 0;
	}
    fillsock ();
    drawtiles (rack);
    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;
    computechecks (dfill, acheck, atrans);
    computechecks (afill, dcheck, dtrans);
}
