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*_