50.3k views
4 votes
The x varies depending on the number of letters you input. And the error persists despite changing the assignment of i or chosenWord, or changing the condition of the if. Even changing the boolean has left the error.

If the solution lies in changing the index, I'm not sure what to assign it.

The section of code where the problem lies (I think):

public static int showResults(String chosenWord, String guess) {

int numChar = 0;

boolean[] matched = new boolean[chosenWord.length()];

// Check for correct letters in the correct position

for (int i = 0; i < chosenWord.length(); i++) {

if (chosenWord.charAt(i) == guess.charAt(i)) {

numChar++;

matched[i] = true;

System.out.print(CORRECT);

}

}

// Check for correct letters in the wrong position

for (int i = 0; i < guess.length(); i++) {

if (!matched[i]) {

char c = guess.charAt(i);

int index = chosenWord.indexOf(c);

if (index != -1 && !matched[index]) {

System.out.print(RIGHT_LETTER_WRONG_PLACE);

matched[index] = true;

} else {

System.out.print(WRONG_LETTER);

}

}

}

System.out.println();

return numChar;

}

}

The full code:

import java.util.Scanner;

import java.io.File;

import java.io.FileNotFoundException;

public class Wordle {

//Game constants

final public static int NUMBER_WORDS = 255;

final public static int MAX_GUESSES = 6;

final public static int WORD_LENGTH = 5;

final public static String FILENAME = "Wordle.txt";

//Result Symbols

final public static char CORRECT = '*';

final public static char RIGHT_LETTER_WRONG_PLACE = '!';

final public static char WRONG_LETTER = 'X';

public static void main(String[] args) throws FileNotFoundException {

int result = 0;

int numGuess = 0;

Scanner keyboard = new Scanner(System.in);

// Get wordFromFile

String word = wordFromFile(NUMBER_WORDS, keyboard);

word = word.toLowerCase();

// Counting loop

while (numGuess < MAX_GUESSES && result != WORD_LENGTH) {

System.out.println("Enter your guess. Remember the word has 5 letters");

String userGuess = keyboard.nextLine();

userGuess = userGuess.toLowerCase();

// Check guess result and update counters

result = showResults(word, userGuess);

numGuess++;

// Check for win condition

if (result == WORD_LENGTH) {

System.out.println("You win!");

}

}

// For incorrect guesses

if (result != WORD_LENGTH) {

System.out.println("The word was " + word);

}

// Print out total guesses

System.out.println(numGuess + " / " + MAX_GUESSES);

}

public static String wordFromFile(int size, Scanner keyboard) throws FileNotFoundException {

int fileNum = 0;

Scanner file = new Scanner (new File(FILENAME));

//User picks word

System.out.println("Which word number would you like? The maximum is 255");

fileNum = keyboard.nextInt();

keyboard.nextLine();

//Skip number of lines in the file that the user entered

for (int b = 0; b < fileNum; b++)

{

file.nextLine();

}

//Skips until the line of the file that contains the word we want to read

file.next();

file.nextInt();

file.nextInt();

file.next();

file.nextInt();

//Reads in needed word

String chosenWord = file.nextLine();

file.close();

//Return chosen word

return chosenWord;

}

public static int showResults(String chosenWord, String guess) {

int numChar = 0;

boolean[] matched = new boolean[chosenWord.length()];

// Check for correct letters in the correct position

for (int i = 0; i < chosenWord.length(); i++) {

if (chosenWord.charAt(i) == guess.charAt(i)) {

numChar++;

matched[i] = true;

System.out.print(CORRECT);

}

}

// Check for correct letters in the wrong position

for (int i = 0; i < guess.length(); i++) {

if (!matched[i]) {

char c = guess.charAt(i);

int index = chosenWord.indexOf(c);

if (index != -1 && !matched[index]) {

System.out.print(RIGHT_LETTER_WRONG_PLACE);

matched[index] = true;

} else {

System.out.print(WRONG_LETTER);

}

}

}

System.out.println();

return numChar;

}

}

User Thexande
by
8.5k points

1 Answer

0 votes

Final answer:

The issue lies in the Java code for a Wordle-like game. An IndexOutOfBoundsException can occur if lengths of the chosen word and user guess differ, and an error in marking the 'matched' array can result in incorrect feedback. Safeguards need to be installed and logic restructured.

Step-by-step explanation:

The question described pertains to debugging Java code written for a Wordle-like game. The primary issue seems to be in the logic handling the comparison of characters between the chosen word and the user's guess. Specifically, this involves determining if the guessed characters are correct, and if they're in the right position.

The reported error is likely due to the assumption made in the code that guess.length() is equal to chosenWord.length(). If the lengths are not equal, attempting to access guess.charAt(i) or matched[i] where i is beyond the length of guess will cause an IndexOutOfBoundsException. One should ensure that the guess is always the same length as the chosen word or add an additional check to handle cases where the lengths might differ.

Another potential issue is with the reuse of the matched array, where the index in the chosen word at which a correct, but misplaced, letter is found might not be marked correctly in the matched array, potentially causing incorrect display of RIGHT_LETTER_WRONG_PLACE. To resolve these issues, consider verifying the lengths of the inputs and restructuring the logic to correctly mark the matched indices.

User Eron Wright
by
7.7k points