As Paul has stated, recursion is the answer to building this generator
correctly.

But just as a sanity check, have you traced out the combinations that your
currect generator is attempting? That may put some light on where it was
breaking. If for nothing else, it will help you understand where the logic
broke down. Learning from our mistakes is always helpful.

Charles P.



On Mon, Jun 1, 2009 at 6:59 AM, Paul Andrews <p...@ipauland.com> wrote:

> I've realised that there needs to be further loops around the layout code.
> For the crossword grid a viable set of horizontal  layout positions may
> start at a given (x,y) position and the solution should search in terms of
> increasing x, then y, so the code might then go something like this..
>
> layoutFrom=Point(0,0); // top left
>
> while  (layoutFrom!=null)
> {
>   newSolution = layoutHorizontalWord(layFrom, currentWord,
> currentSolution);
>   if (newSolution.isValid())
>   {
>       newSolution = layoutXword(words, newSolution );
>       if (newSolution.isValid())
>           return newSolution; // solved
>    }
>   layoutFrom= newSolution.getNextPosition();     // returns the next x,y
> position after the last position used, or null if none left.
> }
>
> Paul
>
>
>
>
>
> ----- Original Message ----- From: "Paul Andrews" <p...@ipauland.com>
> To: "Flash Coders List" <flashcoders@chattyfig.figleaf.com>
> Sent: Monday, June 01, 2009 11:14 AM
> Subject: Re: [Flashcoders] Crossword generator advice
>
>
>  You should use recursion.
>>
>> At each stage you should loop through the available words, trying to lay
>> them onto the crossword area.
>>
>> Paul
>>
>> It goes a bit like this:
>>
>> var wordList = ["word1","word2","word3", ..   ];
>> var xwordSolution:XwordSolution;
>> var currentSolution= new XwordSolution(maxWidth, maxHeight); // emptyGrid
>>
>> xwordSolution = layoutXword(wordList, currentSolution);
>>
>> if (xwordSolution.isValid())
>> trace("Solved!")
>> else
>> trace("no solution found");
>>
>> function layoutXword(words:Array,
>> currentSolution:XwordSolution):XwordSolution
>> {
>> while (words.length > 0){
>> currentWord = words.pop();
>>
>> // horizontal
>>
>> newSolution = layoutHorizontalWord(currentWord, currentSolution);
>> if (newSolution.isValid())
>> {
>>   newSolution = layoutXword(words, newSolution );
>>   if (newSolution.isValid())
>>       return newSolution; // solved
>> }
>> // the horizontal layout wasn't possible, try vertical
>> newSolution = layoutVerticalWord(currentWord, currentSolution);
>> if (newSolution.isValid())
>> {
>>   newSolution = layoutXword(words, newSolution );
>>   if (newSolution.isValid())
>>       return newSolution; // solved
>> }
>> // couldn't layout this word, try the next
>> }
>> // Couldn't layout any of the words - solution failed.
>> return newSolution; // this will be failed
>> }
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> ----- Original Message ----- From: "Paul Steven" <
>> paul_ste...@btinternet.com>
>> To: "'Flash Coders List'" <flashcoders@chattyfig.figleaf.com>
>> Sent: Monday, June 01, 2009 10:33 AM
>> Subject: RE: [Flashcoders] Crossword generator advice
>>
>>
>>  I am still struggling to find a solution to this crossword generator and
>>> could do with some advice on a possible solution I want to try.
>>>
>>> If I enter my list of 16 words in a particular order (an order based on
>>> what
>>> looks like the order the solution would place them), the generator
>>> creates
>>> the puzzle in a matter of seconds.
>>>
>>> However if I enter them in a different order, the generator always breaks
>>> out of the loop by my exit condition (number of loops > 5000). I am
>>> assuming
>>> this is either due to an error in my code or due to the fact there are so
>>> many combinations to try.
>>>
>>> I am therefore thinking a possible solution is to try every permutation
>>> (I
>>> think this is the right word) of the 16 words. If I am correct there
>>> would
>>> be 240 possible permutations. ((X - 1) * X) where X is the number of
>>> items
>>> in the list.
>>>
>>> I am not sure however how to create the 240 variations of the list.
>>>
>>> Any advice on how to create these 240 permutations of the list would be
>>> much
>>> appreciated.
>>>
>>> Thanks
>>>
>>> Paul
>>>
>>> _______________________________________________
>>> Flashcoders mailing list
>>> Flashcoders@chattyfig.figleaf.com
>>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>>
>>
>> _______________________________________________
>> Flashcoders mailing list
>> Flashcoders@chattyfig.figleaf.com
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to