2016-11-15 Wordsearch update

posted Nov 15, 2016, 6:45 AM by Samuel Konstantinovich   [ updated Nov 15, 2016, 1:00 PM ]
WordSearch updates

1. Make a plain text file "words.txt" and fill it with whatever words you like. 
   -Put in words with different lengths to help you test
Your text file should be formatted like this:
ham
beef
sushi
turkey
venison
meatloaf

Update to your WordSearch with two new instance variables:

//all words from a text file get added to wordsToAdd , indicating that they have not yet been added
private ArrayList<String> wordsToAdd;

//all words that were successfully added get moved into wordsAdded.
private ArrayList<String> wordsAdded;

//a random Object to unify your random calls
private Random randgen;


PART II Read your file!

2. Open the file in a method called loadWords(String fileName)
- Use a scanner on a file object to be able to read in the words, adding each new word to wordsToAdd

2b. Make a method to help you test your code:
printWordList() 
will print out wordsToAdd and wordsAdded, so you can see what happens as you write more code.

Now that you got the file input working:
3. In a program Driver.java 
-Make a Word Grid.
-Load the words, print out the ArrayList to see if it worked.

PART III test your overall functionality!

4. Try to add all of those words as randomly as possible using a function 
void fillWithWords()
-This function will use the ArrayList of words, and try to add them to the WordSearch randomly.
-When you successfully add a word, remove it from wordsToAdd, and place it into wordsAdded.
-You can add them all horizontally/vertically and see if they overlap.
-Add as many as you can to the WordSearch, but make sure it doesn't try too many times for each word, allow it to just skip to the next word if it can't find a spot. 

5. In a program Driver.java   Do as much as you can, but do not spend more than 1 hour tonight (1.5 if you are having some trouble)
 If you use facebook while working, then double the time. 
-Make a 7x7 WordSearch.
-Load the words
-Fill the WordSearch using your new function. (do not add random letters)
-Print the wordSearch.



import java.util.*; //random, scanner, arraylist
import java.io.*; //file, filenotfoundexception

public class WordSearch{
    private char[][]data;
    private Random randgen;
    private ArrayList<String>wordsToAdd;
    private ArrayList<String>wordsAdded;


/** METHODS **/

    public static void main(String[]args){
WordSearch w = new WordSearch(7,7);
System.out.println(w);//print a completed wordSearch

        //how to read from a file
try{
   Scanner in = new Scanner(new File("WordSearch.java"));
   //print each word in the text file:
   while(in.hasNext()){
String word = in.next();
System.out.println(word.toUpperCase());
   }
}catch(FileNotFoundException e){
   System.out.println("Invalid filename or path");
   System.exit(1);
}
    }
}

Comments