Skip to content

Commit

Permalink
Fixed CPU-intensive infinite loop in WordList generation process.
Browse files Browse the repository at this point in the history
  • Loading branch information
2onefan2 committed Apr 29, 2024
1 parent 3822b79 commit 869f413
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions src/main/java/uta/cse3310/WordList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uta.cse3310;

import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -8,55 +9,50 @@
import java.nio.file.Path;
import java.nio.file.Paths;

public class WordList //extends Direction
{
public class WordList {
public static List<String> list;
//private [] words;

//method for accesing the word list from file
public static ArrayList<String> getWordList(String wordsFile)
{
public static ArrayList<String> getWordList(String wordsFile) {
ArrayList<String> wordList = new ArrayList<>();

try {
list = Files.readAllLines(Paths.get(wordsFile));
List<String> lines = Files.readAllLines(Paths.get(wordsFile));
if (lines.isEmpty()) {
System.out.println("The file is empty: " + wordsFile);
return wordList; // Return empty list if the file is empty
}

list = new ArrayList<>(lines);
shuffleWords();
wordList.addAll(list);
} catch (IOException e) {
System.out.println("not a valid file!");
System.out.println("Error reading the file: " + wordsFile);
e.printStackTrace(); // Print the stack trace for debugging
}

return wordList;
}

//method for word shuffling
public static void shuffleWords()
{
Collections.shuffle(list);
public static void shuffleWords() {
Collections.shuffle(list);
}

public static ArrayList<String> updatedWordList(ArrayList<String> wordBank)
{
public static ArrayList<String> updatedWordList(ArrayList<String> wordBank) {
int totalCharacters = 0;
int index = 0;
ArrayList<String> updatedList = new ArrayList<>();

while (totalCharacters < 500 && index < wordBank.size())
{
while (totalCharacters < 500 && index < wordBank.size()) {
String word = wordBank.get(index);
if (totalCharacters < 500)
{
if (totalCharacters + word.length() <= 500) {
updatedList.add(word);
totalCharacters += word.length();
} else
{
} else {
break;
}
index++;
}

return updatedList;
}

}

}

0 comments on commit 869f413

Please sign in to comment.