12-10-2013

posted Dec 10, 2013, 8:41 AM by Samuel Konstantinovich   [ updated Dec 10, 2013, 8:49 AM ]
Goal:
minimum:
wordsearch must take these parameters:
rows
cols
filename


bonus points:
wordsearch takes any of these sets of parameters:
rows 
cols 
filename
rows 
cols 
filename randomSeed
rows 
cols 
filename randomSeed cheatFlag

Examples:
java wordsearch 12 12 names.txt
java wordsearch 12 12 names.txt 1923
java wordsearch 12 12 names.txt 1923 1



Solutions to some word search problems:

import java.util.*;
public class WordGrid{

    private char[][]data;

    public void randomize(Random rand){
        for(int i = 0; i < data.length; i++){
            for(int j = 0; j < data[i].length; j++){
                if(data[i][j]=='_')
                    data[i][j]= (char)( 'a'+(int)(rand.nextDouble()*26) );
            }
        }
    }

   public boolean addWord(String word, int row, int col, int drow, int dcol){
        if(drow == 0 && dcol == 0){
            return false;
        }
        /*check if the word doesn't change letters*/
        try{

            for(int i = 0; i < word.length(); i++){
                char letter = data[row + i * drow][col + i * dcol];
                if(letter != '_' && letter != word.charAt(i)){
                    return false;
                }
            }
        }catch(ArrayIndexOutOfBoundsException e){
            return false;
        }
        /*write out the letters to the array*/
        for(int i = 0; i < word.length(); i++){
            data[row + i * drow][col + i * dcol] = word.charAt(i);
        }
        return true;
    }


...
...
}
Comments