Skip to content

Commit

Permalink
Updated WordPuzzle.java
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshyab02 committed Aug 1, 2024
1 parent e499750 commit 247f685
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions src/main/java/uta/cse3310/WordPuzzle.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,67 @@
import java.util.stream.Collectors;

public class WordPuzzle {
    private List<String> words;
    private String displayedPuzzle;
    private String currentPuzzle;
private List<String> words;
private String displayedPuzzle;
private String currentPuzzle;

    public WordPuzzle() {
        words = new ArrayList<>();
        displayedPuzzle = "";
        currentPuzzle = "";
        loadWords(getWordSource());
    }
public WordPuzzle() {
words = new ArrayList<>();
displayedPuzzle = "";
currentPuzzle = "";
loadWords(getWordSource());
}

    private String getWordSource() {
        String testGrid = System.getenv("TEST_GRID");
        if (testGrid != null) {
            return "src/main/resources/test_words_" + testGrid + ".txt";
        }
        return "src/main/resources/words.txt";
    }
private String getWordSource() {
String testGrid = System.getenv("TEST_GRID");
if (testGrid != null) {
return "src/main/resources/test_words_" + testGrid + ".txt";
}
return "src/main/resources/words.txt";
}

    public void loadWords(String filePath) {
        try {
            words = Files.lines(Paths.get(filePath))
                .filter(word -> word.length() >= 3 && word.matches("^[a-z]+$"))
                .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
public void loadWords(String filePath) {
try {
words = Files.lines(Paths.get(filePath))
.filter(word -> word.length() >= 3 && word.matches("^[a-z]+$"))
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
}

    public void generatePuzzle(int numberOfWords) {
        Random random = new Random();
        List<String> selectedWords = new ArrayList<>();
        for (int i = 0; i < numberOfWords; i++) {
            selectedWords.add(words.get(random.nextInt(words.size())));
        }
        currentPuzzle = String.join(" ", selectedWords);
        displayedPuzzle = currentPuzzle.replaceAll("[a-z]", "_");
    }
public void generatePuzzle(int numberOfWords) {
Random random = new Random();
List<String> selectedWords = new ArrayList<>();
for (int i = 0; i < numberOfWords; i++) {
selectedWords.add(words.get(random.nextInt(words.size())));
}
currentPuzzle = String.join(" ", selectedWords);
displayedPuzzle = currentPuzzle.replaceAll("[a-z]", "_");
}

    public boolean revealLetter(char letter) {
        boolean revealed = false;
        StringBuilder newDisplayed = new StringBuilder(displayedPuzzle);
        for (int i = 0; i < currentPuzzle.length(); i++) {
            if (Character.toLowerCase(currentPuzzle.charAt(i)) == Character.toLowerCase(letter) && displayedPuzzle.charAt(i) == '_') {
                newDisplayed.setCharAt(i, currentPuzzle.charAt(i));
                revealed = true;
            }
        }
        displayedPuzzle = newDisplayed.toString();
        return revealed;
    }
public boolean revealLetter(char letter) {
boolean revealed = false;
StringBuilder newDisplayed = new StringBuilder(displayedPuzzle);
for (int i = 0; i < currentPuzzle.length(); i++) {
if (Character.toLowerCase(currentPuzzle.charAt(i)) == Character.toLowerCase(letter) && displayedPuzzle.charAt(i) == '_') {
newDisplayed.setCharAt(i, currentPuzzle.charAt(i));
revealed = true;
}
}
displayedPuzzle = newDisplayed.toString();
return revealed;
}

    public boolean checkSolved(String guess) {
        return guess.equalsIgnoreCase(currentPuzzle);
    }
public boolean checkSolved(String guess) {
return guess.equalsIgnoreCase(currentPuzzle);
}

    public String getDisplayedPuzzle() {
        return displayedPuzzle;
    }
public String getDisplayedPuzzle() {
return displayedPuzzle;
}

    public String getCurrentPuzzle() {
        return currentPuzzle;
    }
public String getCurrentPuzzle() {
return currentPuzzle;
}
}

0 comments on commit 247f685

Please sign in to comment.