i have a client-server architecture game. it’s designed with swing GUI, i have a problem in the game class, the class is supposed to show the 3 players’ names and scores, each time a player answers it should update the jLabel for that player
i wrote this code to update the label for the player who answered correct;y but it gave me an error:Exception in thread “AWT-EventQueue-0” Exception in thread “AWT-EventQueue-0” Exception in thread “AWT-EventQueue-0” java.lang.StackOverflowError
i asked and turned out to be an infinite recursion error but i don’t have any recursive methods in my codes nor have i called one, the error disappears when i delete this part of code (the SUBMITWORD method) but i can’t do this since it’s an important functionality.
i would be glad if someone could figure out the issue
here’s the code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.JOptionPane;
public class Game extends javax.swing.JFrame {
public String s1 = "aufiojdnble";
public String s2 = "gfyrhebdsho";
Info info = new Info();
public int correctAnswersCount = 0;
public int player1ans = 0;
public int player2ans = 0;
public int player3ans = 0;
public Server server ;
public String currentString;
public List<String> predefinedWordsS1 = List.of("found", "bad", "lion", "audio", "fun","job", "no", "bold", "bad", "no", "food", "blonde", "noble");
public List<String> predefinedWordsS2 = List.of("dog", "boy", "good", "her", "red", "hey", "yes", "fry", "hero", "shy", "bed", "shore", "body", "shoes");
public Game() {
initComponents();
displayLetters();
}
////////////////////////////////////////////////////////////////////////////
public void updatePlayerLabels(String[] playerNames) {
// System.out.println("Received player list from server: " + Arrays.toString(playerNames));
if (playerNames.length >= 1) {
jLabel4.setText(playerNames[0]);
}
if (playerNames.length >= 2) {
jLabel5.setText(playerNames[1]);
}
if (playerNames.length >= 3) {
jLabel6.setText(playerNames[2]);
}
}
////////////////////////////////////////////////////////////////////////////
public void displayLetters() { // works
currentString = wordSequence();
jLabel2.setText(currentString);
}
////////////////////////////////////////////////////////////////////////////
public String wordSequence() { // works
// Generate a random number (0 or 1)
Random random = new Random();
int randomNumber = random.nextInt(2);
return (randomNumber == 0) ? s1 : s2;
}
////////////////////////////////////////////////////////////////////////////
public void submitWord(String word) {
if (isValidWord(word, currentString)) {
correctAnswersCount++;
String name = info.getUsername();
String player1 = jLabel4.getText();
String player2 = jLabel5.getText();
String player3 = jLabel6.getText();
if (name.equalsIgnoreCase(player1)) {
player1ans++;
jLabel13.setText(String.valueOf(player1ans));
if (player1ans == 5) {
JOptionPane.showMessageDialog(this, "Congratulations! " + player1 + " has won the game!");
}
} else if (name.equalsIgnoreCase(player2)) {
player2ans++;
jLabel14.setText(String.valueOf(player2ans));
if (player2ans == 5) {
JOptionPane.showMessageDialog(this, "Congratulations! " + player2 + " has won the game!");
}
} else if (name.equalsIgnoreCase(player3)) {
player3ans++;
jLabel15.setText(String.valueOf(player3ans));
if (player3ans == 5) {
JOptionPane.showMessageDialog(this, "Congratulations! " + player3 + " has won the game!");
}
} else {
JOptionPane.showMessageDialog(this, "Invalid word. Please try again.");
}
jTextField1.setText("");
}
}
////////////////////////////////////////////////////////////////////////////
public boolean isValidWord(String word, String currentString) { // works
// Get the list of predefined words based on the currently displayed string
List<String> predefinedWords;
if (currentString.equals(s1)) {
predefinedWords = predefinedWordsS1;
} else if (currentString.equals(s2)) {
predefinedWords = predefinedWordsS2;
} else {
return false;
}
// Check if the entered word is in the list of predefined words
return predefinedWords.contains(word);
}
private void initializeComponents() {
initComponents();
jLabel4.setText("");
jLabel5.setText("");
jLabel6.setText("");
}