Home > front end >  Method not labelling characters correctly from a linked list
Method not labelling characters correctly from a linked list

Time:02-22

I have created a game of wordle where you guess an unknown word. I am using a linked list. If one or more characters exist in the word but not in the correct position the letter is surrounded with a . If the character is not in the word it is surrounded with a -. If it is in the correct position it is surrounded by !. My labelWord(Word mystery) method, where mystery is the unknown word checks if the two words are equal, then a toString method is used to output the guess word with the tags. For example, if the mystery word is "CHINA" and the guess is "CHARM" the toString() method will output: "Word: !C! !H! A -R- -M- ". I have run into difficulty when the guess word is longer than the mystery word.

Word word5 = new Word(Letter.fromString("OBJECT"));
Word word6 = new Word(Letter.fromString("CODE"));

word5.labelWord(word6);

System.out.println(word5.toString());

In the above code, I am guessing the word "OBJECT" for the mystery word "CODE". However, this outputs

"Word: O -B- -J- !E! C T ". The method does not label any of the letters that are outside the length of the mystery word. How can I adjust my method to be able to label characters in words that are longer than the mystery word?

public Word(Letter[] letters) {

        LinearNode<Letter> prevLetter = null;
        LinearNode<Letter> currentLetter;

        for (int i = 0; i< letters.length; i  ) {
            currentLetter = new LinearNode<>(letters[i]);
            if (i == 0) {
                this.firstLetter = currentLetter;
                prevLetter = currentLetter;
                continue;
            }
            prevLetter.setNext(currentLetter);
            prevLetter = currentLetter;
        }
    }
public boolean labelWord(Word mystery) {
        LinearNode<Letter> otherNode = mystery.firstLetter;
        LinearNode<Letter> thisNode = this.firstLetter;
        boolean isEqual = true;
        while(true){
            if(thisNode == null || otherNode == null){
                if(thisNode == null && otherNode == null){
                    break;
                }
                isEqual = false;
                break;
            }
            if(thisNode.getElement().equals(otherNode.getElement())){
                thisNode.getElement().setCorrect();
            }
            else{
                if(mystery.contains(thisNode)){
                    thisNode.getElement().setUsed();
                }
                else {
                    thisNode.getElement().setUnused(); 
                }
                isEqual = false;
            }
            thisNode = thisNode.getNext();
            otherNode = otherNode.getNext();
        }
        return isEqual;
    }

private boolean contains(LinearNode<Letter> letter) {
        LinearNode<Letter> currentNode = firstLetter;
        while (currentNode != null) {
            if (currentNode.getElement().equals(letter.getElement())) {
                return true;
            }
            currentNode = currentNode.getNext();
        }
        return false;
    }

public String toString() {
        String str = "Word: ";
        LinearNode<Letter> currentNode = firstLetter;
        while (currentNode != null) {
            if (currentNode.getElement() == null) {
                str  = currentNode.getElement().toString();
                break;
            }
            str  = currentNode.getElement().toString()   " ";
            currentNode = currentNode.getNext();
        }
        return str;
    }

CodePudding user response:

In labelWord, you break when otherNode is null so the next letters of thisNode aren't evaluated. So get rid of these tests or change

otherNode = otherNode.getNext();

to

if (otherNode.getNext() != null) {
   otherNode = otherNode.getNext();
}

CodePudding user response:

True indeed. You could try this :

while(true){
     if(thisNode == null){  //the evaluation continues even with a shorter mystery
         isEqual = false;
         break;
     }
[...]
    thisNode = thisNode.getNext();
    otherNode = otherNode.getNext();
    if(thisNode == null && otherNode == null){ //keeps equality
        break;   
    }
    return isEqual;
}
  • Related