Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Word Search Puzzle Solver. Finds the words in a word search puzzle, given the puzzle and a list of words.
- *
- * @author Some Student
- * @date 09-27-07
- * @project: Word Search Solver - Program #1
- */
- import java.util.*;
- public class WordSearchSolver
- {
- //size of puzzle board
- int size;
- //2-D array puzzleboard containing array of chars
- char[][] puzzleboard;
- //ArrayList of string words to search for
- java.util.ArrayList<java.lang.String> words;
- /**
- * Constructor for objects of class WordSearchSolver
- */
- public WordSearchSolver(int size, char[][] puzzleboard, java.util.ArrayList<java.lang.String> words)
- {
- // initialise instance variables
- this.size = size;
- this.puzzleboard = puzzleboard;
- this.words = words;
- }
- /**
- * Findwords Method that finds a list of words in a puzzleboard and returns a PuzzleWords list with the
- * words found and location.
- *
- */
- public java.util.ArrayList<PuzzleWord> findwords()
- {
- //indexes
- int wordlistIndex;
- int wordSize;
- int xIndex;
- int yIndex;
- //location of x,y in the puzzle board
- int kRow;
- int kCol;
- int xLoc;
- int yLoc;
- //boolean for found letter
- boolean foundMatch;
- //boolean for if word was found
- boolean finWord;
- //boolean setting for directions
- boolean left;
- boolean right;
- boolean up;
- boolean down;
- boolean downright;
- boolean downleft;
- boolean upleft;
- boolean upright;
- //char letter of the first letter of the word in the word list
- char curLetter;
- //string for a current word and a tempword
- String curWord = new String();
- String tempWord = new String();
- //blank array list PuzzleWords
- ArrayList<PuzzleWord> answer = new ArrayList<PuzzleWord>();
- //go through the word list and use each word and find it in the puzzleboard, repeat till all words are found
- for(wordlistIndex = 0; wordlistIndex < words.size(); wordlistIndex++)
- {
- //setting first word, and its size, and convert it to uppercase
- curWord = words.get(wordlistIndex);
- wordSize = curWord.length();
- curWord = curWord.toUpperCase();
- //reset curletter, booleans for directions
- curLetter = curWord.charAt(0);
- up = false;
- down = false;
- left = false;
- right = false;
- upleft = false;
- upright = false;
- downleft = false;
- downright = false;
- //looping through the puzzleboard
- for(yIndex = 1; yIndex <= size; yIndex++)
- {
- for (xIndex = 1; xIndex <= size; xIndex++)
- {
- //when matched with a letter...
- if(puzzleboard[yIndex][xIndex] == curLetter)
- {
- //copy location
- xLoc = xIndex;
- yLoc = yIndex;
- kRow = xIndex;
- kCol = yIndex;
- //set found letter match as true, and word found as false
- foundMatch = true;
- finWord = false;
- //if yIndex is bigger than or equal to word size, then its possible to go up
- if(yIndex >= wordSize)
- {
- up = true;
- }
- //if yIndex and the wordsize is less than or equal to the size, then tis possible to go down
- if((yIndex + wordSize) <= size)
- {
- down = true;
- }
- //if the xIndex is bigger than or equal to the word size, its possible to fit the word left
- if(xIndex >= wordSize)
- {
- left = true;
- }
- //if xIndex + the wordsize are less than or equal to the size, its possible to go right
- if((xIndex + wordSize) <= size)
- {
- right = true;
- }
- //if up and left are possible, upleft are possible
- if(up && left)
- {
- upleft = true;
- }
- //if up and right are possible, upright is possible
- if(up && right)
- {
- upright = true;
- }
- //if down and left are possible, downleft is possible
- if(down && left)
- {
- downleft = true;
- }
- //if down and right are possible, down right is possible
- if(down && right)
- {
- downright = true;
- }
- //while currently at a matched letter from the grid go up
- if(up && !finWord)
- {
- //make a string of the letters going up with the wordsize
- tempWord = "";
- for(int index = 0; index < wordSize; index++)
- {
- tempWord+=puzzleboard[yLoc][xLoc];
- yLoc--;
- }
- //if current word matches the word going up, place in puzzle word including details
- if(curWord.equals(tempWord))
- {
- Natural row = new Natural(kRow);
- Natural col = new Natural(kCol);
- Natural wordLength = new Natural(wordSize);
- PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.up);
- answer.add(puzWord);
- //set letter match as false, and foundword as true (to skip remaining directions if word is found)
- foundMatch = false;
- finWord = true;
- }
- }
- //reset index for current letter position because word not found
- xLoc = xIndex;
- yLoc = yIndex;
- //while currently at a matched letter from the grid go down
- if(down && !finWord)
- {
- //make a string of the letters going up with the wordsize
- tempWord = "";
- for(int index = 0; index < wordSize; index++)
- {
- tempWord+=puzzleboard[yLoc][xLoc];
- yLoc++;
- }
- //if current word matches the word going up, place in puzzle word including details
- if(curWord.equals(tempWord))
- {
- Natural row = new Natural(kRow);
- Natural col = new Natural(kCol);
- Natural wordLength = new Natural(wordSize);
- PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.down);
- answer.add(puzWord);
- //set letter match as false, and foundword as true (to skip remaining directions if word is found)
- foundMatch = false;
- finWord = true;
- }
- }
- //reset index for current letter position
- xLoc = xIndex;
- yLoc = yIndex;
- //while currently at a matched letter from the grid go left
- if(left && !finWord)
- {
- //make a string of the letters going up with the wordsize
- tempWord = "";
- for(int index = 0; index < wordSize; index++)
- {
- tempWord+=puzzleboard[yLoc][xLoc];
- xLoc--;
- }
- //if current word matches the word going up, place in puzzle word including details
- if(curWord.equals(tempWord))
- {
- Natural row = new Natural(kRow);
- Natural col = new Natural(kCol);
- Natural wordLength = new Natural(wordSize);
- PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.up);
- answer.add(puzWord);
- //set letter match as false, and foundword as true (to skip remaining directions if word is found)
- foundMatch = false;
- finWord = true;
- }
- }
- //reset index for current letter position
- xLoc = xIndex;
- yLoc = yIndex;
- //while currently at a matched letter from the grid go right
- if(right && !finWord)
- {
- //make a string of the letters going up with the wordsize
- tempWord = "";
- for(int index = 0; index < wordSize; index++)
- {
- tempWord+=puzzleboard[yLoc][xLoc];
- xLoc++;
- }
- //if current word matches the word going up, place in puzzle word including details
- if(curWord.equals(tempWord))
- {
- Natural row = new Natural(kRow);
- Natural col = new Natural(kCol);
- Natural wordLength = new Natural(wordSize);
- PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.right);
- answer.add(puzWord);
- //set letter match as false, and foundword as true (to skip remaining directions if word is found)
- foundMatch = false;
- finWord = true;
- }
- }
- //reset index for current letter position
- xLoc = xIndex;
- yLoc = yIndex;
- //while currently at a matched letter from the grid go upleft
- if(upleft && !finWord)
- {
- //make a string of the letters going up with the wordsize
- tempWord = "";
- for(int index = 0; index < wordSize; index++)
- {
- tempWord+=puzzleboard[yLoc][xLoc];
- xLoc--;
- yLoc--;
- }
- //if current word matches the word going up, place in puzzle word including details
- if(curWord.equals(tempWord))
- {
- Natural row = new Natural(kRow);
- Natural col = new Natural(kCol);
- Natural wordLength = new Natural(wordSize);
- PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.upleft);
- answer.add(puzWord);
- //set letter match as false, and foundword as true (to skip remaining directions if word is found)
- foundMatch = false;
- finWord = true;
- }
- }
- //reset index for current letter position
- xLoc = xIndex;
- yLoc = yIndex;
- //while currently at a matched letter from the grid go upright
- if(upright && !finWord)
- {
- //make a string of the letters going up with the wordsize
- tempWord = "";
- for(int index = 0; index < wordSize; index++)
- {
- tempWord+=puzzleboard[yLoc][xLoc];
- xLoc++;
- yLoc--;
- }
- //if current word matches the word going up, place in puzzle word including details
- if(curWord.equals(tempWord))
- {
- Natural row = new Natural(kRow);
- Natural col = new Natural(kCol);
- Natural wordLength = new Natural(wordSize);
- PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.upright);
- answer.add(puzWord);
- //set letter match as false, and foundword as true (to skip remaining directions if word is found)
- foundMatch = false;
- finWord = true;
- }
- }
- //reset index for current letter position
- xLoc = xIndex;
- yLoc = yIndex;
- //while currently at a matched letter from the grid go downleft
- if(downleft && !finWord)
- {
- //make a string of the letters going up with the wordsize
- tempWord = "";
- for(int index = 0; index < wordSize; index++)
- {
- tempWord+=puzzleboard[yLoc][xLoc];
- xLoc--;
- yLoc++;
- }
- //if current word matches the word going up, place in puzzle word including details
- if(curWord.equals(tempWord))
- {
- Natural row = new Natural(kRow);
- Natural col = new Natural(kCol);
- Natural wordLength = new Natural(wordSize);
- PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.downleft);
- answer.add(puzWord);
- //set letter match as false, and foundword as true (to skip remaining directions if word is found)
- foundMatch = false;
- finWord = true;
- }
- }
- //reset index for current letter position
- xLoc = xIndex;
- yLoc = yIndex;
- //while currently at a matched letter from the grid go downright
- if(downright && !finWord)
- {
- //make a string of the letters going up with the wordsize
- tempWord = "";
- for(int index = 0; index < wordSize; index++)
- {
- tempWord+=puzzleboard[yLoc][xLoc];
- xLoc++;
- yLoc++;
- }
- //if current word matches the word going up, place in puzzle word including details
- if(curWord.equals(tempWord))
- {
- Natural row = new Natural(kRow);
- Natural col = new Natural(kCol);
- Natural wordLength = new Natural(wordSize);
- PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.downright);
- answer.add(puzWord);
- //set letter match as false, and foundword as true (to skip remaining directions if word is found)
- foundMatch = false;
- finWord = true;
- }
- }
- }
- }
- }
- }
- return answer;
- }
- /**
- * Local driver for unit testing
- * @param args not used
- */
- public static void main(String[] args)
- {
- // A sample puzzle board
- char [][] board = { {' ', ' ',' ',' '},
- {' ', 'C','A','T'},
- {' ', 'X','O','Z'},
- {' ', 'E','K','L'}};
- // words to find in the puzzle
- ArrayList<String> wordlist = new ArrayList<String>();
- wordlist.add("CAT");
- wordlist.add("TOE");
- // Create instance of solver
- WordSearchSolver ws = new WordSearchSolver(3, board, wordlist);
- // Find matching words
- ArrayList<PuzzleWord> results = ws.findwords();
- // Show results
- System.out.println("Found "+ results.size() + " words.");
- for (PuzzleWord pw: results)
- {
- System.out.println(" " + pw.word + " with direction " + pw.direction);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement