#include <stdio.h>
#include "trie.h"
#include "globals.h"

#define ALLCHK 0x7FFFFFE

unsigned trielookup (t, s) register unsigned t; register char *s;
{
    register unsigned  *p;
foo: 
    if (!*s)
	return (t);
    if (!ptr (t))
	return (0);
    p = dawg + ptr (t);
    do {
	if (chr (*p) == chr (*s)) {
	    t = *p;
	    s++;
	    goto foo;
	}
    }
    while (!last (*p++));
    return (0);
}

unsigned checkout (s, score) char *s; unsigned *score;
{
    register unsigned   chek = 0,
                       *p;
    register char  *s1 = s;
    *score = 0;
    while (s1[-1])
	(*score) += lettervalue[*--s1];
 /* now s1 points to top of word */
    p = dawg + ptr (trielookup (root, s1));
    if (p == dawg)
	return (0);
    do
	if (term (trielookup (*p, s + 1)))
	    chek |= (1 << chr (*p));
    while (!last (*p++));
    while (s[1])
	(*score) += lettervalue[*++s];
    return (chek);
}
 
computechecks (fill, check, trans)
    char fill[17][17]; unsigned check[17][17], trans[17][17];
{
    unsigned    i,
                j;
    for (i = 1; i < 16; i++)
	for (j = 1; j < 16; j++) {
	    trans[j][i] = -1;
	    check[j][i] =
		 fill[i][j] ? 0
		:fill[i][j - 1] || fill[i][j + 1] ?
		     checkout (fill[i] + j, trans[j] + i)
		:ALLCHK;
	}
}
