import java.util.*; import java.io.*; public class Wordle { static ArrayList words = new ArrayList(); static String targetWord; public static void main(String[] args) { // Load words from the file try { Scanner input = new Scanner(new File("words.txt")); while (input.hasNextLine()) { words.add(input.nextLine()); } input.close(); } catch (FileNotFoundException e) { System.out.println("Error: words.txt not found"); System.exit(0); } // Randomly pick one word int targetIndex = (int) (Math.random() * 4499); targetWord = words.get(targetIndex); // Loop 6 times for the player to guess for (int i = 0; i < 6; i++) { System.out.print("Enter your guess: "); Scanner userInput = new Scanner(System.in); String guess = userInput.nextLine(); // Check if the guess is valid while (!words.contains(guess)) { System.out.println("Invalid guess, try again"); System.out.print("Enter your guess: "); guess = userInput.nextLine(); } // Check if the guess is the same as the target word if (guess.equals(targetWord)) { System.out.println("Congratulations! You won"); System.exit(0); } // Give feedback to the player giveFeedback(guess); } // Player lost System.out.println("You lost. The word was " + targetWord); } static void giveFeedback(String guess) { for (int i = 0; i < 5; i++) { char c = guess.charAt(i); if (c == targetWord.charAt(i)) { System.out.println(c + " is exactly right"); } else if (targetWord.indexOf(c) != -1) { System.out.println(c + " is there, but not in the right spot"); } } } }