Click here to Skip to main content
15,849,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am placing a word character by character into a word puzzle. The method generates a random position and random direction in which to place my word in the grid. It first tests if that space is free before adding in the word. My problem is that if it finds that the position is not free, it doesn't try a second time to place that word, it moves on to the next word to be entered. How can I tell it to keep trying with a new random starting position and direction until it finds a space?

This section of the code shows an example of generating the random start position and one of the random directions.

gridDimension is the sise of the grid determined earlier int the code.
puzzle is my 2d array.
puzzleWords is my list of words to be placed in the grid.



C#
private void generateWordSearchPuzzle()
  {

     int randRowPos=0, randColPos=0, randDirection=0, i=0,j=0, wordLength, freeSpace;


      while(i < this.puzzleWords.size())
      {
          // random directions: 0= down, 1= up, 2= right, 3= left
          randDirection= (int) (Math.random()*4);
          randRowPos= (int)(Math.random()*this.gridDimension);
          randColPos= (int)(Math.random()*this.gridDimension);

          int randRowTemp = randRowPos;
          int randColTemp = randColPos;

         if(randDirection == 0)//down
         {
           if((this.puzzle[0].length - (randRowPos +1) >= this.puzzleWords.get(i).length()) && (Character.isLetter( this.puzzle[randRowPos][randColPos])==false))
           {
               for(freeSpace=0; freeSpace < this.puzzleWords.get(i).length(); freeSpace++)
               {
                   if(Character.isLetter( this.puzzle[randRowTemp][randColPos])==false)
                   {
                       randRowTemp++;
                   }
                   else
                   {
                       freeSpace= this.puzzleWords.get(i).length()+1;
                   }
               }
               if(freeSpace== this.puzzleWords.get(i).length())
               {
                   int w= this.puzzleWords.get(i).length()-1;

                   while(w >= 0)
                   {
                       this.puzzle[randRowPos][randColPos]= puzzleWords.get(i).charAt(w);
                       randRowPos++;
                       w--;
                   }
                   i++;
               }
           }
         }
Posted

1 solution

Java
while(i < this.puzzleWords.size())


you are trying each word once.

add another while loop inside your existing while loop, defined to end when the position is found:

Java
while(i < this.puzzleWords.size()) { // each word

  while(positionNotFound){ //search until...
 
    // your code
    // .....
    //....
    if(positionFound){
      break; //leave inner while
    }
  }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900