I still can't seem to think, syntactically, how to implement this.

The logic of the code is in my head, actually, a couple of ways to do it is clear but I'm having trouble in creating a solution for the core part of my game.

So far so good, I've made the menu, added code to deal with the player, added methods to pass letters etc but, this part just hits the nail on the head.

The place where it says //TODO is where my problem is. Which in simpleton terms: How to change the label on the windows form to actually contain the letter. So at the moment the label shows _ _ _ _ _ _ where the word might be Usmaan. If I send a 'Letter' a then the label should become _ _ _ a a _

I have a loop which can go through all of the indexes of the array which in essence is the actual word but just cut up in to chars so that I can check each index if it == to the letter. I've tried to soooo hard but I can't get my head around it. And I've tried using IndexOf and IndexOfAny but still can't nail the solution.

If you can help me, i'd really appreciate it.

Code:

{
            lblTesting.Text = gameLetter; // For Testing Purposes
            char[] wordToCharArray = word.ToCharArray(); // Convert the word to a char array
            lblTestingArray.Text = wordToCharArray[0].ToString(); // For Testing Purposes

            for (int i = 0; i <= word.Length -1; i++)
            {
                if (wordToCharArray[i].ToString() == gameLetter)
                {
                    //TODO
                }              
            }
        }

And these are my global variables so that they can be accessed in any of the methods:

string word;
        int length;
        Random random = new Random();
        string[] wordArray = { "BLUE", "RED", "GOLD", "BLACK", "GREEN" };
        char[] changingWordArray;
        string underscores;

Some of the variables like underscore and changingWordArray don't do anything *YET* - I was just trying to perform certain methods of fixing the problem but, no progress.

PLEASE HELP

string ChangeString(String word, String current, char test) {
    if (word.Length != current.Length) {
        throw new ArgumentException("Error: word and output must be same length");
    }

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < word.Length; i++) {
        if (word[i] == test) {
            sb.Append(word[i]);
        } else {
            sb.Append(current[i]);
        }
    }

    return sb.ToString();
}

or if you are willing to accept confusing code:

String ChangeString(String word, String current, char test) {
    if (word.Length != current.Length) {
        throw new ArgumentException("Error: word and output must be same length");
    }

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < word.Length; i++) {
        sb.Append(word[i] == test ? word[i] : current[i]);
    }

    return sb.ToString();
}
commented: You're too good for this world. +9

Better method is to use a regex. By using a negation on guessed letters, you get the same effect.

Something like:

//NOTE: In a real application, this would go in the code when the guess a letter, AFTER they enter a letter
            string word = "moo"; //For Test
            string guessedletters = "rmwe"; //For test

            string underscoredWord = System.Text.RegularExpressions.Regex.Replace(word, "[^" + guessedletters + "]", "_");

            Console.WriteLine(underscoredWord); //Replace with label

Momerath, How can I apply that code to my existing class?

Momerath, How can I apply that code to my existing class?

You have the word, you have the current "--------" and you have the letter that was guessed. We'll call these word, current and guess. So you do:

current = ChangeString(word, current, guess);

As for using Regex, it limits you to letters and some symbols. What if the word contained a '[' character? You probably won't run into that, but I like to code for contingencies.

Momerath, you haven't used regex much at all have you? Its a faster and more reliable method than .NETs stuff. the "[" in regex is the start of a character class (group of characters). The "^" means "Not". If you needed to match literals of those, you'd escape them, just like in C#. "\[" matches "[".

regex actually supports more characters that NET does. You can implement ASCII and unicode char codes directly in the regex.

See http://www.regular-expressions.info/reference.html

But this isn't the place for a debate over NET vs Regex.

I've used Regex a lot, you shouldn't make assumptions. What I'm referring to is what happens when the user inputs a regex 'reserved' character? In this case '[' wasn't the best choice as it won't do anything, but '\' will.

I'm not sure what you mean by it 'being faster than .NETs stuff'. Regex is part of .NET.

But that isn't important. Yours will work if he validates input. Mine works (albeit slower) without validating input. 1/2 a dozen of one, 6 of the other.

But this isn't the place for a debate over NET vs Regex.

So why did you start one?
You cannot compare .NET with regex.
Also have a look at sed, awk and grep.
Also have a look at the regex variants for Perl, Ruby, PHP, Python, C, Java.

So why did you start one?

I didn't. I defended my response to the OP. So here we go, now I'm defending THAT response.

You cannot compare .NET with regex.

Oh really? So I can't time a replace program, one that uses String.Replace() and another using Regex.Replace()?

Also have a look at sed, awk and grep.

I don't see the relevance. They use regex.

Also have a look at the regex variants for Perl, Ruby, PHP, Python, C, Java.

Yeah, I'm familiar with them. Your point?

I've used Regex a lot, you shouldn't make assumptions. What I'm referring to is what happens when the user inputs a regex 'reserved' character? In this case '[' wasn't the best choice as it won't do anything, but '\' will.

I'm not sure what you mean by it 'being faster than .NETs stuff'. Regex is part of .NET.

But that isn't important. Yours will work if he validates input. Mine works (albeit slower) without validating input. 1/2 a dozen of one, 6 of the other.

Sorry, didn't mean to come across as assuming. I see what you mean now, I though you were referring to the word itself. Of course he should sanitize the input if he's going to release the program. Even replacing "\" with "\\" when its read will do.

As for ''being faster than .NETs stuff'', I've read lots of posts around the Internet about how regex offers a huge advantage over built in String methods.

Yeah, I'm familiar with them. Your point?

Yeah, you are absolutely 100% right, I guess you even invented boiling water . . .

OK, you can stop with the insults. Your posts have no relevance to the OPs question. It has been answered. If you have an issue with me, take it to the PMs.

I request this thread be marked as solved. The OPs question has been given two relevant answers.

>> I request this thread be marked as solved. The OPs question has been given two relevant answers.

I'll let the OP decide that. Now time to start talking back ontopic and end this potential flamewar or I'll start deleting the replies that I think are of no use to the OP.

kthxbye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.