I am doing a simple hangman game. I have gotten everything going except for the part where the user enters a correct char and the corresponding char in the solution word should be replaced with the former.
First, here is my code:
private void checkIfLetterIsInWord(char letter)
{
if (currentWord != string.Empty)
{
if (this.currentWord.Contains(letter))
{
List<int> indices = new List<int>();
for (int x = 0; x < currentWord.Length; x++)
{
if (currentWord[x] == letter)
{
indices.Add(x);
}
}
this.enteredRightLetter(letter, indices);
}
else
{
this.enteredWrongLetter();
}
}
}
private void enteredRightLetter(char letter, List<int> indices)
{
foreach (int i in indices)
{
string temp = lblWord.Text;
temp[i] = letter;
lblWord.Text = temp;
}
}
So my problem is the line
temp[i] = letter;
I get an error here that says "Property or indexer cannot be assigned to — it is read only". I have already googled and found out that strings cannot be modified during runtime. But I have no idea how to substitute the Label which contains the guesses. The label is in the format
_ _ _ _ _ _ _ //single char + space
Can anybody give me a hint how I can replace the chars in the solution word with the guessed chars?