Skip to content

Commit

Permalink
gamelogic
Browse files Browse the repository at this point in the history
changes
  • Loading branch information
Guerrero96 committed Apr 16, 2024
1 parent d4a26a4 commit 3396457
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/main/java/uta/cse3310/GameLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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];
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/uta/cse3310/GameLogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Binary file modified target/classes/uta/cse3310/GameLogic.class
Binary file not shown.
Binary file modified target/test-classes/uta/cse3310/GameLogicTest.class
Binary file not shown.

0 comments on commit 3396457

Please sign in to comment.