0
\$\begingroup\$

Think about this like "Guess the word" game: W _ _ _ _

I have a word that i separate in index - letters like this:

public static string[] splitWord;
word = "WORLD";

string[] splitWord = new string[word.Length];
for(int i = 0; i < splitWord.Length; i++)
    {
        splitWord[i] = word[i].ToString();
    }

This way when i print the splitWord variable

for(int i = 0; i < splitWord.Length;i++)
    {
         print(splitWord[i] + " " + i);
    }

i get this;

 W 0
 o 1
 r 2
 l 3
 d 4

In my Ui i have letters, when i choose one i check if the letter is in the word, this part is done.

public static void SetWord (string letter)
{
    if (GameController.word.Contains (letter)) {
        word.text += letter; ------>setting the word NOT in order
    } else {
        Debug.Log("Letter is not in the word");
    }

}

What i´m having problems with is to place the letter in the correct place of the word. What i want to do is, based on this:

 W 0
 o 1
 r 2
 l 3
 d 4

Is to check "ok this letter is on the word, what index in the Word does this letter has?, place the letter in the correct place". That is the idea. but because im pretty new to code in general i m having problems with this. The idea is to convert that array of stings into a hole string and use Insert() to place the letter in the correct spot.

Any ideas?

Thanks in advance for the help.

\$\endgroup\$
1
  • \$\begingroup\$ @AlexandreVaillancourt i added that part of the code \$\endgroup\$ Feb 8, 2017 at 14:07

2 Answers 2

1
\$\begingroup\$

I believe the easy way is to keep 2 char arrays

  • First one contains the word
  • Second one contains the first letter and the _ char otherwise

What you display to the player is the second array of course.

When you check if the user guessed correctly, find at which index it was in the 1st array, and replace _ by that letter in the second array at the same index.

In order to do that, you should not use the Contains method in order to know if the letter is in the word, but rather parse the array yourself to find where the letter matches.

It could look like this

void CheckInputLetter(array1, array2, letter)
{
    int i = 0;
    while (array1[i] != '\0') //parse the whole array
    {
         if (array1[i] == letter)
             array2[i] = letter;
         i++;
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ actually that is not a bad idea, The problem is that i don´t know how to find at wich index is in the 1st array. My main problem is code wise cause like i said i m just strating with code \$\endgroup\$ Feb 8, 2017 at 14:13
  • \$\begingroup\$ I edited my answer to show you how to find that \$\endgroup\$ Feb 8, 2017 at 14:16
  • \$\begingroup\$ im getting this error: "IndexOutOfRangeException: Array index is out of range." in this line while (splitWord[i] != '\0') //parse the whole array........Any idea what could be happeniong?? \$\endgroup\$ Feb 8, 2017 at 15:47
  • \$\begingroup\$ Yes. My sample code is something you would use in C when array1 is a char*. You can just change the condition to something like while (i < array1.size()). As a habit my answers contain code which is rather pseudo-code as the goal is for you to understand what the code is supposed to do, and not just copy/paste it \$\endgroup\$ Feb 8, 2017 at 15:51
0
\$\begingroup\$

Alternatively, using LINQ gives you one liner:

string displayWord = string.Concat(word.Select(c => guessed.Contains(c) ? c : '_'));

where word and guessed are Lists of chars, first one is the correct word while the latter is list of all guesses(keeping them also gives you option to prevent player guessing a character twice).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .