Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Need help getting the user guess and display it to them in a "LettersGuessed" array and it should be a array of unique values. eg. if a user guesses "h" in the first guess, it should record the "h" only once even if it has been entered again.

What I have tried:

using System;

namespace AssignmentFinal
{
    class Program
    {
        static int numGuess;
        static int lettersLeft;
        public static void Main(string[] args)
        {
            string wordToGuess = GetSecretWord();
            char[] maskWord = GetHiddenLetters(wordToGuess, '-');
            lettersLeft = wordToGuess.Length;
            char userGuess;
            numGuess = (wordToGuess.Length) * 2;
            Console.WriteLine("Welcome to Guess The Word game!");
            while (numGuess > 0 && lettersLeft > 0)
            {
                Console.WriteLine("You have " + numGuess + " guesses!");
                DisplayBlanks(maskWord);
                Console.Write("Enter your guess: ");
                userGuess = char.Parse(Console.ReadLine());
                maskWord = CheckGuess(userGuess, wordToGuess, maskWord);
            }

            //if wrongGuess is 0 exit the program;
            if (numGuess == 0)
            {
                Console.WriteLine("You Lost, the secret word was: " + wordToGuess);
            }
            

        }
        static string GetSecretWord()
        {
            Random num = new Random();
            string[] words = { "hyperbaric", "temporal", "quantum", "radiation", "aardvark", "accident", "dracolich", "professional", "properties", "collections" };
            int wordIndex = num.Next(words.Length);
            string secretWord = words[wordIndex];
            return secretWord;
        }

        static char[] GetHiddenLetters(string word, char mask)
        {
            char[] hidden = new char[word.Length];
            for (int i = 0; i < word.Length; i++)
            {
                hidden[i] = mask;
            }
            return hidden;
        }

        static void DisplayBlanks(char[] characters)
        {
            Console.Write("Secret Word is: ");
            foreach (char letter in characters)
            {
                Console.Write(letter);
            }
            Console.WriteLine();
        }
            static char[] CheckGuess(char letterToCheck, string word, char[] characters)
            {
                //boolean to check for wrong guess
                bool wrongGuess = true;
                if ( numGuess > 0)
                {
                    for (int i = 0; i < word.Length; i++)
                    {
                    
                        if (word[i] == letterToCheck)
                        {
                            
                            characters[i] = word[i];
                            lettersLeft--;
                            //if match set wrong to false
                            wrongGuess = false;
                        }
                    }
                }
                //if wrong guess decrement
                if (wrongGuess)
                {
                    numGuess--;
                }
            return characters;


            }



    }
}
Posted
Updated 26-Oct-21 2:58am
v4
Comments
George Swan 25-Oct-21 5:46am    
I would suggest starting off with an empty List<char> and add characters to it as they are chosen. Use something like: if (usedCharacters.Contains('c')) to determine if the character 'c' has been chosen before. You can iterate over the chosen characters by a simple foreach loop. Something like: foreach (char c in usedCharacters) { Console.Write(c); }. The advantage of this approach over a List of all characters is that you eliminate searching through characters that could not possibly be duplicates.
PIEBALDconsult 25-Oct-21 10:26am    
Hashset.
Richard Deeming 26-Oct-21 9:24am    
Removing the content of your question after it has been answered is extremely rude, and will get you banned.

I have reverted your destructive edit.

1 solution

So create an array of characters and each time the user enters a letter, check to see if it is already used.
There are two ways to do this, but the easiest is probably to create an array of 26 bool values, one for each letter of the alphabet. Preset all values to false. When the use types a letter, convert it to an index into the array (a => 0, A = > 0, b => 1, B => 1, ...) and see if it's true or false.
If it's false, set it to true and process as normal.
If it's true, it's been tried, so remind the user and ask for a new letter.

Then when you want to show him the "used letters" it's just a case of looping through the array, and showing him only letters have a true value assocciated with that index.
 
Share this answer
 
Comments
Member 15405921 25-Oct-21 19:23pm    
I understand what ur saying but do u have a small sample code of what ur saying would help me a lot thanks
OriginalGriff 26-Oct-21 2:27am    
I could - but then you wouldn't learn anything: the purpose of giving you homework isn;t to waste your time, but to give you a chance to practice designing, coding, testing, and debugging your software - and it's a whole load easier to get started with that on a little project like this than a properly complex task.

Give it a try - it's not a hard task if you think about it before you rush into code.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Member 15405921 26-Oct-21 8:54am    
Yeah I finally did it, it works. It wasn’t that hard of a task to be fair. Thanks a lot btw
OriginalGriff 26-Oct-21 9:24am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900