Code
Code
*; //gui
sta c Set<String> dic onary = new HashSet<>(); //set of valid words loaded from the dic onary
file to avoid duplicates
String le erSet;
char mustInclude;
int mistakes = 0;
int totalPoints = 0;
JLabel wordLabel;
JLabel scoreLabel;
JLabel mistakesLabel;
JTextArea usedWordsArea;
displayInstruc ons();
loadDic onary();
setupGame();
setupGUI();
How It Works:
2. You can form words from the given le ers, but each word must include the compulsory
le er.
3. Words are checked against a dic onary, and points are awarded based on the Scrabble
point system.
""";
String line;
while ((line = reader.readLine()) != null) { //loop to read each line, one line= one word
void setupGame() {
.collect(Collectors.joining()); //requires strings not char, concatenates the strings into one to
easily display in le erSet
void setupGUI() {
setSize(400, 600);
setLayout(new BorderLayout());
setDefaultCloseOpera on(EXIT_ON_CLOSE);
le ersPanel.setLayout(null);
wordLabel.setForeground(Color.WHITE);
scoreLabel.setForeground(Color.WHITE);
mistakesLabel.setForeground(Color.WHITE);
infoPanel.add(wordLabel);
infoPanel.add(scoreLabel);
infoPanel.add(mistakesLabel);
infoPanel.setBackground(Color.BLACK);
add(infoPanel, BorderLayout.NORTH);
le ersPanel.add(bu on);
le ersPanel.add(mustIncludeBu on);
bo omPanel.setBackground(Color.BLACK);
bo omPanel.add(deleteBu on);
bo omPanel.add(submitBu on);
usedWordsArea.setEditable(false);
usedWordsArea.setBackground(Color.BLACK);
usedWordsArea.setForeground(Color.WHITE);
usedWordsArea.setText("Used Words:\n");
setVisible(true);
bu on.setBackground(color);
return bu on;
currentWord.append(le er);
updateWordLabel();
void updateWordLabel() {
if (currentWord.length() > 0) {
currentWord.deleteCharAt(currentWord.length() - 1);
updateWordLabel();
void submitWord() {
if (usedWords.contains(word)) {
showError("You have already used that word.");
mistakes++;
} else if (!word.contains(String.valueOf(mustInclude))) {
mistakes++;
} else if (!isValidWord(word)) {
mistakes++;
} else {
totalPoints += points;
usedWords.add(word);
JOp onPane.showMessageDialog(this, "Word accepted! You earned " + points + " points.");
updateUsedWords(word);
JOp onPane.showMessageDialog(this, "Game Over! Your final score is: " + totalPoints);
System.exit(0);
currentWord.setLength(0);
updateWordLabel();
}
if (usedWords.contains(word)) {
return true;
if (word.length() < 4) {
mistakes++;
return true;
} else if (!word.contains(String.valueOf(mustInclude))) {
mistakes++;
return true;
} else if (!isValidWord(word)) {
mistakes++;
return true;
return false;
void endGame() {
JOp onPane.showMessageDialog(this, "Game Over! Your final score is: " + totalPoints);
System.exit(0);
return 0;
}).sum();
int vowelCount = 0;
le ers.add(le er);
vowelCount++;
}
// Fill the rest with random le ers
le ers.add(le er);
return le ers;