-1

I can't permanently replace the array members. When I change the value of String Clue, the string being displayed only displays the current value of clue. I think the problem us on the initialization of char[]. I tried to put them in other parts of the code but it produces error. Beginner here! Hope you can help me. Thanks! :)

 private void clues(String clue)
    {
        int idx = numb[wordOn]+4;

        char[] GuessHide = Words[idx].ToUpper().ToCharArray();
        char[] GuessShow = Words[idx].ToUpper().ToCharArray();

            for (int a = 0; a < GuessHide.Length; a++)
            {
                if (GuessShow[a] != Convert.ToChar(clue.ToUpper()))
                    GuessHide[a] = '*';
                else
                    GuessHide[a] = Convert.ToChar(clue.ToUpper());
            }
            Guess(string.Join("", GuessHide));
    }
KuysChan
  • 1
  • 1
  • 1
  • 5
  • 5
    Strings are immutable in C#. use `ref String clue` if you want change context of `clue` – Orkhan Alikhanov Sep 21 '16 at 16:52
  • Read Immutable strings ! i.e. an object whose state cannot be modified after it is created. http://stackoverflow.com/questions/4274193/what-is-the-difference-between-a-mutable-and-immutable-string-in-c – Tushar Gupta Sep 21 '16 at 16:54
  • I am not sure what the "Guess" function does. does Guess repopulate the word list? IF not then your problem lies with the immutability of strings and you need to insert back into Words. Also, welcome to stack overflow :) – Matt Clark Sep 21 '16 at 16:56
  • @MattClark Guess Func prints the string. Ill check that immutability thingy :D Just learned new programming stuff again, thanks! :) – KuysChan Sep 21 '16 at 16:58
  • Also i think your Convert.ToChar call is a weird choice since clue is likely more than one character long. You would want to index clue probably right? like clue[a] @KuysChan – Matt Clark Sep 21 '16 at 17:01
  • @MattClark Ahm clues() actually receives string type data. But i already put a constraint in the class which calls the clues() that limits the string with only 1 character. Its just i made the conversion in this method. Not sure if im doing it right – KuysChan Sep 21 '16 at 17:06
  • @OrkhanAlikhanov Can you give me a syntax of it? That immutability is new to me. Thanks :) – KuysChan Sep 21 '16 at 17:07

2 Answers2

0

Edited - because you initalize GuessHide at each call of calls in your code and you do not store its current state you basically reset it each time. Still, you can make some small changes in your code like this:

 private static void clues(string clue, char[] GuessHide, char[] GuessShow)
    {
        for (int a = 0; a < GuessHide.Length; a++)
        {
            if (GuessShow[a] == Convert.ToChar(clue.ToUpper()))
            {
                GuessHide[a] = Convert.ToChar(clue.ToUpper());
            }
        }

        Console.WriteLine(string.Join("", GuessHide));
    }

Call it like this:

clues("p", GuessHide, GuessShow);
clues("a", GuessHide, GuessShow);

Initialise GuessShow and GuessHide in the outside code like this:

char[] GuessHide = new string('*', Words[idx].Length).ToCharArray();
char[] GuessShow = Words[idx].ToUpper().ToCharArray();
αNerd
  • 528
  • 1
  • 6
  • 11
  • Yes! That's what my output looks like. For example, the word being masked is "APPEND". When I call clues("P"), the output is "*PP***". But when I call it again with value "A", the output is "A*****". I want it to display "APP***". – KuysChan Sep 21 '16 at 17:10
  • @KuysChan `APP***` because *P* previously used as a clue? – Orkhan Alikhanov Sep 21 '16 at 17:15
  • @OrkhanAlikhanov Yes. Is there something I should add or edit in my code? – KuysChan Sep 21 '16 at 17:18
0
public class Program
{

    static string[] Words;
    static string[] HiddenWords;
    public static void Main()
    {
        Words = new string[] { "Apple", "Banana" };
        HiddenWords = new string[Words.Length];

        for (int i = 0; i < Words.Length; i++)
        {
            HiddenWords[i] = new string('*', Words[i].Length);
        }

        Guess('P', 0);
        Guess('a', 0);
        Guess('A', 1);
        Guess('N', 1);

        Console.ReadLine();
    }


    private static void Guess(char clue, int idx)
    {
        string originalWord = Words[idx];
        string upperedWord = Words[idx].ToUpper();
        char[] foundSoFar = HiddenWords[idx].ToCharArray();
        clue = char.ToUpper(clue);

        for (int i = 0; i < upperedWord.Length; i++)
        {
            if (upperedWord[i] == clue)
            {
                foundSoFar[i] = originalWord[i];
            }
        }

        HiddenWords[idx] = new string(foundSoFar);

        Console.WriteLine(HiddenWords[idx]);
    }
}
Orkhan Alikhanov
  • 9,122
  • 3
  • 39
  • 60