3 Replies - 1075 Views - Last Post: 07 April 2014 - 03:03 PM Rate Topic: -----

#1 XxArcanexX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 14

Help with my hangman game?

Posted 06 April 2014 - 09:04 PM

So I am pretty much new to c# and I am quite a noob at it so any help with my code would be appreciated xD. Anyway I mostly need help with 1. Adding more words and have it randomly pick the word. 2.actually like slimming down the letters and put it together, like if they chose a and they choose a again they can't because its already chosen. 3. maybe some help with my organization lol. And 4. I was kinda confused on how to make it so it equals the user input with the letter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace HangmanGame
{
    class Program 
    {
        
        static void Main()

        {
            string[] letters = {"h, a, n, g, m, a, n"};
   
            string lettera = "a";
            string letterh = "h";
            string lettern = "n";
            string letterg = "g";
            string letterm = "m";
            //string userinput = Console.ReadLine();
            string start ="start";
            string[] word = { "hangman" };
            Console.WriteLine("Welcome to the Hangman Game!");
            Console.WriteLine("Press '{0}' to begin or enter to quit", start);
            if (start == Console.ReadLine())
            {
                Console.WriteLine("Lets begin!");
            }
            else
            {
            
                
                Console.WriteLine("Exiting game.");
                Console.Beep();
                Console.Beep();
                Console.Beep();
                Console.Beep();
                Console.Beep();
                return;
                            
            }
            Console.WriteLine("Your word has 5 letters in it.");
            Console.WriteLine("What letter would you like?");
            
             if (lettera == Console.ReadLine() || lettera != Console.ReadLine() ||letterh == Console.ReadLine() || letterh != Console.ReadLine() || lettern == Console.ReadLine() || lettern != Console.ReadLine() || letterg == Console.ReadLine() || letterg != Console.ReadLine() || letterm == Console.ReadLine() || letterm != Console.ReadLine())
             {
                 Console.WriteLine("You guessed the right letter!");
                 
                 
             }
             else
             {
                 Console.Beep();
                 Console.Beep();                 
                 Console.WriteLine("You guessed the wrong letter!");
             }
             Console.WriteLine("Next Letter!");
             if (lettera == Console.ReadLine() || lettera != Console.ReadLine() || letterh == Console.ReadLine() || letterh != Console.ReadLine() || lettern == Console.ReadLine() || lettern != Console.ReadLine() || letterg == Console.ReadLine() || letterg != Console.ReadLine() || letterm == Console.ReadLine() || letterm != Console.ReadLine())
             {
                 Console.WriteLine("You guessed the right letter!");


             }
             else
             {
                 Console.Beep();
                 Console.Beep();
                 Console.WriteLine("You guessed the wrong letter!");
             }
             Console.WriteLine("Next Letter!");
             if (lettera == Console.ReadLine() || lettera != Console.ReadLine() || letterh == Console.ReadLine() || letterh != Console.ReadLine() || lettern == Console.ReadLine() || lettern != Console.ReadLine() || letterg == Console.ReadLine() || letterg != Console.ReadLine() || letterm == Console.ReadLine() || letterm != Console.ReadLine())
             {
                 Console.WriteLine("You guessed the right letter!");


             }
             else
             {
                 Console.Beep();
                 Console.Beep();
                 Console.WriteLine("You guessed the wrong letter!");
             }

             Console.WriteLine("Next Letter!");
             if (lettera == Console.ReadLine() || lettera != Console.ReadLine() || letterh == Console.ReadLine() || letterh != Console.ReadLine() || lettern == Console.ReadLine() || lettern != Console.ReadLine() || letterg == Console.ReadLine() || letterg != Console.ReadLine() || letterm == Console.ReadLine() || letterm != Console.ReadLine())
             {
                 Console.WriteLine("You guessed the right letter!");


             }
             else
             {
                 Console.Beep();
                 Console.Beep();
                 Console.WriteLine("You guessed the wrong letter!");
             }

             Console.WriteLine("Next Letter!");
             if (lettera == Console.ReadLine() || lettera != Console.ReadLine() || letterh == Console.ReadLine() || letterh != Console.ReadLine() || lettern == Console.ReadLine() || lettern != Console.ReadLine() || letterg == Console.ReadLine() || letterg != Console.ReadLine() || letterm == Console.ReadLine() || letterm != Console.ReadLine())
             {
                 Console.WriteLine("You guessed the right letter!");


             }
             else
             {
                 Console.Beep();
                 Console.Beep();
                 Console.WriteLine("You guessed the wrong letter!");
             }
             Console.WriteLine("Next Letter!");
             if (lettera == Console.ReadLine() || lettera != Console.ReadLine() || letterh == Console.ReadLine() || letterh != Console.ReadLine() || lettern == Console.ReadLine() || lettern != Console.ReadLine() || letterg == Console.ReadLine() || letterg != Console.ReadLine() || letterm == Console.ReadLine() || letterm != Console.ReadLine())
             {
                 Console.WriteLine("You guessed the right letter!");


             }
             else
             {
                 Console.Beep();
                 Console.Beep();
                 Console.WriteLine("You guessed the wrong letter!");
             }
             Console.WriteLine("Next Letter!");
             if (lettera == Console.ReadLine() || lettera != Console.ReadLine() || letterh == Console.ReadLine() || letterh != Console.ReadLine() || lettern == Console.ReadLine() || lettern != Console.ReadLine() || letterg == Console.ReadLine() || letterg != Console.ReadLine() || letterm == Console.ReadLine() || letterm != Console.ReadLine())
             {
                 Console.WriteLine("You guessed the right letter!");


             }
             else
             {
                 Console.Beep();
                 Console.Beep();
                 Console.WriteLine("You guessed the wrong letter!");
             }
             Console.WriteLine("If you guessed hangman, You Win!");
             Console.Beep();
             Console.Beep();
             Console.Beep();
             Console.Beep();
             Console.Beep();
             Console.Beep();
             Console.Beep();
             Console.Beep();
             Console.Beep();
             Console.ReadKey(true);
           


            

            
        }
    }
}


Not the best but its my first game in c# so I got an excuse xD. If you can show me some resources from the microsoft site that would be nice as I have been learning quite well from it :D, I know what I want but I don't know the name of the commands as I pretty much just jumped straight into c# without looking at any books on how to start or anything except the microsoft resource site xD

Is This A Good Question/Topic? 0
  • +

Replies To: Help with my hangman game?

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Help with my hangman game?

Posted 06 April 2014 - 11:10 PM

Look at String.Contains, much better than all those if statements. Speaking of which, you should create a method that checks if the input is good and put it in a loop (while) so you don't have to repeat so much code. Remember DRY: Don't Repeat Yourself. Your code is very WET (We Enjoy Typing).

Learn Arrays and the Random class.
Was This Post Helpful? 0
  • +
  • -

#3 Robbss   User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 57
  • Joined: 26-December 10

Re: Help with my hangman game?

Posted 07 April 2014 - 06:04 AM

I took a good look at your code and I tried set it up in a simpler way for you, please read through and try to understand the code, I've documented it pretty well for you and it should be pretty easy to understand what's going on, take a look at it and try to improve your own code without copying mine, let me know if there's any of it you don't understand, I'll be here to help you along the way.

static void Main()
        {
            // We start out by creating a list of words (Array)
            string[] words = { "hangman", "dinosaur", "helloworld", "randomword" };

            // Now we want to randomly select one of the words, to do that we'll need to use the Random class.
            Random random = new Random();

            // Here we use the random.Next(minimum, maximum) to generate a number between the two values.
            // We use the length of the list of words so that if we add more words, the random number can
            // become higher.
            int randomNumber = random.Next(0, words.Length);

            // Now lets use the randomNumber to get the randomly selected word from the words list.
            string word = words[randomNumber];

            // Now we need to split up the word so that we have each letter, to do that we'll use the .ToCharArray() function.
            char[] characters = word.ToCharArray();

            // Now we should start getting input from the user, making them guess for the word.
            // Lets print some text first though.

            Console.WriteLine("Welcome to the Hangman Game!");
            Console.WriteLine("Write 'start' to begin or 'exit' to quit");


            // Lets read their initial input to see if they want to play or they want to quit.

            do
            {
                // We save what they wrote to a variable.
                string input = Console.ReadLine();

                // Then we check what they wrote up against the avaliable commands (start / exit)
                if (input == "exit")
                {
                    Console.WriteLine("Exiting game.");
                    Console.Beep();
                    Console.Beep();
                    Console.Beep();
                    Console.Beep();
                    Console.Beep();
                    return;
                }
                else if (input == "start")
                {
                    Console.WriteLine("Lets begin!");

                    // This will break the "do" loop, and continue the code, look into using loops
                    // and how they work, they make a lot of work a lot easier.
                    break;
                }

            } while (true);

            // Code then continues here after the "break;", when you write "start".
            // Here we use the length of the randomly selected characters to tell the player.
            Console.WriteLine("Your word has {0} letters in it", characters.Length);

            // Here we'll use a for loop, also an essential loop for all programmers.
            // It'll keep looping while the variable i is less than the length of the characters.
            for (int i = 0; i < characters.Length; i++)
            {
                do
                {
                    // We start out by getting the users input again, being their guess.
                    // We convert the line into characters and then take the first, so that
                    // the player only write one character at a time. (taking the first character if they write more).
                    char input = Console.ReadLine().ToCharArray()[0];

                    // Now we need to check if it's the correct letter
                    if (input == characters[i])
                    {
                        // when it's correct we break the loop, continuing to the next character.
                        Console.WriteLine("You guessed the right letter!");
                        break;
                    }
                    else {
                        // If the character is not the right one, then it's naturally the wrong one ;)/>/>/>
                        Console.WriteLine("Wrong letter!");
                    }
                } while (true);
            }

            // After the for loop is complete the player would've guessed the entire word, therefore the player has won
            // when he/she has reached here.

            // We'll use the word we randomly selected in the start here again.
            Console.WriteLine("You won the word was {0}", word);

            Console.Beep();
            Console.Beep();
            Console.Beep();
            Console.Beep();
            Console.Beep();
            Console.Beep();
            Console.Beep();
            Console.Beep();
            Console.Beep();
            Console.ReadKey(true);

            // Game over.
        }


I don't have time to explain what I did right now, but I'll try to get back to you when I get home.
Was This Post Helpful? 1
  • +
  • -

#4 XxArcanexX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-January 14

Re: Help with my hangman game?

Posted 07 April 2014 - 03:03 PM

Thank you Robbss! That really explained what I wanted especially the stuff I was uncertain about, thank you! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1