Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class wordsearcg {
- //this is the number of words you have under the word search
- public static int num_words = 5;
- //here we double it, because we're going to saerch for every word twice: once forwards, once backwards too
- public static int word_tot = num_words*2;
- //this is 15 spaces, we fill up the output array at the start
- public static String blank = " ";
- //This is an array of strings that will be output.
- //remember that right now, this array is EMPTY.
- public static String[] output_lines = new String[15];
- //These are the "lines" of letters in the word search
- //remember that right now, this array is EMPTY.
- public static String[] lines = new String[15];
- //These are the words you're gonna search for
- //remember that right now, this array is EMPTY.
- public static String[] words = new String[word_tot];
- public static void main(String[] args) throws IOException {
- //initialize output array with blank lines (15 spaces)
- for(int i = 0; i < 15; i++) {
- output_lines[i] = blank;
- }
- //find the input file
- File f = new File("ws.in");
- //set the output file
- File output = new File("out.txt");
- //this makes it so that you can use writer.write()
- //just like you would use System.out.print();
- PrintWriter writer = new PrintWriter(output);
- //get ready to read from the input file
- Scanner input = new Scanner(f);
- //read the lines of the crossword first
- for (int i = 0; i < lines.length; i++)
- lines[i] = input.nextLine();
- //read the words to search for next
- for (int i = 0; i < num_words; i++)
- words[i] = input.nextLine();
- //now, reverse each word
- for(int i = num_words; i < word_tot; i++)
- words[i] = new StringBuffer(words[i - num_words]).reverse().toString();
- //now, we loop
- //once for every word
- for (int w = 0; w < words.length; w++) {
- //once for every starting x position
- for (int x = 0; x < 15; x++) {
- //once for every starting y position
- for(int y = 0; y < 15; y++) {
- //and search each direction
- if(searchRight(w, 0, x, y, false)) {
- //if the search passes, go back through it again, but this time, copy each letter to the output array
- searchRight(w, 0, x, y, true);
- }
- if(searchRightDown(w, 0, x, y, false)) {
- searchRightDown(w, 0, x, y, true);
- }
- if(searchLeftDown(w, 0, x, y, false)) {
- searchLeftDown(w, 0, x, y, true);
- }
- if(searchDown(w, 0, x, y, false)) {
- searchDown(w, 0, x, y, true);
- }
- }
- }
- }
- //finally, print the output array
- //it has been modified each time search is run with "true" as a parameter
- //so now all you have to do is print it
- for(int i = 0; i < output_lines.length; i++) {
- writer.write(output_lines[i]+"\r\n");
- }
- //close it, or else the changes won't save
- writer.close();
- }
- /*
- *
- * Here is what each variable does:
- *
- * 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
- * offset: this is how many letters we have searched for so far in the word
- * it will also control where we look in the word search, according to the start position.
- * You always start with "0", and let the method control the variable.
- * startX, startY: The starting position in the word search. THESE DON'T CHANGE DURING A SEARCH.
- * place_letters:
- *
- * 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),
- * 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.
- *
- */
- public static boolean searchRight(int word_index, int offset, int startX, int startY, boolean place_letters) {
- //To search right, we just apply the offset to the x direction
- int curX = startX + offset;
- int curY = startY;
- /*
- * if you want to search down, you would only apply the offset to the Y.
- * You can see how its done in each method, and it should make sense.
- */
- // check bounds
- //used 15 because that is how large the word search is
- //and 0, because, well, obviously there are no negative indices in arrays
- //NOTE: These would be the "negative" base cases
- if (curX >= 15) {
- return false;
- }
- if (curY >= 15) {
- return false;
- }
- if (curX < 0) {
- return false;
- }
- if (curY < 0) {
- return false;
- }
- //find the two character we're going to compare
- //charAt(offset) return the character that is at position "offset."
- //If you pretend you're searching for the word "poop", and offset is 0, then its going to be "p".
- //If you're searching for the same word, and the offset is 1, its going to be "o".
- char word_char = words[word_index].charAt(offset);
- //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
- //is sort of like the "x" coordinate. See explanation above.
- //we look at "curX" and "curY", the values we calculated earlier.
- char search_char = lines[curY].charAt(curX);
- //this is where we copy over the letters we found.
- //since they are already guaranteed to match, it doesnt matter which one you pick.
- if(place_letters) {
- //this is actually the most elegant way to "replace" a certain character in a string.
- //some brief studying should make it clear what is happening here.
- char[] stringTochars = output_lines[curY].toCharArray();
- stringTochars[curX] = word_char;
- String charToString = new String(stringTochars);
- output_lines[curY] = charToString;
- }
- if (word_char == search_char) {
- // we have a match. If we're at the end of the word, return true,
- // otherwise, keep searching
- //NOTE: This would be the "positive" base case.
- if (offset == words[word_index].length() -1) {
- return true;
- } else {
- //here, we increment offset by one. This will update curX and curY accordingly, and continue the search util it either breaks
- //or finds a match.
- return searchRight(word_index, offset + 1, startX, startY, place_letters);
- }
- }
- //return false, just for good measure
- //it definitely should have kicked out of the method by this point, but just in case, this is agood failsafe.
- //since none of the previous conditions are "guaranteed," the compuler wont even accept this file if you leave this line out.
- return false;
- }
- public static boolean searchLeft(int word_index, int offset, int startX, int startY, boolean place_letters) {
- int curX = startX - offset;
- int curY = startY;
- // check bounds
- if (curX >= 15) {
- return false;
- }
- if (curY >= 15) {
- return false;
- }
- if (curX < 0) {
- return false;
- }
- if (curY < 0) {
- return false;
- }
- char word_char = words[word_index].charAt(offset);
- char search_char = lines[curY].charAt(curX);
- if(place_letters) {
- char[] stringTochars = output_lines[curY].toCharArray();
- stringTochars[curX] = word_char;
- String charToString = new String(stringTochars);
- output_lines[curY] = charToString;
- }
- if (word_char == search_char) {
- // we have a match. If we're at the end of the word, return true,
- // otherwise, keep searching
- if (offset == words[word_index].length() -1) {
- return true;
- } else {
- return searchLeft(word_index, offset + 1, startX, startY, place_letters);
- }
- }
- return false;
- }
- public static boolean searchDown(int word_index, int offset, int startX, int startY, boolean place_letters) {
- int curX = startX;
- int curY = startY+offset;
- // check bounds
- if (curX >= 15) {
- return false;
- }
- if (curY >= 15) {
- return false;
- }
- if (curX < 0) {
- return false;
- }
- if (curY < 0) {
- return false;
- }
- // if still here, current characters are in bounds.
- char word_char = words[word_index].charAt(offset);
- char search_char = lines[curY].charAt(curX);
- if(place_letters) {
- char[] stringTochars = output_lines[curY].toCharArray();
- stringTochars[curX] = word_char;
- String charToString = new String(stringTochars);
- output_lines[curY] = charToString;
- }
- if (word_char == search_char) {
- // we have a match. If we're at the end of the word, return true,
- // otherwise, keep searching
- if (offset == words[word_index].length() -1) {
- return true;
- } else {
- return searchDown(word_index, offset + 1, startX, startY, place_letters);
- }
- }
- return false;
- }
- public static boolean searchRightDown(int word_index, int offset, int startX, int startY, boolean place_letters) {
- int curX = startX+offset;
- int curY = startY+offset;
- // check bounds
- if (curX >= 15) {
- return false;
- }
- if (curY >= 15) {
- return false;
- }
- if (curX < 0) {
- return false;
- }
- if (curY < 0) {
- return false;
- }
- // if still here, current characters are in bounds.
- char word_char = words[word_index].charAt(offset);
- char search_char = lines[curY].charAt(curX);
- if(place_letters) {
- char[] stringTochars = output_lines[curY].toCharArray();
- stringTochars[curX] = word_char;
- String charToString = new String(stringTochars);
- output_lines[curY] = charToString;
- }
- if (word_char == search_char) {
- // we have a match. If we're at the end of the word, return true,
- // otherwise, keep searching
- if (offset == words[word_index].length() -1) {
- return true;
- } else {
- return searchRightDown(word_index, offset + 1, startX, startY, place_letters);
- }
- }
- return false;
- }
- public static boolean searchLeftDown(int word_index, int offset, int startX, int startY, boolean place_letters) {
- int curX = startX-offset;
- int curY = startY+offset;
- // check bounds
- if (curX >= 15) {
- return false;
- }
- if (curY >= 15) {
- return false;
- }
- if (curX < 0) {
- return false;
- }
- if (curY < 0) {
- return false;
- }
- // if still here, current characters are in bounds.
- char word_char = words[word_index].charAt(offset);
- char search_char = lines[curY].charAt(curX);
- if(place_letters) {
- char[] stringTochars = output_lines[curY].toCharArray();
- stringTochars[curX] = word_char;
- String charToString = new String(stringTochars);
- output_lines[curY] = charToString;
- }
- if (word_char == search_char) {
- // we have a match. If we're at the end of the word, return true,
- // otherwise, keep searching
- if (offset == words[word_index].length() -1) {
- return true;
- } else {
- return searchLeftDown(word_index, offset + 1, startX, startY, place_letters);
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment