diff --git a/src/main/java/uta/cse3310/WordBank.java b/src/main/java/uta/cse3310/WordBank.java index 6975f89..96a138c 100644 --- a/src/main/java/uta/cse3310/WordBank.java +++ b/src/main/java/uta/cse3310/WordBank.java @@ -8,7 +8,6 @@ import java.util.Random; //known bugs -//wordsPlaced is not accurate. not every word in wordsPlaced is actually placed on the grid public class WordBank { private char[][] grid; @@ -25,7 +24,7 @@ public void addWordsFromFile(String fileName, List wordList) { try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { String line; while ((line = br.readLine()) != null) { - if (line.length() >= 4 && random.nextDouble() < 0.025 && wordList.size() < 100) { + if (line.length() >= 4 && random.nextDouble() < 0.015 && wordList.size() < 200) { wordList.add(line); } } @@ -54,10 +53,11 @@ public void generateGrid(int rows, int cols, List wordList) { Random random = new Random(); while (overlapCount < 5) { // Continue generating grid until at least 5 words are overlapped wordsPlaced.clear(); + wordLocations.clear(); for (String word : wordList) { boolean wordPlaced = false; int attempts = 0; - while (!wordPlaced && attempts < 10) { + while (!wordPlaced && attempts < 15) { int row = random.nextInt(rows); int col = random.nextInt(cols); @@ -91,7 +91,7 @@ public void generateGrid(int rows, int cols, List wordList) { end[0] = row+word.length()-1; end[1] = col; - wordLocations.add(new WordLocation(word, start[0], start[1], end[0], end[1], direction)); //This is causing errors + wordLocations.add(new WordLocation(word, start[0], start[1], end[0], end[1], direction)); } } else if (direction == 1 && col + word.length() <= cols) { // Horizontal placement @@ -124,7 +124,6 @@ public void generateGrid(int rows, int cols, List wordList) { end[0] = row; end[1] = col+word.length()-1; wordLocations.add(new WordLocation(word, start[0], start[1], end[0], end[1], direction)); - //ArrayList.add(new wordLocation(word,start,end,direction)); This is causing errors } } else if (direction == 2 && row - word.length() >= -1) { // Reverse vertical placement @@ -157,7 +156,6 @@ public void generateGrid(int rows, int cols, List wordList) { end[0] = row-word.length()+1; end[1] = col; wordLocations.add(new WordLocation(word, start[0], start[1], end[0], end[1], direction)); - //ArrayList.add(new wordLocation(word,start,end,direction)); This is causing errors } } else if (direction == 3 && col - word.length() >= -1) { // Reverse horizontal placement @@ -190,7 +188,6 @@ public void generateGrid(int rows, int cols, List wordList) { end[0] = row; end[1] = col-word.length()+1; wordLocations.add(new WordLocation(word, start[0], start[1], end[0], end[1], direction)); - //ArrayList.add(new wordLocation(word,start,end,direction)); This is causing errors } } else if (direction == 4 && row + word.length() <= rows && col + word.length() <= cols) { // Diagonal down-right placement @@ -221,7 +218,6 @@ public void generateGrid(int rows, int cols, List wordList) { end[0] = row+word.length()-1; end[1] = col+word.length()-1; wordLocations.add(new WordLocation(word, start[0], start[1], end[0], end[1], direction)); - //ArrayList.add(new wordLocation(word,start,end,direction)); This is causing errors } } attempts++; @@ -229,24 +225,24 @@ public void generateGrid(int rows, int cols, List wordList) { } if (overlapCount < 5) { clearGrid(); + wordsPlaced.clear(); + wordLocations.clear(); } } - // Print the grid printGrid(); - // Print the words placed // System.out.println("Overlapped Words: " + overlapCount); // System.out.println("Words Placed:"); // for (String word : wordsPlaced) { // System.out.println(word); // } - + fillEmptyCells(rows, cols); //printGrid(); //System.out.println("Word Location:"); - //for (String word : wordsPlaced) { //print for wordLocation after error is fixed + //for (String word : wordsPlaced) { //System.out.println(wordLocation); // } } @@ -275,23 +271,6 @@ private void fillEmptyCells(int rows, int cols) { } } - // public List getWordList() { - // return wordList; - // } - - // public String getRandomWord() { - // // Return a random word from wordList - // return null; - // } - - // public boolean containsWord(String word) { - // return wordList.contains(word); - // } - - // public boolean isValidWord(String word) { - // return wordList.contains(word); - // } - private void clearGrid() { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { diff --git a/src/main/java/uta/cse3310/WordLocation.java b/src/main/java/uta/cse3310/WordLocation.java index 385e734..f093867 100644 --- a/src/main/java/uta/cse3310/WordLocation.java +++ b/src/main/java/uta/cse3310/WordLocation.java @@ -66,4 +66,12 @@ public int getDirection() { public void setDirection(int direction) { this.direction = direction; } + + @Override + public String toString() { + return "Word: " + word + + ", Start: (" + startRow + ", " + startCol + ")" + + ", End: (" + endRow + ", " + endCol + ")" + + ", Direction: " + direction; + } } diff --git a/src/test/java/uta/cse3310/WordBankTest.java b/src/test/java/uta/cse3310/WordBankTest.java index 14c0970..dd0c0bb 100644 --- a/src/test/java/uta/cse3310/WordBankTest.java +++ b/src/test/java/uta/cse3310/WordBankTest.java @@ -15,6 +15,7 @@ public class WordBankTest { public List wordList; public WordBank wordBank; public List wordsPlaced; + private List wordLocations; @Before public void setUp() { @@ -38,10 +39,36 @@ public void testAddWordsFromFile() { public void testGenerateGrid() { assertTrue(wordsPlaced.isEmpty()); wordBank.generateGrid(25, 25, wordList); + System.out.println("Words Placed:"); wordsPlaced = wordBank.getWordsPlaced(); assertNotNull(wordsPlaced); //assertFalse(wordsPlaced.isEmpty()); } + //Test if wordsPlaced is correct + @Test + public void testWordsPlaced() { + assertTrue(wordsPlaced.isEmpty()); + //assertTrue(wordLocations.isEmpty()); + wordBank.generateGrid(25, 25, wordList); + + wordsPlaced = wordBank.getWordsPlaced(); + wordLocations = wordBank.getWordLocations(); + System.out.println("Word Location:"); + System.out.println(wordLocations.size()); + for (WordLocation location : wordLocations) { + System.out.println(location.toString()); + } + + System.out.println("Words Placed:"); + for (String word : wordsPlaced) { + System.out.println(word); + } + System.out.println(wordsPlaced.size()); + + assertNotNull(wordsPlaced); + assertTrue(wordsPlaced.size()==wordLocations.size()); + //assertFalse(wordsPlaced.isEmpty()); + } }