This is for a hangman program it works fine if the word I am guessing only contains each letter once but if it contains one or more of the letters twice when I click on the letter again nothing happens.
private void charGuess(char letter )
{
if (!myWords[randomNum].guess_word.Contains(letter))
{
totalWrongGuesses(letter);
}
else
{
int tempIndex = myWords[randomNum].guess_word.IndexOf(letter);
astericksBox.Text = astericksBox.Text
.Remove(tempIndex,1)
.Insert(tempIndex,letter
.ToString());
}
if (!astericksBox.Text.Contains("*"))
{
MessageBox.Show("You've Won!!");
}
}
private int randomNumGenerator()
{
int rndNum; // a variable to temporarily store the random number generated
Random randomNum = new Random(); // creates a new Random object randomNum
return rndNum = randomNum.Next(0,19); // returns a randomNum vlue between 0 and 19
}
private void button15_Click(object sender, EventArgs e)
{
charGuess('a');
}
Update: I found the solution for my problem and posted it as an answer below.
replace the body of your else with
var indexes = AllIndexesOf(myWords[randomNum].guess_word,letter);
foreach(int i in indexes)
astericksBox.Text = astericksBox.Text.Remove(i,1).Insert(i,letter.ToString());
and create this function (which i got from Finding ALL positions of a substring in a large string in C# )
public static List<int> AllIndexesOf(this string str, string value)
if (String.IsNullOrEmpty(value))
throw new ArgumentException("the string to find may not be empty", "value");
List<int> indexes = new List<int>();
for (int index = 0; ; index += value.Length)
{
index = str.IndexOf(value, index);
if (index == -1)
return indexes;
indexes.Add(index);
}
}
Replace this:
int tempIndex = myWords[randomNum].guess_word.IndexOf(letter);
astericksBox.Text = astericksBox.Text.Remove(tempIndex,1).Insert(tempIndex,letter.ToString());
With this:
int tempIndex = 0;
do
{
tempIndex = myWords[randomNum].guess_word.IndexOf(letter, tempIndex);
astericksBox.Text = astericksBox.Text.Remove(tempIndex,1).Insert(tempIndex,letter.ToString());
tempIndex++;
}
while (tempIndex > 0);
And for efficiency/performance I might do this:
int tempIndex = 0;
var mask = astericksBox.Text.ToCharArray();
do
{
tempIndex = myWords[randomNum].guess_word.IndexOf(letter, tempIndex);
mask[tempIndex] = letter;
tempIndex++;
}
while (tempIndex > 0);
astericksBox.Text = new string(mask);
That code also reads a lot easier.
I changed the else statement in charGuess to the code below and it now works as expected.
else
{
int tempIndex = myWords[randomNum].guess_word.IndexOf(letter);
String tempWord = myWords[randomNum].guess_word;
astericksBox.Text = astericksBox.Text.Remove(tempIndex,1).Insert(tempIndex,letter.ToString());
tempWord = tempWord.Remove(tempIndex, 1).Insert(tempIndex, "£");
myWords[randomNum].guess_word = tempWord;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.