Garmaine Staff asked 2 years ago

I am writing a program that runs an interface of a game. The player is given a list of words of int n size to guess from, where each word is guaranteed to only contain each letter once. The game gives the player back the number of same and different values depending on the guess the player makes. Where same is the number of characters that are the right letter in the right position, and different is the number of characters that are the right letter but in the wrong position.

For example, if the player guesses the word "words" and the correct word is "wreck" the game would return the values of same as 1 (w is the correct letter in the right position), and the value of different as 1 (r is the correct letter but in the wrong position).

I have a function eliminate that properly eliminates the words in a the list based on the same and different values, but I am trying to further have my program iterate through the list of words assuming that one word is the a word, while the other words are not the secret word to see if I can get a list size of just one guess by calculating the same and different values for each non-secret word. How would I properly implement this?

Here I am iterating over my list of words until it is found that I have list size of 1:

String guess = new String();
for (String word : words)
{
    len = find(word, n, words);
    if (len.size() == 1)
    {
         guess = word;
         return guess;
    }
}

Here I am trying to iterate through each word assuming it may be the secret word while the others in the words list are not :

public List find(String word, int n, List<String> words)
    {
        LinkedList<Integer> s = new LinkedList<Integer>();
        LinkedList<Integer> d = new LinkedList<Integer>();
        LinkedList<String> newList = new LinkedList<String>(lengthList);

        int same = 0;
        int different = 0;
        String secret = word;

        for (String w : words)
        {
            if (w == guess)
            {
                newList.remove(word);
            }
        }
        for (int i = 0; i < newList.size(); i++)
        {
            String guess = newList.get(i);
            for (int j = 0; j < word.length(); j++)
            {
                char ch = guess.charAt(j);
                if (guess.charAt(j) == secret.charAt(j))
                {
                    same ++;
                }
                else if (secret.contains(String.valueOf(ch)))
                {
                    different ++;
                }
            }
            s.add(same);
            d.add(different);
            LinkedList<String> newGuess = new LinkedList<String>();
            newGuess.add(word);
            words = eliminate(words, n, newGuess, s, d);
        }
        return words;
    }