diff --git a/word-search/Board.java b/word-search/Board.java new file mode 100644 index 0000000..f14e6e5 --- /dev/null +++ b/word-search/Board.java @@ -0,0 +1,230 @@ +/* +* +* Matt Jensen +* CS145 +* Assignment 1 +* 4/17/19 +* +*/ +import java.util.*; +import java.awt.*; + +public class Board { + protected static enum Direction { + DOWN, UP, LEFT, RIGHT + }; + protected char[][] board; + protected char[][] solution; + protected int size; + protected String[] unplacedWords; + protected String[] placedWords; + protected String[] allWords; + public Board(String[] words) { + setWords(words); + this.size = getMaxWordSize(); + this.board = new char[size][size]; + this.solution = new char[size][size]; + placeWords(); + } + + public void placeWords() { + for( int i = 0; i < unplacedWords.length; i++) { + placeWord(unplacedWords[i]); + } + } + + + /** + * + * places a single word on the board. + * picks a random index i, j + * picks a random direction + * + * @return boolean the placement successful + */ + private void placeWord(String inputWord) { + + Direction direction = randomDirection(); + int rowIndex = (int) (Math.random() * (this.getSize())); + int columnIndex = (int) (Math.random() * (this.getSize())); + + int placementTries = 0; + for(int tries = 0; tries < 100; tries++) { + if(isPlaced(inputWord, rowIndex, columnIndex, direction)){ + for(int i = 0; i < this.placedWords.length; i++) { + if(this.placedWords[i] == null) { + placedWords[i] = inputWord; + return; + } + } + return; + } + direction = randomDirection(); + rowIndex = (int) (Math.random() * (this.getSize())); + columnIndex = (int) (Math.random() * (this.getSize())); + } + } + private boolean isPlaced(String word, int rowIndex, int columnIndex, Direction direction) { + char[][] currentBoard = copyBoard(this.getBoard()); + if( direction == Direction.DOWN ) { + for( int i = 0; i < word.length(); i++) { + if( currentBoard[i][columnIndex] == 0) { + currentBoard[i][columnIndex] = word.charAt(i); + + } else if( currentBoard[i][columnIndex] == word.charAt(i)){ + + currentBoard[i][columnIndex] = word.charAt(i); + } + else { + return false; + } + } + } + if( direction == Direction.UP ) { + for( int i = 0; i < word.length(); i++) { + if( currentBoard[word.length() - i - 1][columnIndex] == 0) { + currentBoard[word.length() - i - 1][columnIndex] = word.charAt(i); + } else if( currentBoard[word.length() - i - 1][columnIndex] == word.charAt(i)){ + currentBoard[word.length() - i - 1][columnIndex] = word.charAt(i); + } + else { + return false; + } + } + } + if( direction == Direction.LEFT ) { + for( int i = 0; i < word.length(); i++) { + if( currentBoard[rowIndex][word.length() - i - 1] == 0) { + currentBoard[rowIndex][word.length() - i - 1] = word.charAt(i); + } else if( currentBoard[rowIndex][word.length() - i - 1] == word.charAt(i)){ + currentBoard[rowIndex][word.length() - i - 1] = word.charAt(i); + } + else { + return false; + } + } + } + if( direction == Direction.RIGHT ) { + for( int i = 0; i < word.length(); i++) { + if( currentBoard[rowIndex][i] == 0) { + currentBoard[rowIndex][i] = word.charAt(i); + + } else if( currentBoard[rowIndex][i] == word.charAt(i)){ + + currentBoard[rowIndex][i] = word.charAt(i); + } + else { + return false; + } + } + } + setBoard(currentBoard); + return true; + } + public char[][] getBoard() { + return this.board; + } + private void setBoard(char[][] newBoard) { + this.board = copyBoard(newBoard); + this.solution = copyBoard(newBoard); + } + private static char[][] copyBoard(char[][] old) { + if (old == null) { + return null; + } + char[][] result = new char[old.length][]; + for (int r = 0; r < old.length; r++) { + result[r] = old[r].clone(); + } + return result; + } + + /** + * + * get board size based on largest word input. + * + * @return void + */ + private int getMaxWordSize() { + int max = 0; + for( int i = 0; i < allWords.length; i++) { + if( allWords[i].length() > max ) { + max = allWords[i].length(); + } + } + return max; + } + public void setWords(String[] words){ + this.allWords = words; + this.unplacedWords = words; + this.placedWords = new String[words.length]; + } + public int getSize() { + return this.size; + } + + public String toString() { + return "Hidden Words:\n" + + this.placedWordsToString() + "\n" + + boardToString(this.getBoard(), false); + } + public String solutionToString() { + return "Words:\n" + + this.placedWordsToString() + "\n" + + boardToString(this.getBoard(), true); + } + public String[] getPlacedWords() { + return this.placedWords; + } + private String placedWordsToString() { + String result = ""; + for(int i = 0; i < this.getPlacedWords().length; i++) { + if( this.getPlacedWords()[i] != null ) { + result = result + "| " + this.getPlacedWords()[i] + " "; + } + } + return result + "|\n"; + } + private static String boardToString(char[][] boardForPrint, boolean solutions) { + char[] CHAR_ALPHA = "abcdefghijklmnopqrstuvwxyz".toCharArray(); + Random random = new Random(CHAR_ALPHA.length); + String text = ""; + for(int i = 0; i < boardForPrint.length; i++) { + text = text + "| "; + for(int j = 0; j < boardForPrint[i].length; j++) { + if(boardForPrint[i][j] == 0) { + if(solutions) { + text = text + "- "; + } else { + int randomCharIndex = random.nextInt(CHAR_ALPHA.length); + char randomChar = CHAR_ALPHA[randomCharIndex]; + text = text + randomChar + " "; + } + } else { + text = text + boardForPrint[i][j] + " "; + } + } + text = text + "|\n"; + } + return text; + + } + + protected static Direction randomDirection() { + int index = (int) (Math.random() * 4); + + if( index == 0) { + return Direction.UP; + } + if( index == 1) { + return Direction.DOWN; + } + if( index == 2) { + return Direction.RIGHT; + } + if( index == 3) { + return Direction.LEFT; + } + return Direction.UP; + } +} diff --git a/word-search/ConsolePrompt.java b/word-search/ConsolePrompt.java new file mode 100644 index 0000000..e56b146 --- /dev/null +++ b/word-search/ConsolePrompt.java @@ -0,0 +1,158 @@ +/* +* +* Matt Jensen +* CS145 +* Assignment 1 +* 4/25/19 +* +*/ + +import java.util.*; +import java.awt.*; +import java.io.*; + +public class ConsolePrompt { + + private static String[] options = {"Generate a new word search", "Print out your word search", "Show the solutions to your word search", "Quit the program"}; + private static String[] values = {"g", "p", "s", "q"}; + private static boolean DEBUG = false; + + // print the introduction to the program on start up. + public static void intro() { + System.out.println("Welcome to my word search generator"); + System.out.println("This program allows you to generate your own word search puzzle"); + } + + // prompts for words. + // puts them in an array. + // pulls from a file if not null + public static String[] forWords(int count, String fileName, Scanner console) throws FileNotFoundException { + String[] words = new String[count]; + if(fileName != null) { + Scanner scanner = new Scanner(new File("words.txt")); + for(int i = 0; i < words.length; i++) { + words[i] = scanner.nextLine(); + } + } + else { + String question = "Next word:"; + String input = ""; + for(int i = 0; i < words.length; i++) { + input = prompt(question, console); + words[i] = input; + } + + } + return words; + } + + // prompts for filename + // TODO: check for file existance + public static String forFileName( Scanner console) { + String question = "Generate from file?"; + boolean fromFile = forBoolean(question, console); + if(!fromFile) { + return null; + } + question = "Filename?"; + String fileName = prompt(question, console); + while( ! isFile(fileName) ) { + System.out.println("Unrecognized input."); + fileName = prompt(question, console); + } + return fileName; + } + + // prompts for word cound + // prompts again if not an integer + public static int forWordCount(Scanner console) { + String question = "How many words would you like to include in the word search?"; + String input = prompt(question, console); + while( ! isInteger(input) ) { + System.out.println("Unrecognized input."); + input = prompt(question, console); + } + return getInteger(input); + } + + // TODO: validate if an integer + private static boolean isInteger(String input) { + return true; + } + // TODO: validate if file exists + private static boolean isFile(String input) { + return true; + } + // gets integer form input + private static int getInteger(String input) { + return Integer.parseInt(input); + } + + private static String getString(String input) { + return input; + } + + private static String prompt(String prompt, Scanner console) { + System.out.println(prompt); + String response = console.next(); + System.out.println(); + return response; + } + // returns all the possible options + public static String getOptionPrompt() { + String prompt = "Please select an option below:\n"; + for( int i = 0; i < options.length; i++ ) { + prompt += options[i] + " (" + values[i] + ")\n"; + } + return prompt; + } + + // prompts from option input + // rejects options outside of class variable + public static char forOption( Scanner console ) { + String question = getOptionPrompt(); + String input = prompt(question, console); + while( ! isOption(input) ) { + System.out.println("Unrecognized input."); + input = prompt(question, console); + } + return getOption(input); + } + // validates if character is an option + private static boolean isOption(String input) { + for(int i = 0; i < values.length; i++) { + if( input.startsWith(values[i]) ) { + return true; + } + } + return false; + } + // return first character of input for options. + private static char getOption(String input) { + return input.charAt(0); + } + + // prompts for boolean value + public static boolean forBoolean( String prompt, Scanner console ) { + String input = prompt(prompt, console); + while( ! isBooleanInput(input) ) { + System.out.println("Unrecognized input."); + input = prompt(prompt, console); + } + return getBooleanInput(input); + } + // validates boolean value + private static boolean isBooleanInput(String input) { + return input.startsWith("y") || input.startsWith("Y") || input.startsWith("n") || input.startsWith("N"); + } + // convert string input to boolean value. + private static boolean getBooleanInput(String input) { + if(input.startsWith("y") || input.startsWith("Y")) { + return true; + } + if(input.startsWith("n") || input.startsWith("N")) { + return false; + } + return false; + } +} diff --git a/word-search/README.md b/word-search/README.md new file mode 100644 index 0000000..699edd8 --- /dev/null +++ b/word-search/README.md @@ -0,0 +1,11 @@ +# Word Search Generator + +## topics +Printing, data types, methods, operators, expressions, variables, for loop, +parameters, returns, String objects, Scanner object, if/else statements, while loop, arrays + +## Learning Outcomes: +- Begin to become familiar with setup, design, execution and testing of basic Java programs +- Design and develop a multi-method program in good style +- Demonstrate the ability to decompose a problem and reduce redundancy using methods +- Apply tools and techniques introduced in class to form a workin diff --git a/word-search/WordSearch.java b/word-search/WordSearch.java new file mode 100644 index 0000000..848dbdf --- /dev/null +++ b/word-search/WordSearch.java @@ -0,0 +1,81 @@ +/* +* +* Matt Jensen +* CS145 +* Assignment 1 +* 4/25/19 +* +*/ + +import java.util.*; +import java.awt.*; +import java.io.*; + +public class WordSearch { + + protected static boolean DEBUG; + protected static Board board; + private static int wordCount = 20; + + public static void main( String[] args ) throws FileNotFoundException { + Scanner console = new Scanner( System.in ); + ConsolePrompt.intro(); + char option = ConsolePrompt.forOption(console); + while(option != 'q') { + if(option == 'g') { + String fileName = ConsolePrompt.forFileName(console); + generate(fileName, console); + } + if(option == 's') { + showSolution(console); + } + if(option == 'p') { + showBoard(console); + } + System.out.println(); + option = ConsolePrompt.forOption(console); + } + System.out.println("Goodbye!"); + return; + } + + /** + * + * generates a new board from the set words. + * + * @param console console for printing prompts to. + * @return void + */ + protected static void generate(String fromFile, Scanner console) throws FileNotFoundException { + int wordCount = ConsolePrompt.forWordCount(console); + board = new Board( + ConsolePrompt.forWords(wordCount, fromFile, console) + ); + } + + + + + /** + * + * prints the solution (x's for letters between words) + * + * @param console console for printing prompts to. + * @return void + */ + protected static void showSolution(Scanner console) { + System.out.println(board.solutionToString()); + } + + /** + * + * prints the entire board with words. + * + * @param console console for printing prompts to. + * @return void + */ + protected static void showBoard(Scanner console) { + System.out.println(board); + } + +} diff --git a/word-search/specification.pdf b/word-search/specification.pdf new file mode 100644 index 0000000..d86885c Binary files /dev/null and b/word-search/specification.pdf differ diff --git a/word-search/words.txt b/word-search/words.txt new file mode 100644 index 0000000..a89b38f --- /dev/null +++ b/word-search/words.txt @@ -0,0 +1,24 @@ +spell +tremble +page +embarrass +care +oval +close +believe +hall +magenta +count +shade +tight +utopian +haunt +wary +tent +bomb +complain +preserve +thirsty +curve +design +flippant