/*
 *      XCountdown - X version of the popular Channel 4 quiz show
 *
 * This software comes with NO warranty whatsoever. I therefore take no
 * responsibility for any damages, losses or problems caused through use
 * or misuse of this program.
 *
 * I hereby grant permission for this program to be freely copied and
 * distributed by any means, provided no charge is made for it.
 *
 * Matthew Chapman, csuoq@csv.warwick.ac.uk
 *    June 1995.
 */

/* xlettround.c - interface routines for the letters round */

#include "xcountdown.h"
#include "globals.h"

void LettRound(void);
void ScoreRound(char *targ);
void Win(int ply,int amount);
void SmugMode(char *targ,char *longest);
void DoLett(Widget w, XtPointer client_data, XtPointer call_data);

char targstr[NL+1];
Boolean in_letts;

/* main interface to letters game */
void LettRound()
{
    static char buff[1024];

    in_conundrum=False;
    in_letts=False;
    if (round>0)
    {
	sprintf(buff,"And now for round %d, another letters round.",
		round+1);
	DoMess(buff,3000);
    }
    InitRound();
    sprintf(buff,"Choosing the letters is %s.",
	    app_data.name[ply_go]);
    DoMess(buff,1000);
    in_letts=True;
    while(letts_chosen<NL)
	ProcessEvent();
    in_letts=False;
    targstr[letts_chosen]='\0';
    DoMess("Right, your time starts now.",3000);
    XtAppAddTimeOut(app_context,CLOCK_DELAY,Click,NULL);
    ChangeCarol(1);
    DoMess("Enter your longest word below...",3000);
    while (comp < 5000)
	ProcessEvent();
    DoMess("Right, time's up. Let's see how you did.",3000);
    ChangeCarol(2);
    ScoreRound(targstr);
    Wait(6000);
    ChangeCarol(2);
    round++;
    ply_go=1-ply_go;
}

/* validate, verify and score the round */
void ScoreRound(char *targ)
{
    String ans;
    char word[NP][NL+1],tcpy[NL+1],buff[1024];
    int a,i=ply_go,j,l0,l1;
    char *c;
    Boolean done;
    enum { VALID,INVALID,WRONGLETT };
    int isvalid[NP]={INVALID,INVALID};

    for (a=0; a<NP; a++)
    {
	WaitForMess();
	XtVaGetValues((a==0) ? input1[a] : input2[a],XtNstring,&ans,NULL);
#ifdef DEBUG
	printf("ans=%s.\n",ans);
#endif
	strncpy(word[a],ans,NL);
	word[a][NL]='\0';
	if ((int)strlen(word[a])>0)
	{
	    XawTextBlock txt;
	    /* show answer to other player */
	    txt.firstPos=0;
	    txt.format=FMT8BIT;
	    txt.length=strlen(word[a]);
	    txt.ptr=word[a];
	    XawTextReplace((a==0) ? input1[1-a] : input2[1-a],0,NL,&txt);
		
	    /* convert to upper case */
	    for (j=0; j<(int)strlen(word[a]); j++)
		word[a][j]=toupper(word[a][j]);
	    /* remove any trailing spaces */
	    while (word[a][strlen(word[a])-1]==' ')
		word[a][strlen(word[a])-1]='\0';
	}
    }

    for (a=0; a<NP; a++)
    {
	strcpy(tcpy,targ);
	done=False;
	for (j=0; ((!done) && j<(int)strlen(word[i])); j++)
	{
	    c=strchr(tcpy,word[i][j]);
	    if (c==NULL)
		done=True;
	    else
		*c=' ';	
	}
	if (!done)
	{
	    Boolean valid;
	    /* look up word in dictionary, allowing for 1 letter exceptions */
	    if ((int)strlen(word[i])==1)
		valid=((word[i][0]=='A') || (word[i][0]=='I'));
	    else
		valid=(((int)strlen(word[i])>1) && wordsearch(word[i]));

	    if (valid)
		isvalid[i]=VALID;
	    else
		isvalid[i]=INVALID;
	}
	else
	    isvalid[i]=WRONGLETT;

	i=1-i;
    }

    i=ply_go;
    if ((isvalid[i]==VALID) && (isvalid[1-i]==VALID))
    {
	if (strcmp(word[i],word[1-i])==0)
	    DoMess("That is a valid word. Well done.",3000);
	else
	    DoMess("Both words are valid. Well done.",3000);
    } else
	if ((isvalid[i]==VALID) &&
	    ((isvalid[1-i]==INVALID) || (isvalid[1-i]==WRONGLETT)))
	{
	    if (strlen(word[1-i])==0)
		sprintf(buff,"Your word is fine %s.",app_data.name[i]);
	    else
		sprintf(buff,"Your word is fine %s, but %s %s.",
			app_data.name[i],word[1-i],(isvalid[1-i]==INVALID) ?
			"is not a valid word" : "uses invalid letters");
	    DoMess(buff,6000);
	} else
	    if (((isvalid[i]==INVALID) || (isvalid[i]==WRONGLETT)) &&
		(isvalid[1-i]==VALID))
	    {
		if (strlen(word[i])==0)
		    sprintf(buff,"Your word is fine %s.",app_data.name[1-i]);
		else
		    sprintf(buff,"Your word is fine %s, but %s %s.",
			    app_data.name[1-i],word[i],(isvalid[i]==INVALID) ?
			    "is not a valid word" : "uses invalid letters");
		DoMess(buff,6000);
	    } else
		if ((strlen(word[i])==0) && (strlen(word[1-i])==0))
		{
		    DoMess("Oh dear, no words at all.",3000);
		} else
		    if ((isvalid[i]==INVALID) && (isvalid[1-i]==INVALID))
		    {
			if ((strlen(word[i])==0) || (strlen(word[1-i])==0))
			{
			    sprintf(buff,"Sorry, %s is not in the dictionary, %s.",
				    word[((strlen(word[i])==0) ? 1-i : i)],app_data.name[
											 ((strlen(word[i])==0) ? 1-i : i)]);
			    DoMess(buff,3000);
			}
			else
			{
			    if (strcmp(word[i],word[1-i])!=0)
				DoMess("Sorry, neither word is in the dictionary.",3000);
			    else
				DoMess("Sorry, that word is not in the dictionary.",3000);
			}
		    } else
			if ((isvalid[i]==WRONGLETT) && (isvalid[1-i]==WRONGLETT))
			{
			    DoMess("You have both used letters which are not available.",3000);
			} else
			    if ((isvalid[i]==INVALID) && (isvalid[1-i]==WRONGLETT))
			    {
				if (strlen(word[i])==0)
				    sprintf(buff,"Sorry, %s uses invalid letters.",word[1-i]);
				else
				    sprintf(buff,"%s is not a valid word, and %s uses invalid letters.",
					    word[i],word[1-i]);
				DoMess(buff,6000);
			    } else
				if ((isvalid[1-i]==INVALID) && (isvalid[i]==WRONGLETT))
				{
				    if (strlen(word[1-i])==0)
					sprintf(buff,"Sorry, %s uses invalid letters.",word[i]);
				    else
					sprintf(buff,"%s is not a valid word, and %s uses invalid letters.",
						word[1-i],word[i]);
				    DoMess(buff,6000);
				}


    l0=strlen(word[0]);
    l1=strlen(word[1]);
    if (isvalid[0]==VALID && isvalid[1]==VALID)
    {
	if (l0==l1)
	{
	    if (l0==9)
	    {
		sprintf(buff,"Both contestants score 18 points for getting a 9 letter word!");
		l0=18; l1=18;
	    }
	    else
		sprintf(buff,"So both contestants score %d point%s.",
			(int)strlen(word[0]),((int)strlen(word[0])>1) ? "s" : "");
	    DoMess(buff,4000);
	    UpdateScore(0,l0);
	    UpdateScore(1,l1);
	}
	else if (l0>l1)
	    Win(0,l0);
	else
	    Win(1,l1);
    }
    else if (isvalid[0]==VALID)
	Win(0,l0);
    else if (isvalid[1]==VALID)
	Win(1,l1);
    else
	DoMess("So neither contestant scores any points.",3000);
    if (isvalid[0]!=VALID) { l0=0; word[0][0]='\0'; }
    if (isvalid[1]!=VALID) { l1=0; word[1][0]='\0'; }
    Wait(3000);
    /* pass in longest word found */
    SmugMode(targ,word[(l0>=l1) ? 0 : 1]);
}

/* given player has entered a correct word, scoring the given amount */
void Win(int ply,int amount)
{
    char buff[1024];

    if (amount==9)
    {
	sprintf(buff,"%s receives a special score of 18 for getting a 9 letter word!",
		app_data.name[ply]);
	amount=18;
    }
    else 
	sprintf(buff,"So %s scores %d point%s.",app_data.name[ply],amount,
		(amount>1) ? "s" : "");
    DoMess(buff,4000);
    UpdateScore(ply,amount);
}

/* calls computer search, and displays a longer word if it finds one */
void SmugMode(char *targ,char *longest)
{
    char new_long[NL+1],tcpy[NL+1],buff[1024];
    int start,i,j,k;

#ifdef DEBUG	
    printf("Longest=%s.\n",longest);
    printf("targ=%s\n",targ);
#endif
    strcpy(new_long,search(targ,NL));
#ifdef DEBUG
    printf("new long=%s.\n",new_long);
#endif
    WaitForMess();
    ChangeCarol(3);
    if ((int)strlen(new_long)>(int)strlen(longest))
    {
	sprintf(buff,"There is a%s %d letter word: %s",(strlen(new_long)==8) ?
		"n" : "",strlen(new_long),new_long);
	DoMess(buff,3000);
    }
    else
    {
	DoMess("I couldn't find anything better.",3000);
	strcpy(new_long,longest);
    }
    if ((int)strlen(new_long)>0)
    {
	start=(NL-(int)strlen(new_long))/2;
	for (i=0; i<NP; i++)
	{
	    strcpy(tcpy,targ);
	    for(j=0;j<(int)strlen(new_long);j++)
	    {
		sprintf(buff,"%c",new_long[j]);
		XtVaSetValues(lett[i][j+start][1],XtNlabel,buff,SETBG(steelblue),NULL);
		for (k=0; new_long[j]!=tcpy[k]; k++)
		    ;
		XtVaSetValues(lett[i][k][0],XtNlabel," ",SETBG(gray40),NULL);
		tcpy[k]=' ';
	    }
	}
    }
}

/* callback for Vowel button or Consonant button */
void DoLett(Widget w, XtPointer client_data, XtPointer call_data)
{
    if (in_letts && IsClear() && letts_chosen<NL && XtDisplay(w)==dpy[ply_go])
    {
	char buff[1024],chosen;
	int i;

	if (w==vbutt[ply_go])
	    chosen=GiveVowel();
	else
	    chosen=GiveConsonant();

	sprintf(buff,"%c",chosen);
	for(i=0;i<NP;i++)
	    XtVaSetValues(lett[i][letts_chosen][0],XtNlabel,buff,SETBG(steelblue),NULL);
	targstr[letts_chosen++]=chosen;
	sprintf(buff,"The letter %c.",chosen);	
	DoMess(buff,500);
    }
}
