Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎


posted Nov 10, 2017, 10:17 AM by Konstantinovich Samuel   [ updated Nov 12, 2017, 1:02 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:


Update to your WordSearch with 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;

Update your wordSearch with the methods discussed in class:

//Two Constructors:
public WordSearch( int rows, int cols, String fileName)

public WordSearch( int rows, int cols, String fileName, int randSeed)

//Two Methods
private boolean addWord( int r, int c, String word, int rowIncrement, int colIncrement)
    -when colIncrement and rowIncrement are both 0, return false. 
    -when you successfully add a word, move the word from wordsToAdd to wordsAdded, then return true.
    -return false otherwise. 

private boolean addAllWords()
    -Attempt to add all of the words from the wordsToAdd list. Keep trying many times* or until you run out.
    -Experiment with *many times, maybe 1000 is enough, maybe 10000. If it is slow, cut it down!

Finally, make sure your toString verifies your words are adding properly. Feel free to print the object inside of its own methods as a debug!!!!

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

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