Hey.
I'm making a Hangman and I have this code to retrieve a random word from a txt file:
private void ord()
{
Random random = new Random();
StreamReader read = new StreamReader("sweord.txt");
ArrayList lines = new ArrayList();
string line;
while ((line = read.ReadLine()) != null)
lines.Add(line);
read.Close();
int randomLine = random.Next(0, lines.Count);
string word;
word = lines[randomLine].ToString();
game = word;
for (int i = 0; i < game.Length; i++)
{
maskedword += "_ ";
}
/*(strings)*/ mword = maskedword; //saving maskedword for further use as I have to reset it
label5.Text = maskedword;
maskedword = "";
}
Maskedword is as you can see put into a label which shows "_ _ _ _ _". And this is where I'm stuck.
To check if the user has made a correct guess or not I have:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
guess = e.KeyChar.ToString().ToUpper();
for (int i = 0; i < game.Length; i++)
{
if (game[i] == e.KeyChar) //game is the randomed word
{
}
...
Now. Let's say the word (game) is "Water" and the user guessed "t". I have to put the "t" where it belongs so it should look like this:
"_ _ t _ _"
I've been trying to do this in a few different ways but none works. The problem isn't in extracting the guessed letter but to put it into a string (or something else) in its correct position.
if (game[i] == e.KeyChar)
{
mword.Substring(i, 1) = game[i];
label5.Text = mword;
...
}
One example above. I'm trying to put the letter into mword and then write it into the label but I get the error messages: "The lefthand side of an assignment must be a variable, property or indexer" and "Cannot implicitly convert type char to string."
I've tried some stuff with replace and char arrays without success.
Any thoughts?