Advertisement
jdalbey

WordSearchSolver.java (defective)

Mar 29th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.20 KB | None | 0 0
  1. /**
  2.  * Word Search Puzzle Solver. Finds the words in a word search puzzle, given the puzzle and a list of words.
  3.  *
  4.  * @author Some Student
  5.  * @date 09-27-07
  6.  * @project: Word Search Solver - Program #1
  7.  */
  8.  
  9. import java.util.*;
  10.  
  11. public class WordSearchSolver
  12. {
  13.     //size of puzzle board
  14.     int size;
  15.     //2-D array puzzleboard containing array of chars
  16.     char[][] puzzleboard;
  17.     //ArrayList of string words to search for
  18.     java.util.ArrayList<java.lang.String> words;
  19.    
  20.    
  21.      /**
  22.      * Constructor for objects of class WordSearchSolver
  23.      */
  24.     public WordSearchSolver(int size, char[][] puzzleboard, java.util.ArrayList<java.lang.String> words)
  25.     {
  26.         // initialise instance variables
  27.         this.size = size;
  28.         this.puzzleboard = puzzleboard;
  29.         this.words = words;
  30.     }
  31.  
  32.     /**
  33.      * Findwords Method that finds a list of words in a puzzleboard and returns a PuzzleWords list with the
  34.      * words found and location.
  35.      *
  36.      */
  37.     public java.util.ArrayList<PuzzleWord> findwords()
  38.     {
  39.         //indexes
  40.         int wordlistIndex;
  41.         int wordSize;
  42.         int xIndex;
  43.         int yIndex;
  44.         //location of x,y in the puzzle board
  45.         int kRow;
  46.         int kCol;
  47.         int xLoc;
  48.         int yLoc;
  49.         //boolean for found letter
  50.         boolean foundMatch;
  51.         //boolean for if word was found
  52.         boolean finWord;
  53.         //boolean setting for directions
  54.         boolean left;
  55.         boolean right;
  56.         boolean up;
  57.         boolean down;
  58.         boolean downright;
  59.         boolean downleft;
  60.         boolean upleft;
  61.         boolean upright;
  62.         //char letter of the first letter of the word in the word list
  63.         char curLetter;
  64.         //string for a current word and a tempword
  65.         String curWord = new String();
  66.         String tempWord = new String();
  67.         //blank array list PuzzleWords
  68.         ArrayList<PuzzleWord> answer = new ArrayList<PuzzleWord>();
  69.        
  70.         //go through the word list and use each word and find it in the puzzleboard, repeat till all words are found
  71.         for(wordlistIndex = 0; wordlistIndex < words.size(); wordlistIndex++)
  72.         {
  73.             //setting first word, and its size, and convert it to uppercase
  74.             curWord = words.get(wordlistIndex);
  75.             wordSize = curWord.length();
  76.             curWord = curWord.toUpperCase();
  77.            
  78.             //reset curletter, booleans for directions
  79.             curLetter = curWord.charAt(0);
  80.             up = false;
  81.             down = false;
  82.             left = false;
  83.             right = false;
  84.             upleft = false;
  85.             upright = false;
  86.             downleft = false;
  87.             downright = false;
  88.            
  89.                 //looping through the puzzleboard
  90.                 for(yIndex = 1; yIndex <= size; yIndex++)
  91.                 {
  92.                     for (xIndex = 1; xIndex <= size; xIndex++)
  93.                     {
  94.                         //when matched with a letter...
  95.                         if(puzzleboard[yIndex][xIndex] == curLetter)
  96.                         {
  97.                             //copy location
  98.                             xLoc = xIndex;
  99.                             yLoc = yIndex;
  100.                             kRow = xIndex;
  101.                             kCol = yIndex;
  102.                             //set found letter match as true, and word found as false
  103.                             foundMatch = true;
  104.                             finWord = false;
  105.                    
  106.                             //if yIndex is bigger than or equal to word size, then its possible to go up
  107.                             if(yIndex >= wordSize)
  108.                             {
  109.                                 up = true;
  110.                             }
  111.                             //if yIndex and the wordsize is less than or equal to the size, then tis possible to go down
  112.                             if((yIndex + wordSize) <= size)
  113.                             {
  114.                                 down = true;
  115.                             }
  116.                             //if the xIndex is bigger than or equal to the word size, its possible to fit the word left
  117.                             if(xIndex >= wordSize)
  118.                             {
  119.                                 left = true;
  120.                             }
  121.                             //if xIndex + the wordsize are less than or equal to the size, its possible to go right
  122.                             if((xIndex + wordSize) <= size)
  123.                             {
  124.                                 right = true;
  125.                             }
  126.                             //if up and left are possible, upleft are possible
  127.                             if(up && left)
  128.                             {
  129.                                 upleft = true;
  130.                             }
  131.                             //if up and right are possible, upright is possible
  132.                             if(up && right)
  133.                             {
  134.                                 upright = true;
  135.                             }
  136.                             //if down and left are possible, downleft is possible
  137.                             if(down && left)
  138.                             {
  139.                                 downleft = true;
  140.                             }
  141.                             //if down and right are possible, down right is possible
  142.                             if(down && right)
  143.                             {
  144.                                 downright = true;
  145.                             }
  146.                    
  147.                             //while currently at a matched letter from the grid go up
  148.                             if(up && !finWord)
  149.                             {
  150.                                 //make a string of the letters going up with the wordsize
  151.                                 tempWord = "";
  152.                                 for(int index = 0; index < wordSize; index++)
  153.                                 {
  154.                                     tempWord+=puzzleboard[yLoc][xLoc];
  155.                                     yLoc--;
  156.                                 }
  157.                        
  158.                                 //if current word matches the word going up, place in puzzle word including details
  159.                                 if(curWord.equals(tempWord))
  160.                                 {
  161.                                     Natural row = new Natural(kRow);
  162.                                     Natural col = new Natural(kCol);
  163.                                     Natural wordLength = new Natural(wordSize);
  164.  
  165.                                     PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.up);
  166.                                     answer.add(puzWord);
  167.                                     //set letter match as false, and foundword as true (to skip remaining directions if word is found)
  168.                                     foundMatch = false;
  169.                                     finWord = true;
  170.                                 }
  171.                             }
  172.                            
  173.                             //reset index for current letter position because word not found
  174.                             xLoc = xIndex;
  175.                             yLoc = yIndex;
  176.                             //while currently at a matched letter from the grid go down
  177.                             if(down && !finWord)
  178.                             {
  179.                                 //make a string of the letters going up with the wordsize
  180.                                 tempWord = "";
  181.                                 for(int index = 0; index < wordSize; index++)
  182.                                 {
  183.                                     tempWord+=puzzleboard[yLoc][xLoc];
  184.                                         yLoc++;
  185.                                 }
  186.                        
  187.                                 //if current word matches the word going up, place in puzzle word including details
  188.                                 if(curWord.equals(tempWord))
  189.                                 {
  190.                                     Natural row = new Natural(kRow);
  191.                                     Natural col = new Natural(kCol);
  192.                                     Natural wordLength = new Natural(wordSize);
  193.  
  194.                                     PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.down);
  195.                                     answer.add(puzWord);
  196.                                     //set letter match as false, and foundword as true (to skip remaining directions if word is found)
  197.                                     foundMatch = false;
  198.                                     finWord = true;
  199.                                 }
  200.                             }
  201.                    
  202.                             //reset index for current letter position
  203.                             xLoc = xIndex;
  204.                             yLoc = yIndex;
  205.                             //while currently at a matched letter from the grid go left
  206.                             if(left && !finWord)
  207.                             {
  208.                                 //make a string of the letters going up with the wordsize
  209.                                 tempWord = "";
  210.                                 for(int index = 0; index < wordSize; index++)
  211.                                 {
  212.                                     tempWord+=puzzleboard[yLoc][xLoc];
  213.                                     xLoc--;
  214.                                 }
  215.                        
  216.                                 //if current word matches the word going up, place in puzzle word including details
  217.                                 if(curWord.equals(tempWord))
  218.                                 {
  219.                                     Natural row = new Natural(kRow);
  220.                                     Natural col = new Natural(kCol);
  221.                                     Natural wordLength = new Natural(wordSize);
  222.  
  223.                                     PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.up);
  224.                                     answer.add(puzWord);
  225.                                     //set letter match as false, and foundword as true (to skip remaining directions if word is found)
  226.                                     foundMatch = false;
  227.                                     finWord = true;
  228.                                 }
  229.                             }
  230.                    
  231.                             //reset index for current letter position
  232.                             xLoc = xIndex;
  233.                             yLoc = yIndex;
  234.                             //while currently at a matched letter from the grid go right
  235.                             if(right && !finWord)
  236.                             {
  237.                                 //make a string of the letters going up with the wordsize
  238.                                 tempWord = "";
  239.                                 for(int index = 0; index < wordSize; index++)
  240.                                 {
  241.                                     tempWord+=puzzleboard[yLoc][xLoc];
  242.                                     xLoc++;
  243.                                 }
  244.                        
  245.                                 //if current word matches the word going up, place in puzzle word including details
  246.                                 if(curWord.equals(tempWord))
  247.                                 {
  248.                                     Natural row = new Natural(kRow);
  249.                                     Natural col = new Natural(kCol);
  250.                                     Natural wordLength = new Natural(wordSize);
  251.  
  252.                                     PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.right);
  253.                                     answer.add(puzWord);
  254.                                     //set letter match as false, and foundword as true (to skip remaining directions if word is found)
  255.                                     foundMatch = false;
  256.                                     finWord = true;
  257.                                 }
  258.                             }
  259.                            
  260.                             //reset index for current letter position
  261.                             xLoc = xIndex;
  262.                             yLoc = yIndex;
  263.                             //while currently at a matched letter from the grid go upleft
  264.                             if(upleft && !finWord)
  265.                             {
  266.                                 //make a string of the letters going up with the wordsize
  267.                                 tempWord = "";
  268.                                 for(int index = 0; index < wordSize; index++)
  269.                                 {
  270.                                     tempWord+=puzzleboard[yLoc][xLoc];
  271.                                     xLoc--;
  272.                                     yLoc--;
  273.                                 }
  274.                        
  275.                                 //if current word matches the word going up, place in puzzle word including details
  276.                                 if(curWord.equals(tempWord))
  277.                                 {
  278.                                     Natural row = new Natural(kRow);
  279.                                     Natural col = new Natural(kCol);
  280.                                     Natural wordLength = new Natural(wordSize);
  281.  
  282.                                     PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.upleft);
  283.                                     answer.add(puzWord);
  284.                                     //set letter match as false, and foundword as true (to skip remaining directions if word is found)
  285.                                     foundMatch = false;
  286.                                     finWord = true;
  287.                                 }
  288.                             }
  289.                    
  290.                             //reset index for current letter position
  291.                             xLoc = xIndex;
  292.                             yLoc = yIndex;
  293.                             //while currently at a matched letter from the grid go upright
  294.                             if(upright && !finWord)
  295.                             {
  296.                                 //make a string of the letters going up with the wordsize
  297.                                 tempWord = "";
  298.                                 for(int index = 0; index < wordSize; index++)
  299.                                 {
  300.                                     tempWord+=puzzleboard[yLoc][xLoc];
  301.                                     xLoc++;
  302.                                     yLoc--;
  303.                                 }
  304.                        
  305.                                 //if current word matches the word going up, place in puzzle word including details
  306.                                 if(curWord.equals(tempWord))
  307.                                 {
  308.                                     Natural row = new Natural(kRow);
  309.                                     Natural col = new Natural(kCol);
  310.                                     Natural wordLength = new Natural(wordSize);
  311.                                    
  312.                                     PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.upright);
  313.                                     answer.add(puzWord);
  314.                                     //set letter match as false, and foundword as true (to skip remaining directions if word is found)
  315.                                     foundMatch = false;
  316.                                     finWord = true;
  317.                                 }
  318.                             }
  319.                    
  320.                             //reset index for current letter position
  321.                             xLoc = xIndex;
  322.                             yLoc = yIndex;
  323.                             //while currently at a matched letter from the grid go downleft
  324.                             if(downleft && !finWord)
  325.                             {
  326.                                 //make a string of the letters going up with the wordsize
  327.                                 tempWord = "";
  328.                                 for(int index = 0; index < wordSize; index++)
  329.                                 {
  330.                                     tempWord+=puzzleboard[yLoc][xLoc];
  331.                                     xLoc--;
  332.                                     yLoc++;
  333.                                 }  
  334.                        
  335.                                 //if current word matches the word going up, place in puzzle word including details
  336.                                 if(curWord.equals(tempWord))
  337.                                 {
  338.                                     Natural row = new Natural(kRow);
  339.                                     Natural col = new Natural(kCol);
  340.                                     Natural wordLength = new Natural(wordSize);
  341.  
  342.                                     PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.downleft);
  343.                                     answer.add(puzWord);
  344.                                     //set letter match as false, and foundword as true (to skip remaining directions if word is found)
  345.                                     foundMatch = false;
  346.                                     finWord = true;
  347.                                 }
  348.                             }
  349.                    
  350.                             //reset index for current letter position
  351.                             xLoc = xIndex;
  352.                             yLoc = yIndex;
  353.                             //while currently at a matched letter from the grid go downright
  354.                             if(downright && !finWord)
  355.                             {
  356.                                 //make a string of the letters going up with the wordsize
  357.                                 tempWord = "";
  358.                                 for(int index = 0; index < wordSize; index++)
  359.                                 {
  360.                                     tempWord+=puzzleboard[yLoc][xLoc];
  361.                                     xLoc++;
  362.                                     yLoc++;
  363.                                 }
  364.                        
  365.                                 //if current word matches the word going up, place in puzzle word including details
  366.                                 if(curWord.equals(tempWord))
  367.                                 {
  368.                                     Natural row = new Natural(kRow);
  369.                                     Natural col = new Natural(kCol);
  370.                                     Natural wordLength = new Natural(wordSize);
  371.  
  372.                                     PuzzleWord puzWord = new PuzzleWord(curWord,col,row,wordLength,PuzzleWord.Directions.downright);
  373.                                     answer.add(puzWord);
  374.                                     //set letter match as false, and foundword as true (to skip remaining directions if word is found)
  375.                                     foundMatch = false;
  376.                                     finWord = true;
  377.                                 }
  378.                             }  
  379.                         }
  380.                     }
  381.                 }
  382.             }
  383.            
  384.             return answer;
  385.     }
  386.     /**
  387.      * Local driver for unit testing
  388.      * @param args not used
  389.      */
  390.     public static void main(String[] args)
  391.     {
  392.         // A sample puzzle board
  393.         char [][] board = { {' ', ' ',' ',' '},
  394.                             {' ', 'C','A','T'},
  395.                             {' ', 'X','O','Z'},
  396.                             {' ', 'E','K','L'}};
  397.         // words to find in the puzzle
  398.         ArrayList<String> wordlist = new ArrayList<String>();
  399.         wordlist.add("CAT");
  400.         wordlist.add("TOE");
  401.         // Create instance of solver
  402.         WordSearchSolver ws = new WordSearchSolver(3, board, wordlist);
  403.         // Find matching words
  404.         ArrayList<PuzzleWord> results = ws.findwords();
  405.         // Show results
  406.         System.out.println("Found "+ results.size() + " words.");
  407.         for (PuzzleWord pw: results)
  408.         {
  409.             System.out.println("  " + pw.word + " with direction " + pw.direction);
  410.         }
  411.     }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement