import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.lang.Math;
public class WordSearchPuzzle
{
private char[][] puzzle;
private List<String> puzzleWords;
private List<String> wordsForGrid;
private int gridDimension;
private char [] alphabet;
public WordSearchPuzzle(List<String>userSpecifiedWords)
{
gridDimension = sizeOfGrid(puzzleWords);
this.puzzle = new char[gridDimension][gridDimension];//grid is going to be a square
this.puzzleWords=userSpecifiedWords;
}
public WordSearchPuzzle(String wordFile, int wordCount, int shortest, int longest)
{
int temp;
if(shortest>longest)
{
temp=longest;
longest=shortest;
shortest=temp;
}
puzzleWords = loadWordsFromFile(wordFile,shortest,longest);
for(int i=0;i<wordCount;i++)
{
int pos=(int)(Math.random()*puzzleWords.size());
wordsForGrid.add(puzzleWords.get(pos));
}
int gridDimension = sizeOfGrid(puzzleWords);
this.puzzle = new char[gridDimension][gridDimension];
puzzleWords=wordsForGrid;
}
public int sizeOfGrid(List<String> puzzleWords)
{
double temp=0;
int gridDimension;
for(int i=0;i<puzzleWords.size();i++)
{
temp+=puzzleWords.get(i).length();
}
temp =Math.sqrt(temp);
temp=(int)(temp*1.75);//scalling up by a factor of 1.75 as suggested;
gridDimension=(int)(temp);
return gridDimension;
}
/*public void fillGrid (List<String> puzzleWords)
{
for(int i=0;i<puzzleWords.size();i++)
{
}
}*/
private List<String> loadWordsFromFile(String wordFile, int shortest, int longest)
{
try
{
FileReader aFileReader = new FileReader(wordFile);
BufferedReader aBufferReader = new BufferedReader(aFileReader);
String lineFromFile;
List <String>words=new ArrayList<String>();//would not let me create an empty List, created an ArrayList insead.
lineFromFile = aBufferReader.readLine();
int wordLength;
while(lineFromFile!=null)
{
wordLength=lineFromFile.length();
if(wordLength>=shortest&&wordLength<=longest)
{
words.add(lineFromFile);
}
lineFromFile=aBufferReader.readLine();
}
aBufferReader.close();
aFileReader.close();
return words;
}
catch (IOException x)
{
return null;
}
}
public List<String> getWordSearchList()
{
return this.puzzleWords;
}
public char[][]getPuzzleAsGrid()
{
int i;
int j;
alphabet =new char []{('a'),('b'),('c'),('d'),('e'),('f'),('g'),('h'),('i'),('j'),('k'),('l'),('m'),('n'),('o'),('p'),('q'),('r'),('s'),('t'),('u'),('v'),('w'),('x'),('y'),('z')};
for(i=0;i<puzzle.length;i++)
{
for(j=0;j<gridDimension;j++)
{
int randomChar = (int)(Math.random()*alphabet.length);
this.puzzle[i][j]=alphabet[randomChar];
System.out.print(puzzle[i][j]+"");
}
System.out.println();
}
return puzzle;
}
public String getPuzzleAsString()
{
int i;
int j;
String puzzleString="";
for(i=0;i<gridDimension;i++)
{
for(j=0;j<gridDimension;j++)
{
puzzleString+=puzzle[i][j];
}
if(i==gridDimension)
{
puzzleString+="\n";
}
}
return puzzleString;
}
public void showWordSearchPuzzle()
{
generateWordSearchPuzzle();
for(int i=0;i<puzzleWords.size();i++)
{
System.out.println(puzzleWords.get(i));
}
}
public void generateWordSearchPuzzle()
{
int j=0;//to be used to check if the space where the current word is supposed to go is empty
int i;//to be used to go through the list of words
int k;//to be used to get the individual character from the current word
int randomDirection;//determine which direction to place the word
int randomRow;//starting position for word
int randomCol;//starting position for word
for(i=0;i<puzzleWords.size();)
{
randomDirection = (int)(Math.random()*4);
randomRow=(int)(Math.random()*gridDimension);
randomCol=(int)(Math.random()*gridDimension);
int randomColTemp = randomCol;
int randomRowTemp = randomRow;
if(randomDirection==0)//word being placed to the right
{
if(gridDimension-(randomCol+1)>=puzzleWords.get(i).length())
{
for(j=0;j<puzzleWords.get(i).length();j++)
{
if(puzzle[randomRow][randomColTemp]==0)
{
randomColTemp++;
}
else
j=puzzleWords.get(i).length()+1;//space is not empty,want to break the loop
}
}
if(j==puzzleWords.get(i).length())
{
for(k=0;k<puzzleWords.get(i).length();)
{
puzzle[randomRow][randomCol]=puzzleWords.get(i).charAt(k);
randomCol++;
k++;
}
i++;
}
}
if(randomDirection==1)//word being placed to the left)
{
if(randomCol+1>=puzzleWords.get(i).length())
{
for(j=0;j<puzzleWords.get(i).length();j++)
{
if(puzzle[randomRow][randomColTemp]==0)
{
randomColTemp--;
}
else
j=puzzleWords.get(i).length()+1;
}
}
if(j==puzzleWords.get(i).length())
{
for(k=0;k<puzzleWords.get(i).length();)
{
puzzle[randomRow][randomCol]=puzzleWords.get(i).charAt(k);
randomCol--;
k++;
}
i++;
}
}
if(randomDirection==2)//word being placed vertically down
{
if(gridDimension-(randomRow+1)>=puzzleWords.get(i).length())
{
for(j=0;j<puzzleWords.get(i).length();j++)
{
if(puzzle[randomRowTemp][randomCol]==0)
{
randomRowTemp++;
}
else
j=puzzleWords.get(i).length()+1;
}
}
if(j==puzzleWords.get(i).length())
{
for(k=0;k<puzzleWords.get(i).length();)
{
puzzle[randomRow][randomCol]=puzzleWords.get(i).charAt(k);
randomRow++;
k++;
}
i++;
}
}
if(randomDirection==3)//word being placed vertically up
{
if(randomRow+1>=puzzleWords.get(i).length())
{
for(j=0;j<puzzleWords.get(i).length();j++)
{
if(puzzle[randomRowTemp][randomCol]==0)
{
randomRowTemp--;
}
else
j=puzzleWords.get(i).length()+1;
}
}
if(j==puzzleWords.get(i).length())
{
for(k=0;k<puzzleWords.get(i).length();)
{
puzzle[randomRow][randomCol]=puzzleWords.get(i).charAt(k);
randomRow--;
k++;
}
i++;
}
}
}
for(int l=0;i<gridDimension;i++)
{
for(int m=0;j<gridDimension;j++)
{
if(puzzle[l][m]==0)
{
int randomAlphabeticChar = (int)(Math.random()*alphabet.length);
puzzle[l][m]=alphabet[randomAlphabeticChar];
}
}
}
}
}