Skip to content

Commit

Permalink
feat: use better random words
Browse files Browse the repository at this point in the history
  • Loading branch information
Tosox committed Jul 6, 2023
1 parent 69ff06a commit d8de4ba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
24 changes: 14 additions & 10 deletions src/main/java/de/tosoxdev/tosoxjr/commands/hangman/Hangman.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@

import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

public class Hangman {
public static final HashMap<String, String> RANDOM_WORD_APIS = new HashMap<>(Map.of(
"en", "https://random-word-api.vercel.app/api?words=1",
"de", "https://alex-riedel.de/randV2.php?anz=1"
"en", "https://capitalizemytitle.com/wp-content/tools/random-word/en/nouns.txt",
"de", "https://capitalizemytitle.com/wp-content/tools/random-word/de/nouns.txt"
));
private static final HashMap<String, List<String>> RANDOM_WORD_LIST = new HashMap<>();

static {
for (Map.Entry<String, String> entry : RANDOM_WORD_APIS.entrySet()) {
String response = APIRequest.getString(entry.getValue());
RANDOM_WORD_LIST.put(entry.getKey(), response != null ? List.of(response.split(",")) : null);
}
}

private static final String API_DICTIONARY = "https://www.dictionaryapi.com/api/v3/references/collegiate/json/%s?key=%s";
private static final int REGIONAL_INDICATOR_A_CP = 0x1F1E6;
Expand Down Expand Up @@ -202,14 +211,9 @@ private void endGame(GameState state) {
}

private String generateWord() {
String lang = RANDOM_WORD_APIS.getOrDefault(language.toLowerCase(), RANDOM_WORD_APIS.get("en"));
String response = APIRequest.getString(lang);
if (response == null) {
return null;
}

// Remove brackets and quotation marks
return response.substring(2, response.length() - 2);
List<String> words = RANDOM_WORD_LIST.getOrDefault(language.toLowerCase(), RANDOM_WORD_LIST.get("en"));
int randomIdx = ThreadLocalRandom.current().nextInt(words.size());
return words.get(randomIdx);
}

private String showWord() {
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/de/tosoxdev/tosoxjr/commands/scramble/Scramble.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class Scramble {
public static final HashMap<String, String> RANDOM_WORD_APIS = new HashMap<>(Map.of(
"en", "https://random-word-api.vercel.app/api?words=1",
"de", "https://alex-riedel.de/randV2.php?anz=1"
"en", "https://capitalizemytitle.com/wp-content/tools/random-word/en/nouns.txt",
"de", "https://capitalizemytitle.com/wp-content/tools/random-word/de/nouns.txt"
));
private static final HashMap<String, List<String>> RANDOM_WORD_LIST = new HashMap<>();

static {
for (Map.Entry<String, String> entry : RANDOM_WORD_APIS.entrySet()) {
String response = APIRequest.getString(entry.getValue());
RANDOM_WORD_LIST.put(entry.getKey(), response != null ? List.of(response.split(",")) : null);
}
}

private static final int TIMEOUT_MS = 2 * 60 * 1000;
private static final int STOP_SIGN_CP = 0x1F6D1;

Expand Down Expand Up @@ -129,19 +139,9 @@ private void endGame(GameState state, String sender) {
}

private String generateWord() {
String lang = RANDOM_WORD_APIS.getOrDefault(language.toLowerCase(), RANDOM_WORD_APIS.get("en"));
String response = APIRequest.getString(lang);
if (response == null) {
return null;
}

// Remove brackets and quotation marks
response = response.substring(2, response.length() - 2);

// Replace german umlauts
response = response.replace("&auml;", "ä").replace("&ouml;", "ö").replace("&uuml;", "ü").replace("&szlig;", "ß");

return response;
List<String> words = RANDOM_WORD_LIST.getOrDefault(language.toLowerCase(), RANDOM_WORD_LIST.get("en"));
int randomIdx = ThreadLocalRandom.current().nextInt(words.size());
return words.get(randomIdx);
}

private EmbedBuilder createGameEmbed(GameState state, String sender) {
Expand Down

0 comments on commit d8de4ba

Please sign in to comment.