Hangman style code not working as intended

Hey all, I’m working on a hangman style game, and I’m having a bad time getting my Game to work properly. I’ve been going over this code for days trying to get it to work properly, I just can’t seem to get everything working as I intend.

Here’s what I’m trying to do. I have a CSV file that has two columns. The first is the category text, so “Sports” or “Food” the second is the actual word the player will be guessing. So, “Soccer” or “Apple”.

I’ve gotten my script to read the CSV and am having no known issues with that. My goal is to replace every character with an “”. And then when the player selects a character, if that character is in the answer, it will replace the “” with the correct character. (As you would expect in a hangman game).

The issue I am having is regarding spaces (" ") and new lines seem to be throwing things off. Below you will find my (MESSY!) code, and a image of the result.

void SetCurrentQuestion()
{
    int RandomQuestionIndex = UnityEngine.Random.Range(0, CSVP.unansweredquestions.Count);
    CurrentQuestion = CSVP.unansweredquestions[RandomQuestionIndex];
    hinttext.text = CurrentQuestion.hint; 
    StringBuilder sb = new StringBuilder("");
    char[] myChars = {' '};
    string[] CurrentQuestionarray = CurrentQuestion.answer.Split(myChars[0]);
    if(CurrentQuestionarray.Length == 1)
    {
        for (int i = 0; i < CurrentQuestionarray.Length; i++)
        {
            for (int ii = 1; ii < CurrentQuestionarray*.Length; ii++)*

{
sb.Append(PLACEHOLDER);
}
sb.AppendLine();
}
Answertext.text = sb.ToString();
sb.Remove(sb.Length - 1, 1);
UserInput = sb.ToString();
CSVP.unansweredquestions.RemoveAt(RandomQuestionIndex);
}
if (CurrentQuestionarray.Length >1)
{
for (int i = 0; i < CurrentQuestionarray.Length; i++)
{
if (i == 0)
{
for (int ii = 0; ii < CurrentQuestionarray*.Length; ii++)*
{
sb.Append(PLACEHOLDER);
}
}
if (i > 0)
{
for (int ii = 0; ii < CurrentQuestionarray*.Length; ii++)*
{
sb.Append(PLACEHOLDER);
}
}
sb.AppendLine();
}
Answertext.text = sb.ToString();
UserInput = sb.ToString();
CSVP.unansweredquestions.RemoveAt(RandomQuestionIndex);
}
}
[And here is an image of what I am getting][1]
The answer is “Red Pepper Flakes” and as you can see, the first line works as intended, the second line loses the first character and it’s added on the end, and the third line is missing the first two characters and has 3 on the end.
For single word answers I am able to complete the puzzle and get the victory screen, that’s why I added the “if … length =1” statement, so that I could adjust the functions of two or more words without messing up what I already had working lol.
I’m happy to provide more info, and am hopeful that someone might be able to help!
Thanks in advance.
_*[1]: https://imgur.com/PLAjq6Q*_

In your StringBuilder object, sb, you want to create a ‘*’ character for each character in your CurrentQuestionarray. You want ’
’ new lines in sb inserted between the CurrentQuestionarray element boundaries.

You only need 1 if statement to do this - no need to test if CurrentQuestionarray has only 1 element, and no need to test within the parent nested loop if i == 0 or i > 0 because you’re doing exactly the same thing in each inner test. :

  // this first line is just a shortcut for illustration
  string[] cqArray = CurrentQuestionarray; 
  if( cqArray.Length  > 0  )  { // make sure you have a question!
        for ( int i = 0; i < cqArray.Length; i++ )  {
              for ( int j = 1; j < cqArray*.Length; j++ )  {* 

sb.Append(PLACEHOLDER);
}
if( i < cqLen - 1 ) sb.AppendLine();
}
Answertext.text = sb.ToString();
UserInput = sb.ToString();
CSVP.unansweredquestions.RemoveAt(RandomQuestionIndex);
}
That is really all you need. it will not place a ’
’ after the last word in your current question phrase - i.e. skip the newline after the last element in the current question array ;

As a general practice, if you use ‘i’ for outer loop don’t use ‘ii’ for inner loop indices. It’s easier to read if you use i and j - and accepted practice for nested loops: i, j, k, … and so on.
As to the other problems you are having, we need to see the rest of the code.