import java.util.Scanner;
public class WordSearch
{
static long seed;
static WordBoard board;
public static void main(String[] args)
{
board = new WordBoard(10, 10, seed);
System.out.println(WordSearch.findWords(10, 10, seed));
System.out.println(board.getSeed());
}
public static long promptUserForSeed(Scanner input)
{
System.out.println("Enter seed:");
seed = input.nextLong();
return seed;
}
public static WordBoard findWords(int rows, int cols, long seed)
{
board = new WordBoard(10, 10, seed);
System.out.println(board);
System.out.println(board.getSeed());
boolean foundLetter = false;
boolean foundWord = false;
int i2 = 0;
int j2 = 0;
String tempWord = new String();
char[][] puzzle = board.getBoard(); //Create a 2D char array called puzzle that is equal to the 2D array generated by WordBoard
for(String word : board.getDictionary()) //For each word in WordBoard's dictionary
{
char letter = word.charAt(0);
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;
boolean upLeft = false;
boolean upRight = false;
boolean downLeft = false;
boolean downRight = false;
for(int i1 = 0; i1 < puzzle.length; i1++) //Loop through the puzzle rows
{
for(int j1 = 0; j1 < puzzle.length; j1++) //Loop through puzzle columns
{
if(puzzle[i1][j1] == letter) //When matched with a letter in the first index of a word in WordBoard's dictionary
{
i2 = i1; //Copy location
j2 = j1;
foundLetter = true; //We found a letter
foundWord = false;
if(i1 >= word.length()) //Possible to go up in the grid
{
up = true;
}
if((i1 + word.length()) <= puzzle.length) //Possible to go down in the grid
{
down = true;
}
if(j1 >= word.length()) //Possible to go left in the grid
{
left = true;
}
if((j1 + word.length()) <= puzzle.length) //Possible to go right in the grid
{
right = true;
}
if(up == true && left == true) //Possible to go up and left in the grid
{
upLeft = true;
}
if(up == true && right == true) //Possible to go up and right in the grid
{
upRight = true;
}
if(down == true && left == true) //Possible to go down and left in the grid
{
downLeft = true;
}
if(down == true && right == true) //Possible to go down and right in the grid
{
downRight = true;
}
if(up == true && foundLetter == true && foundWord == false) //While at a matched letter, go up
{
tempWord = "";
for(int index = 0; index < word.length(); index++) //Make a string of letters going up
{
tempWord += puzzle[i2][j2];
i2--;
}
if(word.equals(tempWord)) //If the dictionary word matches the temporary word going up
{
foundLetter = false;
foundWord = true; //We found a word
board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
}
}
i2 = i1; //Reset index for current letter position since we didn't find a word
j2 = j1;
if(down == true && foundLetter == true && foundWord == false) //While at a matched letter, go down
{
tempWord = "";
for(int index = 0; index < word.length(); index++) //Make a string of letters going down
{
tempWord += puzzle[i2][j2];
i2++;
}
if(word.equals(tempWord)) //If the dictionary word matches the temporary word going down
{
foundLetter = false;
foundWord = true; //We found a word
board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
}
}
i2 = i1; //Reset index for current letter position since we didn't find a word
j2 = j1;
if(left == true && foundLetter == true && foundWord == false) //While at a matched letter, go left
{
tempWord = "";
for(int index = 0; index < word.length(); index++) //Make a string of letters going left
{
tempWord += puzzle[i2][j2];
j2--;
}
if(word.equals(tempWord)) //If the dictionary word matches the temporary word going left
{
foundLetter = false;
foundWord = true; //We found a word
board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
}
}
i2 = i1; //Reset the index for current letter position since we didn't find a word
j2 = j1;
if(right == true && foundLetter == true && foundWord == false) //While at a matched letter, go right
{
tempWord = "";
for(int index = 0; index < word.length(); index++) //Make a string of letters going right
{
tempWord += puzzle[i2][j2];
j2++;
}
if(word.equals(tempWord)) //If the dictionary word matches the temporary word going right
{
foundLetter = false;
foundWord = true; //We found a word
board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
}
}
i2 = i1; //Reset index for current letter position since we didn't find a word
j2 = j1;
if(upLeft == true && foundLetter == true && foundWord == false) //While at a matched letter, go up and left
{
tempWord = "";
for(int index = 0; index < word.length(); index++) //Make a string of letters going up and left
{
tempWord += puzzle[i2][j2];
j2--;
i2--;
}
if(word.equals(tempWord)) //If the dictionary word matches the temporary word going up and left
{
foundLetter = false;
foundWord = true; //We found a word
board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
}
}
i2 = i1; //Reset the index for current letter position since we didn't find a word
j2 = j1;
if(upRight == true && foundLetter == true && foundWord == false) //While at a matched letter, go up and right
{
tempWord = "";
for(int index = 0; index < word.length(); index++) //Make a string of letters going up and right
{
tempWord += puzzle[i2][j2];
j2++;
i1--;
}
if(word.equals(tempWord)) //If the dictionary word matches the temporary word going up and right
{
foundLetter = false;
foundWord = true; //We found a word
board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
}
}
i2 = i1; //Reset index for current letter position since we didn't find a word
j2 = j1;
if(downLeft == true && foundLetter == true && foundWord == false) //While at a matched letter, go down and left
{
tempWord = "";
for(int index = 0; index < word.length(); index++) //Make a string of letters going down and left
{
tempWord += puzzle[i2][j2];
j2--;
i2++;
}
if(word.equals(tempWord)) //If the dictionary word matches the temporary word going down and left
{
foundLetter = false;
foundWord = true; //We found a word
board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
}
}
i2 = i1; //Reset index for current letter position since we didn't find a word
j2 = j1;
if(downRight == true && foundLetter == true && foundWord == false) //While at a matched letter, go down and right
{
tempWord = "";
for(int index = 0; index < word.length(); index++) //Make a string of letters going down and right
{
tempWord += puzzle[i2][j2];
j2++;
i2++;
}
if(word.equals(tempWord)) //If the dictionary word matches the temporary word going down and right
{
foundLetter = false;
foundWord = true; //We found a word
board.highlightWord(i1, j1, i2, j2); //Highlight the dictionary word on the grid
}
}
}
}
}
}
return board; //Return the WordBoard object with highlighted words
}
}