2 Replies - 3395 Views - Last Post: 31 October 2011 - 07:46 AM Rate Topic: -----

#1 iHateCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-October 11

c# hangman console app

Posted 31 October 2011 - 07:02 AM

I've been set the task of creating a hangman game console app. Which have done. There is nothing wrong with the code but the game doesnt work as it should.

I need it to randomly generate a word from the 'dictionary'. and then the user enter a letter or a guess. I have tried and failed. Here is my code:

I know there is a randGen line of code I could use but i am unsure how. Any help would be much appeciated

namespace Hangman2
{
    class Hangman
    {
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Red;
            string Name;
            string sInput;

            //declaring the word array
            string[] wordBank = new string[5];
            wordBank[0] = "Dog";
            wordBank[1] = "Computer";
            wordBank[2] = "Tree";
            wordBank[3] = "Balls";
            wordBank[4] = "House";


            //displaying the game layout 


            Console.WriteLine("                         ************************    ");
            Console.WriteLine("                         *       HANGMAN!       *    ");
            Console.WriteLine("                         ************************    ");
          

            Console.WriteLine(" ________________ ");
            Console.WriteLine(" | | ");
            Console.WriteLine(" | ( ) ");
            Console.WriteLine(" | /|\\ ");
            Console.WriteLine(" | | ");
            Console.WriteLine(" | /|\\ ");
            Console.WriteLine(" | ");
            Console.WriteLine(" | ");
            Console.WriteLine(" |_______ ");

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

            Console.Clear();

            //displaying user greeting message
            Console.WriteLine(Name + ", Welcome");

            Console.WriteLine("                         ************************    ");
            Console.WriteLine("                         *       HANGMAN!       *    ");
            Console.WriteLine("                         ************************    ");

            Console.WriteLine();

            //draw the hang structure
            Console.WriteLine(" ______________ ");
            Console.WriteLine(" |/ ");
            Console.WriteLine(" | ");
            Console.WriteLine(" | ");
            Console.WriteLine(" | ");
            Console.WriteLine(" | ");
            Console.WriteLine(" | ");
            Console.WriteLine(" |_______ ");


            //declare the word list display/output condition
            if (wordBank[0].Length == 3)
            {
                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 == 5)
            {
                Console.WriteLine(" The word is : _ _ _ _ _ ");
            }
            else if (wordBank[4].Length == 5)
            {
                Console.WriteLine(" The word is : _ _ _ _ _ ");
            }
            
            Console.WriteLine();
            Console.WriteLine(" ***Enter a new letter***");           
            sInput = Convert.ToString(Console.ReadLine());
            Console.ReadKey();
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: c# hangman console app

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,649
  • Joined: 02-June 10

Re: c# hangman console app

Posted 31 October 2011 - 07:13 AM

First - Having that if...else construct just hurts.
I'm sure you've learned loops. You should build your "The word is _ _" line, just do it like this.

You say you tried and failed at doing the random selection. But I see no effort of that. How can we *help* you with your random selection if you don't supply us the code you are using and the error you are getting?

MSDN Page for the random class:
http://msdn.microsof...tem.random.aspx
Was This Post Helpful? 0
  • +
  • -

#3 iHateCode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-October 11

Re: c# hangman console app

Posted 31 October 2011 - 07:46 AM

No i said i have tried and failed at making a successful hangman game :)
And i am unsure how to use random selection, thats why i have not used it. hence no code. I just know it exists.

Thanks for the link, I'll have a read
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1