/*
 *
 * score.c -- find score for a turn
 *
 * This code is copyright 1995 by James A. Cherry, jac@doe.carleton.ca.
 * It is not to be redistributed or modified without his permission.
 *
 */

#include "funddefs.h"
#include "globals.h"
#include "proto.h"
#include "socket.h"


int FindNewWords( newlet, placed, xinc, yinc )
    struct plrlet *newlet;
    int placed, xinc, yinc;
/* Look for new words formed and find the score for them. */
{
    struct plrlet *currlet;
    int i, w, j;
    int dx, dy;
    int x, y;
    int score, mult, tsc, cptr, blloc;
    char c, ch;
    
    w = 0;
    dx = 1 - xinc;
    dy = 1 - yinc;
    for( i = 0; i <= placed; i++ ) {
	if( i < placed ) {
	    currlet = &(newlet[i]);
	    x = currlet->x;
	    y = currlet->y;
	} else {
	    x = newlet[0].x;
	    y = newlet[0].y;
	    dx = xinc;
	    dy = yinc;
	}
	if( ( y - dy > 0 && x - dx > 0 && Board[y - dy][x - dx] >= 'A' )
	    || ( y + dy < 16 && x + dx < 16 && Board[y + dy][x + dx] >= 'A' )
	    || i == placed ) {
	    while( Board[y - dy][x - dx] >= 'A' && x - dx > 0 && y - dy > 0 ) {
		x -= dx;
		y -= dy;
	    }
	    NewWords[w].startx = x;
	    NewWords[w].starty = y;
	    NewWords[w].dir = dy;
	    score = 0;
	    mult = 1;
	    cptr = 0;
	    blloc = -1;
	    for( ;; ) {
		c = Board[y][x];
		if( c >= 'A' ) {
		    if( ( x != B1X || y != B1Y ) && ( x != B2X || y != B2Y ) )
			score += Letters[c - 'A'].points;
		    NewWords[w].letters[cptr++] = c;
		} else {
		    for( j = 0; j < placed; j++ )
			if( x == newlet[j].x && y == newlet[j].y )
			    break;
		    if( j == placed ) break;
		    if( blloc == -1 ) blloc = cptr;
		    else blloc = -2;
		    ch = newlet[j].letter;
		    if( ch == CH_BL ) {
			tsc = 0;
			NewWords[w].letters[cptr++] = newlet[j].blankchar;
		    } else {
			tsc = Letters[ch - 'A'].points;
			NewWords[w].letters[cptr++] = ch;
		    }
		    if( c == CH_DL ) tsc *= 2;
		    else if( c == CH_TL ) tsc *= 3;
		    else if( c == CH_DW ) mult *= 2;
		    else if( c == CH_TW ) mult *= 3;
		    score += tsc;
		}
		x += dx;
		y += dy;
		if( x > 15 || y > 15 ) break;
	    }
	    score *= mult;
	    if( placed == 7 && i == placed ) score += 50;
	    NewWords[w].length = cptr;
	    NewWords[w].letters[cptr] = '\0';
	    NewWords[w].blankloc = blloc;
	    NewWords[w].score = score;
	    if( i != placed || cptr != 1 ) w++;
	}
    }
    return( w );
}

int BinSearch( word )
    char *word;
/* Look up a word in the dictionary. */
{
    long f, l, m;
    int found, n;
    
    for( n = 0; word[n] != '*' && word[n] != '\0'; n++ );
    f = WLen[strlen( word )][word[0] - 'A'];
    l = WLen[strlen( word )][word[0] - 'A' + 1] - 1;
    if( f > l ) return( 0 );
    found = 0;
    do {
	m = ( f + l ) / 2;
	if( strncmp( word, &(Words[WPtr[m]]), n ) > 0 )
	    f = m + 1;
	else if( strncmp( word, &(Words[WPtr[m]]), n ) < 0 )
	    l = m - 1;
	else {
	    found = 1;
	    f = m;
	    l = m;
	}
    } while( f < l );
    if( strncmp( word, &(Words[WPtr[f]]), n ) == 0 
	|| strncmp( word, &(Words[WPtr[l]]), n ) == 0 ) found = 1;
    return( found );
}

void DisplayWords( plr, newlet, nwords, placed )
    int plr, nwords, placed;
    struct plrlet *newlet;
/* Adjust board and tracking after a play is made. */
{
    int i, score;
    char tile;
    int x, y;
    
    tile = 0;
    score = 0;
    for( i = 0; i < nwords; i++ )
	score += NewWords[i].score;
    
    for( i = 0; i < placed; i++ ) {
	x = newlet[i].x;
	y = newlet[i].y;
	tile = newlet[i].letter;
	TilesLeft[Let2Ind( tile )]--;
	if( tile == CH_BL ) {
	    tile = newlet[i].blankchar;
	    if( B1X == 0 ) {
		B1X = x;
		B1Y = y;
	    } else {
		B2X = x;
		B2Y = y;
	    }
	}
	Board[y][x] = tile;
    }
    SortTiles( PlrTiles[plr] );
}

