Guest User

Untitled

a guest
Jun 18th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.80 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.Scanner;
  6.  
  7. public class wordsearcg {
  8.    
  9.     //this is the number of words you have under the word search
  10.     public static int num_words = 5;
  11.     //here we double it, because we're going to saerch for every word twice: once forwards, once backwards too
  12.     public static int word_tot = num_words*2;
  13.    
  14.     //this is 15 spaces, we fill up the output array at the start
  15.     public static String blank = "               ";
  16.    
  17.     //This is an array of strings that will be output.
  18.     //remember that right now, this array is EMPTY.
  19.     public static String[] output_lines = new String[15];
  20.    
  21.     //These are the "lines" of letters in the word search
  22.     //remember that right now, this array is EMPTY.
  23.     public static String[] lines = new String[15];
  24.  
  25.     //These are the words you're gonna search for
  26.     //remember that right now, this array is EMPTY.
  27.     public static String[] words = new String[word_tot];
  28.  
  29.     public static void main(String[] args) throws IOException {
  30.        
  31.         //initialize output array with blank lines (15 spaces)
  32.         for(int i = 0; i < 15; i++) {
  33.             output_lines[i] = blank;
  34.         }
  35.        
  36.         //find the input file
  37.         File f = new File("ws.in");
  38.         //set the output file
  39.         File output = new File("out.txt");
  40.         //this makes it so that you can use writer.write()
  41.         //just like you would use System.out.print();
  42.         PrintWriter writer = new PrintWriter(output);
  43.        
  44.         //get ready to read from the input file
  45.         Scanner input = new Scanner(f);
  46.        
  47.         //read the lines of the crossword first
  48.         for (int i = 0; i < lines.length; i++)
  49.             lines[i] = input.nextLine();
  50.        
  51.         //read the words to search for next
  52.         for (int i = 0; i < num_words; i++)
  53.             words[i] = input.nextLine();
  54.        
  55.         //now, reverse each word
  56.         for(int i = num_words; i < word_tot; i++)
  57.             words[i] = new StringBuffer(words[i - num_words]).reverse().toString();
  58.        
  59.         //now, we loop
  60.         //once for every word
  61.         for (int w = 0; w < words.length; w++) {
  62.             //once for every starting x position
  63.             for (int x = 0; x < 15; x++) {
  64.                 //once for every starting y position
  65.                 for(int y = 0; y < 15; y++) {
  66.                     //and search each direction
  67.                     if(searchRight(w, 0, x, y, false)) {
  68.                         //if the search passes, go back through it again, but this time, copy each letter to the output array
  69.                         searchRight(w, 0, x, y, true);
  70.                     }
  71.                     if(searchRightDown(w, 0, x, y, false)) {
  72.                         searchRightDown(w, 0, x, y, true);
  73.                     }
  74.                     if(searchLeftDown(w, 0, x, y, false)) {
  75.                         searchLeftDown(w, 0, x, y, true);
  76.                     }
  77.                     if(searchDown(w, 0, x, y, false)) {
  78.                         searchDown(w, 0, x, y, true);
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.        
  84.         //finally, print the output array
  85.         //it has been modified each time search is run with "true" as a parameter
  86.         //so now all you have to do is print it
  87.         for(int i = 0; i < output_lines.length; i++) {
  88.             writer.write(output_lines[i]+"\r\n");
  89.         }
  90.         //close it, or else the changes won't save
  91.         writer.close();
  92.     }
  93.  
  94.     /*
  95.      *
  96.      * Here is what each variable does:
  97.      *
  98.      * word_index: this tells the method which word it should be looking for. It pulls the word out of the array of words whenever it needs it
  99.      * offset: this is how many letters we have searched for so far in the word
  100.      *          it will also control where we look in the word search, according to the start position.
  101.      *          You always start with "0", and let the method control the variable.
  102.      * startX, startY: The starting position in the word search. THESE DON'T CHANGE DURING A SEARCH.
  103.      * place_letters:
  104.      *
  105.      * This one takes a little explaining to do. First time through, you would set this as false. Then, if a search returns positive (you found a whole word),
  106.      * you would want to go through the search again, but set this to false. This tells the method to copy each letter over to the output array.
  107.      *
  108.      */
  109.     public static boolean searchRight(int word_index, int offset, int startX, int startY, boolean place_letters) {
  110.        
  111.         //To search right, we just apply the offset to the x direction
  112.         int curX = startX + offset;
  113.         int curY = startY;
  114.        
  115.         /*
  116.          * if you want to search down, you would only apply the offset to the Y.
  117.          * You can see how its done in each method, and it should make sense.
  118.          */
  119.  
  120.         // check bounds
  121.         //used 15 because that is how large the word search is
  122.         //and 0, because, well, obviously there are no negative indices in arrays
  123.         //NOTE: These would be the "negative" base cases
  124.         if (curX >= 15) {
  125.             return false;
  126.         }
  127.         if (curY >= 15) {
  128.             return false;
  129.         }
  130.         if (curX < 0) {
  131.             return false;
  132.         }
  133.         if (curY < 0) {
  134.             return false;
  135.         }
  136.  
  137.         //find the two character we're going to compare
  138.        
  139.         //charAt(offset) return the character that is at position "offset."
  140.         //If you pretend you're searching for the word "poop", and offset is 0, then its going to be "p".
  141.         //If you're searching for the same word, and the offset is 1, its going to be "o".
  142.         char word_char = words[word_index].charAt(offset);
  143.        
  144.         //this is the character on the grid of the word search. Since each "line" can be considered as a "y" coordinate, then the "charAt()" method
  145.         //is sort of like the "x" coordinate. See explanation above.
  146.         //we look at "curX" and "curY", the values we calculated earlier.
  147.         char search_char = lines[curY].charAt(curX);
  148.        
  149.         //this is where we copy over the letters we found.
  150.         //since they are already guaranteed to match, it doesnt matter which one you pick.
  151.         if(place_letters) {
  152.             //this is actually the most elegant way to "replace" a certain character in a string.
  153.             //some brief studying should make it clear what is happening here.
  154.             char[] stringTochars = output_lines[curY].toCharArray();
  155.             stringTochars[curX] = word_char;
  156.             String charToString = new String(stringTochars);
  157.             output_lines[curY] = charToString;
  158.         }
  159.  
  160.         if (word_char == search_char) {
  161.             // we have a match. If we're at the end of the word, return true,
  162.             // otherwise, keep searching
  163.            
  164.             //NOTE: This would be the "positive" base case.
  165.             if (offset == words[word_index].length() -1) {
  166.                 return true;
  167.             } else {
  168.                 //here, we increment offset by one. This will update curX and curY accordingly, and continue the search util it either breaks
  169.                 //or finds a match.
  170.                 return searchRight(word_index, offset + 1, startX, startY, place_letters);
  171.             }
  172.         }
  173.        
  174.         //return false, just for good measure
  175.         //it definitely should have kicked out of the method by this point, but just in case, this is  agood failsafe.
  176.         //since none of the previous conditions are "guaranteed," the compuler wont even accept this file if you leave this line out.
  177.         return false;
  178.     }
  179.    
  180.     public static boolean searchLeft(int word_index, int offset, int startX, int startY, boolean place_letters) {
  181.         int curX = startX - offset;
  182.         int curY = startY;
  183.  
  184.         // check bounds
  185.         if (curX >= 15) {
  186.             return false;
  187.         }
  188.         if (curY >= 15) {
  189.             return false;
  190.         }
  191.         if (curX < 0) {
  192.             return false;
  193.         }
  194.         if (curY < 0) {
  195.             return false;
  196.         }
  197.  
  198.         char word_char = words[word_index].charAt(offset);
  199.         char search_char = lines[curY].charAt(curX);
  200.        
  201.         if(place_letters) {
  202.             char[] stringTochars = output_lines[curY].toCharArray();
  203.             stringTochars[curX] = word_char;
  204.             String charToString = new String(stringTochars);
  205.             output_lines[curY] = charToString;
  206.         }
  207.  
  208.         if (word_char == search_char) {
  209.             // we have a match. If we're at the end of the word, return true,
  210.             // otherwise, keep searching
  211.             if (offset == words[word_index].length() -1) {
  212.                 return true;
  213.             } else {
  214.                 return searchLeft(word_index, offset + 1, startX, startY, place_letters);
  215.             }
  216.         }
  217.         return false;
  218.     }
  219.    
  220.     public static boolean searchDown(int word_index, int offset, int startX, int startY, boolean place_letters) {
  221.         int curX = startX;
  222.         int curY = startY+offset;
  223.  
  224.         // check bounds
  225.         if (curX >= 15) {
  226.             return false;
  227.         }
  228.         if (curY >= 15) {
  229.             return false;
  230.         }
  231.         if (curX < 0) {
  232.             return false;
  233.         }
  234.         if (curY < 0) {
  235.             return false;
  236.         }
  237.         // if still here, current characters are in bounds.
  238.  
  239.         char word_char = words[word_index].charAt(offset);
  240.         char search_char = lines[curY].charAt(curX);
  241.        
  242.         if(place_letters) {
  243.             char[] stringTochars = output_lines[curY].toCharArray();
  244.             stringTochars[curX] = word_char;
  245.             String charToString = new String(stringTochars);
  246.             output_lines[curY] = charToString;
  247.         }
  248.  
  249.         if (word_char == search_char) {
  250.             // we have a match. If we're at the end of the word, return true,
  251.             // otherwise, keep searching
  252.             if (offset == words[word_index].length() -1) {
  253.                 return true;
  254.             } else {
  255.                 return searchDown(word_index, offset + 1, startX, startY, place_letters);
  256.             }
  257.         }
  258.         return false;
  259.     }
  260.    
  261.     public static boolean searchRightDown(int word_index, int offset, int startX, int startY, boolean place_letters) {
  262.         int curX = startX+offset;
  263.         int curY = startY+offset;
  264.  
  265.         // check bounds
  266.         if (curX >= 15) {
  267.             return false;
  268.         }
  269.         if (curY >= 15) {
  270.             return false;
  271.         }
  272.         if (curX < 0) {
  273.             return false;
  274.         }
  275.         if (curY < 0) {
  276.             return false;
  277.         }
  278.         // if still here, current characters are in bounds.
  279.  
  280.         char word_char = words[word_index].charAt(offset);
  281.         char search_char = lines[curY].charAt(curX);
  282.        
  283.         if(place_letters) {
  284.             char[] stringTochars = output_lines[curY].toCharArray();
  285.             stringTochars[curX] = word_char;
  286.             String charToString = new String(stringTochars);
  287.             output_lines[curY] = charToString;
  288.         }
  289.  
  290.         if (word_char == search_char) {
  291.             // we have a match. If we're at the end of the word, return true,
  292.             // otherwise, keep searching
  293.             if (offset == words[word_index].length() -1) {
  294.                 return true;
  295.             } else {
  296.                 return searchRightDown(word_index, offset + 1, startX, startY, place_letters);
  297.             }
  298.         }
  299.         return false;
  300.     }
  301.    
  302.     public static boolean searchLeftDown(int word_index, int offset, int startX, int startY, boolean place_letters) {
  303.         int curX = startX-offset;
  304.         int curY = startY+offset;
  305.  
  306.         // check bounds
  307.         if (curX >= 15) {
  308.             return false;
  309.         }
  310.         if (curY >= 15) {
  311.             return false;
  312.         }
  313.         if (curX < 0) {
  314.             return false;
  315.         }
  316.         if (curY < 0) {
  317.             return false;
  318.         }
  319.         // if still here, current characters are in bounds.
  320.  
  321.         char word_char = words[word_index].charAt(offset);
  322.         char search_char = lines[curY].charAt(curX);
  323.        
  324.         if(place_letters) {
  325.             char[] stringTochars = output_lines[curY].toCharArray();
  326.             stringTochars[curX] = word_char;
  327.             String charToString = new String(stringTochars);
  328.             output_lines[curY] = charToString;
  329.         }
  330.  
  331.         if (word_char == search_char) {
  332.             // we have a match. If we're at the end of the word, return true,
  333.             // otherwise, keep searching
  334.             if (offset == words[word_index].length() -1) {
  335.                 return true;
  336.             } else {
  337.                 return searchLeftDown(word_index, offset + 1, startX, startY, place_letters);
  338.             }
  339.         }
  340.         return false;
  341.     }
  342. }
Add Comment
Please, Sign In to add comment