0

I am trying to make a project for school where I have to guess a word. If the word is right, it gets a red dot and if it's in there somewhere, it will get a yellow dot and if there is no similarity, it will get a gray dot. I have made an array that contains the word that has to be guessed that's named array and an array that contains the word being guessed having one character in each array entry.

I thought some setup like this would be viable:

place = 0;   // The tekenaarextra and checkletters are not the problem that's just the result that happens if they are true

while(place <5){
    if(array[place] == array2[place]){
        System.out.println(array[1]);
        checkletters(place,1,Color.RED);
        tekenaarextra2();
        place ++;
    }else if( array[0] == array2[place] || array[1] == array2[place] || array[2] == array2[place] || array[3] == array2[place] || array[4] == array2[place] ){ 
        checkletters(place,1,Color.YELLOW);
        tekenaarextra2();
        place ++;
    }else{
        checkletters(place,1,Color.GRAY); // this is just a method to draw the dots.
        tekenaarextra2();
        place ++;
    }
}

However, whilst using this I get error messages and I do not quite know how to really compare separate entries from the arrays without putting them into different Strings which is a lot of separate work.

For example, I got String[] array = {a,p,p,l,e}, and I got String[] array2 = {p,l,a,t,e}

I want to then compare the first entry from the array2 to the first one of the array and if they're the same character, I should execute a certain command to draw a dot if it's not so, else if it should compare the first entry from array 2 to all the other entries to see if it needs a yellow dot. If it all tests false and it doesn't contain any of them, it should just have an else that leads into drawing a gray dot. And this over 5 times to compare all the letters of array 2, but that can be done simply by a while statement.

Simplified version of what I need to do

if(array[0]==array2[0]){ // if the first letter of guessed word is the  first letter in the mystery word
  drawreddot();
} else if( array[0] == array2[1] || array[0] == array2[2] || array[0] == array2[3] || array[0] == array2[4]){ 
// if the letter guessed isn't in the same place but in the mystery word on another place
draw yellow dot
}else{ // if it doesn't compare to any of the entries
draw gray dot
}
4
  • 1
    I'm a little confused on the requirements for drawing red, yellow, and gray dots. For my own understanding, a red dot is drawn if all elements of both arrays are equal. A yellow dot is drawn if at least one element exists in both arrays. A gray dot is drawn if no elements are equal. Have I understood the problem statement correctly? (I'd also like to point out that if all elements of your string arrays are characters, then you can simply use a scalar String. A string's characters can be accessed in the same way that an array's elements can be accessed) Commented May 27, 2022 at 16:54
  • Its a changed version of like something of wordle I am controling what entry I am checking in the variable place so first time I am comparing the first entry of array 2 (what the user guessed) to all the variables of array (the one that has to be guessed) so if the first entry of array2 is the same character as the first entry of the word to be guessed it gets a green dot and if its somewhere else in the word to be guessed it gets yellow and if its not similar to any of the characters in the word to be guessed it gets gray Commented May 27, 2022 at 16:57
  • 1
    string array or char array? Commented May 27, 2022 at 17:40
  • I split the original String word into String[] array Commented May 27, 2022 at 17:44

2 Answers 2

1
import java.util.*;
import java.util.stream.*;
public class DotIt {
    public static void main(String[] args) {
        String mysteryWord = "apple";
        char[] mysteryChars = mysteryWord.toCharArray();
        String guessedWord = "plate";
        char[] guessedChars = guessedWord.toCharArray();
        List<Integer> mysteryCharList = mysteryWord.chars().boxed().collect(Collectors.toList());

        System.out.println("Mystery word: " + mysteryWord);
        System.out.println("Guessed word: " + guessedWord);

        for (int i = 0; i < mysteryChars.length; i++) {
            if (guessedChars[i] == mysteryChars[i]) {
                // letter of the guessed word is at same positioon in the mystery word
                System.out.println("letter " + (i + 1) + " ('" + guessedChars[i] + "') is red.");
            } else if (mysteryCharList.indexOf(Integer.valueOf(guessedChars[i])) != -1) {
                // the letter of the guessed word is in the mystery word at on another place
                System.out.println("letter " + (i + 1) + " ('" + guessedChars[i] + "') is yellow.");
            } else {
                // the letter of the guessed word isn't in the mystery word
                System.out.println("letter " + (i + 1) + " ('" + guessedChars[i] + "') is grey.");
            }
        }
    }
}
$ java DotIt.java
Mystery word: apple
Guessed word: plate
letter 1 ('p') is yellow.
letter 2 ('l') is yellow.
letter 3 ('a') is yellow.
letter 4 ('t') is grey.
letter 5 ('e') is red.
$ 
Sign up to request clarification or add additional context in comments.

2 Comments

The results of this are misleading in cases where the user's guessed word has repeated letters but the target word does not. A good example of this is when mysteryWord == "mop" and guessedWord == "mom". The second "m" in "mom" is marked as yellow, but that's misleading because there's only one "m" in the target and the first "m" in the user's guessed word was already marked as being valid.
I do not quite mind its not to be perfect its just a concept that I have to demonstrate
0

if input array is string array try this below code.

private void guess(String[] guessed, String[] original){
    Set<String> set = new HashSet<>();
    for(String o : original){
        set.add(o);
    }
    for (int i = 0; i < guessed.length; i++) {
        if(guessed[i].equals(original[i])){
            System.out.println("GREEN");
        }
        else if(set.contains(guessed[i])){
            System.out.println("YELLOW");
        }
        else{
            System.out.println("GREY");
        }
    }
}

my main method

public static void main(String[] args) {
    Solution solution = new Solution();
    solution.guess(new String[]{"a", "p", "p", "l", "e"}, new String[]{"p", "p", "a", "l", "x"});
}

3 Comments

I added java.util.Arays since I got an error message but when I try to run it now it gives an error code when its executing this void when I provide the guess and the original String[] array
Im using bluej 502 as required for this school project that uses java 11+ this works with that right or
i have modified ans. try again. if got error please post the error log here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.