It is still a little scattered, but when debugging I can pick a letter, but it does not compare it to the wordArray/wordBank. So I am not able to "pick a letter" and it replace the * with the correct letter picked. I can't seem to figure out the code to do so.
Can anyone help?!?!?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;


namespace Hangman
{
    class Program
    {
        static void Main(string[] args)
        {
            string Name;
            string gameWord;
            bool[] boolLetters;
            char[] wordArray;
            char[] letters;
            char PlayerGuess;

            // set geussable letters
            letters = new char[26] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

            // initializing all letters to false
            boolLetters = new bool[26];
            for (int i = 0; i < boolLetters.Length; i++)
            {
                boolLetters[i] = false;
            }
            // set word to null
            gameWord = "";

            // set wordArray to gameWord
            wordArray = new char[1] { ' ' };

            //declaring the word array
            string[] wordBank = new string[9];

            wordBank[0] = "Fishing";
            wordBank[1] = "Computer";
            wordBank[2] = "Tree";
            wordBank[3] = "Location";
            wordBank[4] = "Technician";
            wordBank[5] = "Work";
            wordBank[6] = "Butterfly";
            wordBank[7] = "Toddler";
            wordBank[8] = "Flowers";

            //displaying the game layout
            Console.WriteLine("      ~*~*~*~*~*~*~*~*~*~*~*~    ");
            Console.WriteLine("     ~*~Welcome to Hangman!~*~   ");
            Console.WriteLine("      ~*~*~*~*~*~*~*~*~*~*~*~    ");

            //Ask user to Enter Name
            Console.Write("Please enter your Name: ");
            Name = Convert.ToString(Console.ReadLine());

            //display user greeting message
            Console.WriteLine(Name + ", Welcome, to Hangman");
            Console.WriteLine("       ~*~*~*~*~*~*~*~*~*~*~*~    ");
            Console.WriteLine("         ~*~*~Hangman!~*~*~    ");
            Console.WriteLine("       ~*~*~*~*~*~*~*~*~*~*~*~   ");
            Console.WriteLine();

            // creates a random number to choose a word from word bank
            Random random = new Random();
            int randomNumber = random.Next(0, 9);

            //selects the word from the wordBank using randomNumber
            gameWord = wordBank[randomNumber];

            // wordArray is set to wordArray
            wordArray = new char[gameWord.Length];
            wordArray = gameWord.ToCharArray();

            //declare the word list display/output condition
            if (wordBank[0].Length == 7)
            {
                Console.WriteLine(" The word is : * * * * * * * ");
            }
            else if (wordBank[1].Length == 8)
            {
                Console.WriteLine(" The word is : * * * * * * * *  ");
            }
            else if (wordBank[2].Length == 4)
            {
                Console.WriteLine(" The word is : * * * * ");
            }
            else if (wordBank[3].Length == 8)
            {
                Console.WriteLine(" The word is : * * * * * * * *");
            }
            else if (wordBank[4].Length == 10)
            {
                Console.WriteLine(" The word is : * * * * * * * * * * ");
            }
            else if (wordBank[5].Length == 4)
            {
                Console.WriteLine(" The word is : * * * * ");
            }
            else if (wordBank[6].Length == 9)
            {
                Console.WriteLine(" The word is : * * * * * * * * * ");
            }
            else if (wordBank[7].Length == 7)
            {
                Console.WriteLine(" The word is : * * * * * * * ");
            }
            else if (wordBank[8].Length == 7)
            {
                Console.WriteLine(" The word is : * * * * * * * ");
            }

            string[] geuss = new string[gameWord.Length];

            for (int i = 0; i < gameWord.Length; i++) ;

            bool repeat = false;
            do
            {
                Console.Write("Please enter a new letter: ");
                string guess = Console.ReadLine();

                if (guess.Length > 1)
                {
                    Console.WriteLine("Only one letter is allowed");
                    repeat = true;
                 }
                else
                {
                    PlayerGuess = Convert.ToChar(guess);
                    if (char.IsLetter(PlayerGuess) == true)
                    {
                        PlayerGuess = char.ToUpper((char)PlayerGuess);
                        repeat = false;
                    }
                else
                    {
                        Console.WriteLine("You can only enter letter values, please re-enter choice.");
                        repeat = true;
                    }
                }
            } while (repeat);
        }
    }
}

Recommended Answers

All 5 Replies

Hi,

line 115 of your code above has a typo:

string[] geuss = new string[gameWord.Length];

the word 'geuss'

Maybe this will help you out.

Regards..,

Before we talk about how to guess letters and replace the *s, there are a number of problems in the code that should be addressed first. I might seem to get a little pedantic here, but clearing up these issues will make the code shorter and easier to understand, as well as clear up a few misunderstandings about how some of these things work.

From the top:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

You're only actually using the System namespace; remove the rest of the lines so it's clear which parts of the library you're actually using:

using System;

At the start of Main:

static void Main(string[] args)
{
    string Name;
    string gameWord;
    bool[] boolLetters;
    char[] wordArray;
    char[] letters;
    char PlayerGuess;

This advance declaration is required in C, but not C#. In most cases, I prefer to see variable declarations at the same point they're first used, like this:

string Name = Convert.ToString(Console.ReadLine());

That way I don't have to go back to the top of Main to remind myself that Name is a string.

(Note: I'm editing my copy of the code as I go here, so some of the following recommendations will make more sense if you're following along with the edits)

This is a little verbose:

char[] letters = new char[26] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

Another way to get a character array is to get it from a string:

char[] letters = "abcdefghijklmnopqrstuvwxyz".ToCharArray();

That's easier to read, but let's go one step further--strings can work like character arrays in some ways, so let's just keep it as a string:

string letters = "abcdefghijklmnopqrstuvwxyz";

And finally, this string never changes, so it can be a constant defined outside of Main:

const string letters = "abcdefghijklmnopqrstuvwxyz";

static void Main(string[] args)
{

I notice that letters isn't actually used anywhere. It looks like you want to use it here:

if (char.IsLetter(PlayerGuess) == true)

Char.IsLetter returns whether the character in question is classified as a Unicode character, which convers a lot more than just A-Z, so I think what you really want is something like this:

if (letters.Contains(PlayerGuess) == true)

And as long as we're discussing that line, the == true part isn't necessary--if statements operate on the result of their expression:

if (letters.Contains(PlayerGuess))

That won't quite work, though, because String.Contains looks for strings, not characters. Here's an alternative:

if (letters.IndexOf(PlayerGuess) != -1)

That's a little ugly; we'll revisit this one later.

Back up to the top of Main:

bool[] boolLetters = new bool[26];
for (int i = 0; i < boolLetters.Length; i++)
{
    boolLetters[i] = false;
}

The default value for Boolean variables is false, so the loop isn't necessary; all you need is this:

bool[] boolLetters = new bool[26];

Hm, we're not actually using boolLetters anywhere else, so I'm deleting it for now.

In case this isn't just a typing mistake:

// set word to null
string gameWord = "";

The empty string ("") is not the same thing as null. A null string has no value, while the empty string is considered to be a string with length 0. Remember that.

Also, you don't actually use gameWord until you've assigned it some other value, so this line isn't really necessary. You can live with just this, a little further down:

string gameWord = wordBank[randomNumber];

On to wordArray:

char[] wordArray = new char[1] { ' ' };

You're assigning a value to this array later, but not actually using it anywhere. I'm deleting this one as well.

A comment before I do though:

string gameWord = "";

// set wordArray to gameWord
char[] wordArray = new char[1] { ' ' };

This isn't doing what you say it is. gameWord is an empty string, but wordArray represents a string with one character, a space. You'd fix that like this:

char[] wordArray = new char[0];

Now, the word bank:

string[] wordBank = new string[9];

wordBank[0] = "Fishing";
wordBank[1] = "Computer";
wordBank[2] = "Tree";
wordBank[3] = "Location";
wordBank[4] = "Technician";
wordBank[5] = "Work";
wordBank[6] = "Butterfly";
wordBank[7] = "Toddler";
wordBank[8] = "Flowers";

There's a shorter syntax for array initialization:

string[] wordBank = {
    "Fishing", "Computer", "Tree",
    "Location", "Technician", "Work",
    "Butterfly", "Toddler", "Flowers"
};

Also, this array never changes, so it can get pulled out as well. Note that you can't use const:

static readonly string[] wordBank = {
    "Fishing", "Computer", "Tree",
    "Location", "Technician", "Work",
    "Butterfly", "Toddler", "Flowers"
};

static void Main(string[] args)
{

On variable names: Instead of Name, I'd use name. This is an fairly common convention.

Punctuation police...

Console.WriteLine(name + ", Welcome, to Hangman");

No comma necessary after "Welcome":

Console.WriteLine(name + ", Welcome to Hangman");

Now for random numbers:

int randomNumber = random.Next(0, 9);

If you need a zero-based random index, there's a simpler overload of Random.Next:

int randomNumber = random.Next(9);

Also, if wordBank changes, you won't want to have to keep editing this line to adjust for the new length, so just use it directly:

int randomNumber = random.Next(wordBank.Length);

So now we use it:

string gameWord = wordBank[randomNumber];

But this is the only place randomNumber gets used, so we can combine these two lines:

string gameWord = wordBank[random.Next(wordBank.Length)];

Now the giant if/else if block:

if (wordBank[0].Length == 7)
{
    Console.WriteLine(" The word is : * * * * * * * ");
}
else if (wordBank[1].Length == 8)
{
    ...

This is confused. What's going to happen is that wordBank[0].Length will always be 7, so it will always print " The word is : * * * * * * * " Not what you really want.

A good start is to use the actual word, which you've already put into gameWord. No need to spin through the whole word bank, just use the one you picked:

Console.WriteLine(" The word is: ", gameWord);

Of course, this will show you the entire word, and not hide the un-guessed letters. Aha! now we're at your original question.

A few other things first:

string[] geuss = new string[gameWord.Length];

You don't use geuss anywhere else; I'm deleting it.

for (int i = 0; i < gameWord.Length; i++) ;

A loop that does nothing? Deleted.

string guess = Console.ReadLine();

if (guess.Length > 1)
{

If you only want one character, you can use Console.ReadKey():

char guess = Console.ReadKey().KeyChar;

And since we have a character now, we don't need this:

char PlayerGuess = Convert.ToChar(guess);

So just remove that line and replace PlayerGuess with guess.

Now we can get rid of the whole if (guess.Length > 1) block, and we're down to:

if (letters.IndexOf(guess) != -1)
{
    guess = Char.ToUpper(guess);
    repeat = false;
}
else
{
    Console.WriteLine("You can only enter letter values, please re-enter choice.");
    repeat = true;
}

It looks like the loop this if/else block is in is just so you can get an actual letter out of the user, and guess won't be available outside of the loop because it's declared inside. Let's put it outside so we can use it later:

char guess;
do
{
    Console.Write("Please enter a new letter: ");
    guess = Console.ReadKey().KeyChar;

Well that was a lot of stuff--I think it's useful, though.

...and I need to get back to work :( I'll get back with a response to your actual question later on.

Oh, also. Here's my copy I've been editing so far:

using System;

    namespace Hangman
    {
        class Program
        {
            const string letters = "abcdefghijklmnopqrstuvwxyz";

            static readonly string[] wordBank = {
                "Fishing", "Computer", "Tree",
                "Location", "Technician", "Work",
                "Butterfly", "Toddler", "Flowers"
            };

            static void Main(string[] args)
            {
                Console.WriteLine("      ~*~*~*~*~*~*~*~*~*~*~*~    ");
                Console.WriteLine("     ~*~Welcome to Hangman!~*~   ");
                Console.WriteLine("      ~*~*~*~*~*~*~*~*~*~*~*~    ");

                Console.Write("Please enter your Name: ");
                string name = Convert.ToString(Console.ReadLine());

                Console.WriteLine(name + ", Welcome to Hangman");
                Console.WriteLine("       ~*~*~*~*~*~*~*~*~*~*~*~    ");
                Console.WriteLine("         ~*~*~Hangman!~*~*~    ");
                Console.WriteLine("       ~*~*~*~*~*~*~*~*~*~*~*~   ");
                Console.WriteLine();

                // creates a random number to choose a word from word bank
                Random random = new Random();

                //selects the word from the wordBank using randomNumber
                string gameWord = wordBank[random.Next(wordBank.Length)];

                // TODO: Hide un-guessed letters.
                Console.WriteLine(" The word is: ", gameWord);

                bool repeat = false;
                char guess;
                do
                {
                    Console.Write("Please enter a new letter: ");
                    guess = Console.ReadKey().KeyChar;

                    if (letters.IndexOf(guess) != -1)
                    {
                        guess = Char.ToUpper(guess);
                        repeat = false;
                    }
                    else
                    {
                        Console.WriteLine("You can only enter letter values, please re-enter choice.");
                        repeat = true;
                    }

                } while (repeat);

                // TODO: Finish the program; see if the letter is in the word and go back for more guesses.
            }
        }
    }

Thank you so much, gusano79. I really appricate the walkthrough, it has helped me out tremendously. I not only can read the code, but I have a better understanding of it all. Cant wait to see/hear what your next post consisting of it, but again I really appricate it. I have to leave for a second shift in less than thiry minutes, but I will be back later tonight.

Hi again... I got super busy for a while there and this got left behind; sorry. Would it still be helpful to continue?

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.