/*
 *      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.
 */

/* counundrum.c - generates a conundrum */

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

/* choses a conundrum which is rearranged into a 5 letter word followed by a
   4 letter word
*/
void pick_conun(char *orig,char *mixed)
{
    const int sublen1=5,sublen2=4; /* required length of both subsets */
    int range,i,j,l,a,pos;
    char tcpy[10],chosen[10],buff[10],left[10],sub[5],fin[10];
    char *c;
    char done;
	
    range = word_pos[NL-1] - word_pos[NL-2];

    fin[0]='\0';
    /* loop round until a valid solution is found
       WARNING: this could take a long time, depending on the dictionary and
       values of various constants used. As default a solution is usually
       found on the first go, and hasn't been known to take more than 3 attempts.
       */
    while (strlen(fin)==0)
	{
	    int cn=1;
	    Boolean uniq=False;
		
	    fin[0]='\0';

	    while (!uniq)
		{
		    int l2=9,i2,j2;
		    char tcpy2[10];
		    char *c2;
		    Boolean done2;

		    /* chosen a random word of length NL */
		    strcpy(chosen,dict[(rand()%range) + word_pos[NL-2]]);

		    /* check that chosen word can't be arranged into a different word */
		    uniq=True;
		    for (i2=word_pos[l2-2]; (uniq) && (i2<word_pos[l2-1]); i2++)
			{
			    strcpy(tcpy2,chosen);
			    done2=False;
			    for (j2=0; ((!done2) && j2<l2); j2++)
				{
				    c2=strchr(tcpy2,dict[i2][j2]);
				    if (c2==NULL)
					done2=True;
				    else
					*c2=' ';	
				}
			    if ((!done2) && (strcmp(dict[i2],chosen)!=0))
				uniq=False;
			}
		}

#ifdef DEBUG
	    printf("Chosen: %s\n",chosen);
#endif
	    l=sublen1;
	    for (i=word_pos[l-2]; i<word_pos[l-1]; i++)
		{
		    strcpy(tcpy,chosen);
		    done=0;
		    for (j=0; ((!done) && j<l); j++)
			{
			    c=strchr(tcpy,dict[i][j]);
			    if (c==NULL)
				done=1;
			    else
				*c=' ';	
			}
		    if (!done)
			{
			    /* a word of length sublen1 has been found */
			    strcpy(buff,chosen);
			    /* find leftover letters from chosen NL letter word */
			    pos=0;
			    for (a=0; a<l; a++)
				if ((c=(strchr(buff,dict[i][a]))))
				    *c=' ';
			    for (a=0; a<NL; a++)
				if (buff[a]!=' ')
				    left[pos++]=buff[a];
			    left[pos]='\0';
			    /* use search() from lettround.c to find longest subword of
			       leftover letters
			       */
			    strcpy(sub,search(left,sublen2));
			    /* subword found must use all remaining letters */
			    if (strlen(sub)==sublen2)
				{
				    /* replace old solution with new one with probability
				       based on number of solutions found so far
				       */
				    if (((rand()%(cn++))+1)==1)
					{					
					    sprintf(fin,"%s%s",dict[i],sub);
					    if (!strcmp(fin,chosen))
						fin[0]='\0';
					}
				}
			}
		}
	}
    strcpy(orig,chosen);
    strcpy(mixed,fin);
}
