• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Iterate through list of Words While Calling Function – Java

April 11, 2020 by

Questions › Iterate through list of Words While Calling Function – Java
0
Vote Up
Vote Down
Garmaine asked 4 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;
    }    
Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: function, java, linked-list, loops

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023