zhrgci
zhrgci

Reputation: 686

List[i].Replace in for-loop won't return string

Trying to make hangman (i'm still a newbie) and the program chooses a random word out of a textfile ==> word turned into arrays. And i have to put it in a label while having the textlabel modified to what's in the letterlist. Thing is: it doesn't show anything in the label and i can't seem to figure out why.

So the for-loop is the modifier and when it has modified every string in the list it should return the word with the right letter or "_".

At first i tried is by doing: letterlist[i] = Letter or letterlist[i] = "_", but would happen is if i typed in a right letter it would show only that letter.

For example: word = "pen". If i typed in "p", it resulted in "ppp".

letterlist = new List<string>();

char[] wordarray = woord.GetWordcharArray(); //word in charArrays
string newwordstring = new string(wordarray);

for (int i = 0; i < wordarray.Length; i++)
{
    letterlist.Add(" "); //adds empty strings in list with the length of the word 
}

/*
 * For-loop for every string in List to check and modify if it's correct or not 
 */
for (int i = 0; i < letterlist.Count; i++)
{
    if (letterlist[i].Contains(Letter) && newwordstring.Contains(Letter)) //right answer: letter[i] = Letter
    {
        letterlist[i].Replace(Letter, Letter);
    }
    else if (letterlist[i].Contains(" ") && newwordstring.Contains(Letter)) //right answer: letter[i] = "" 
    {
        letterlist[i].Replace(" ", Letter);
    }
    else if (letterlist[i].Contains("_") && newwordstring.Contains(Letter)) //right answer: letter[i] = "_"
    {
        letterlist[i].Replace("_", Letter);
    }
    else if (letterlist[i].Contains(" ") && !newwordstring.Contains(Letter)) //wrong answer: letter[i] = "" 
    {
        letterlist[i].Replace(" ", "_");
    }
    else if (letterlist[i].Contains("_") && !newwordstring.Contains(Letter)) //wrong answer: letter[i] = "_"
    {
        letterlist[i].Replace(" ", "_");
    }
}

/*
 * empty += every modified letterlist[i]-string
 */
string empty = "";
foreach (string letter in letterlist)
{
    empty += letter;
}
return empty;

New code but it only shows "___" ("_" as many times as the amount of letters as word has):

char[] wordarray = woord.GetWordcharArray(); //word in charArrays
string newwordstring = new string(wordarray); //actual word
string GuessedWord = new string('_', newwordstring.Length);//word that shows in form

bool GuessLetter(char letterguess)
{
   bool guessedright = false;

   StringBuilder builder = new StringBuilder(GuessedWord);

   for(int i = 0; i < GuessedWord.Length; i++)
   {
        if(char.ToLower(wordarray[i]) == Convert.ToChar(Letter))
        {
            builder[i] = wordarray[i];
            guessedright = true;
        }
    }

    GuessedWord = builder.ToString();
    return guessedright;
}

return GuessedWord;

Upvotes: 0

Views: 103

Answers (1)

Rafalon
Rafalon

Reputation: 4525

First of all, note that C# string are immutable, which means letterlist[i].Replace(" ", "_") does not replace spaces with underscores. It returns a new string in which spaces have been replaced with underscores.
Therefore, you should reassign this result:

letterlist[i] = letterlist[i].Replace(" ", "_");

Second, Replace(Letter, Letter) won't do much.

Third, in your first for loop, you set every item in letterlist to " ".
I don't understand then why you expect (in your second for loop) letterlist[i].Contains("_") to ever be true.

Finally, I'll leave here something you might find interesting (especially the use of StringBuilder):

class Hangman
{
    static void Main()
    {
        Hangman item = new Hangman();
        item.Init();

        Console.WriteLine(item.Guessed); // ____

        item.GuessLetter('t'); // true
        Console.WriteLine(item.Guessed); // T__t

        item.GuessLetter('a'); // false
        Console.WriteLine(item.Guessed); // T__t

        item.GuessLetter('e'); // true
        Console.WriteLine(item.Guessed); // Te_t
    }

    string Word {get;set;}
    string Guessed {get;set;}

    void Init()
    {
        Word = "Test";
        Guessed = new string('_',Word.Length);
    }

    bool GuessLetter(char letter)
    {
        bool guessed = false;

        // use a stringbuilder so you can change any character
        var sb = new StringBuilder(Guessed);

        // for each character of Word, we check if it is the one we claimed
        for(int i=0; i<Word.Length; i++)
        {
            // Let's put both characters to lower case so we can compare them right
            if(Char.ToLower(Word[i]) == Char.ToLower(letter)) // have we found it?
            {
                // Yeah! So we put it in the stringbuilder at the same place
                sb[i] = Word[i];
                guessed = true;
            }
        }

        // reassign the stringbuilder's representation to Guessed
        Guessed = sb.ToString();

        // tell if you guessed right
        return guessed;
    }
}

Upvotes: 2

Related Questions