Skip to content

Commit

Permalink
WordBank bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
bcg2488 committed Apr 30, 2024
1 parent b0c9f25 commit cdaf30e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
37 changes: 8 additions & 29 deletions src/main/java/uta/cse3310/WordBank.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +24,7 @@ public void addWordsFromFile(String fileName, List<String> 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);
}
}
Expand Down Expand Up @@ -54,10 +53,11 @@ public void generateGrid(int rows, int cols, List<String> 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);

Expand Down Expand Up @@ -91,7 +91,7 @@ public void generateGrid(int rows, int cols, List<String> 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
Expand Down Expand Up @@ -124,7 +124,6 @@ public void generateGrid(int rows, int cols, List<String> 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
Expand Down Expand Up @@ -157,7 +156,6 @@ public void generateGrid(int rows, int cols, List<String> 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
Expand Down Expand Up @@ -190,7 +188,6 @@ public void generateGrid(int rows, int cols, List<String> 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
Expand Down Expand Up @@ -221,32 +218,31 @@ public void generateGrid(int rows, int cols, List<String> 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++;
}
}
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);
// }
}
Expand Down Expand Up @@ -275,23 +271,6 @@ private void fillEmptyCells(int rows, int cols) {
}
}

// public List<String> 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++) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/uta/cse3310/WordLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
27 changes: 27 additions & 0 deletions src/test/java/uta/cse3310/WordBankTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class WordBankTest {
public List<String> wordList;
public WordBank wordBank;
public List<String> wordsPlaced;
private List<WordLocation> wordLocations;

@Before
public void setUp() {
Expand All @@ -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());
}
}

0 comments on commit cdaf30e

Please sign in to comment.