diff --git a/src/main/java/uta/cse3310/GameLogic.java b/src/main/java/uta/cse3310/GameLogic.java index f084ef6..f4a3500 100644 --- a/src/main/java/uta/cse3310/GameLogic.java +++ b/src/main/java/uta/cse3310/GameLogic.java @@ -2,8 +2,10 @@ import java.io.BufferedReader; -import java.io.FileReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -37,20 +39,26 @@ public char[][] getWordGrid() { return wordGrid; } - // Method to read words from a file - public void setWordsFromFile(String filePath) { + public void setWordsFromFile(String url) { List wordsList = new ArrayList<>(); - try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { - String line; - while ((line = br.readLine()) != null) { - wordsList.add(line.trim()); + try { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setRequestMethod("GET"); + + try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + String line; + while ((line = br.readLine()) != null) { + wordsList.add(line.trim()); + } } } catch (IOException e) { e.printStackTrace(); } + randomWords = wordsList.toArray(new String[0]); } + // Method to generate random words public void generateRandomWords(int wordCount) { randomWords = new String[wordCount]; diff --git a/src/test/java/uta/cse3310/GameLogicTest.java b/src/test/java/uta/cse3310/GameLogicTest.java index 6dffcac..f3b13bb 100644 --- a/src/test/java/uta/cse3310/GameLogicTest.java +++ b/src/test/java/uta/cse3310/GameLogicTest.java @@ -15,12 +15,15 @@ public void setUp() { } @Test - public void testSetWordsFromFile() { - gameLogic.setWordsFromFile("words.txt"); + public void testSetWordsFromURL() { + PlayerType player = new PlayerType("PlayerName", "PlayerColor", PlayerType.Status.Waiting); + GameLogic gameLogic = new GameLogic(player); + gameLogic.setWordsFromFile("https://raw.githubusercontent.com/utastudents/cse3310_sp24_group_8/main/src/main/java/uta/cse3310/words.txt"); String[] randomWords = gameLogic.getRandomWords(); assertNotNull(randomWords); assertTrue(randomWords.length > 0); } + @Test public void testGenerateRandomWords() { diff --git a/target/classes/uta/cse3310/GameLogic.class b/target/classes/uta/cse3310/GameLogic.class index 58ae0c8..41e2e67 100644 Binary files a/target/classes/uta/cse3310/GameLogic.class and b/target/classes/uta/cse3310/GameLogic.class differ diff --git a/target/test-classes/uta/cse3310/GameLogicTest.class b/target/test-classes/uta/cse3310/GameLogicTest.class index 5013e46..1caf392 100644 Binary files a/target/test-classes/uta/cse3310/GameLogicTest.class and b/target/test-classes/uta/cse3310/GameLogicTest.class differ