Link to home
Start Free TrialLog in
Avatar of December2000
December2000

asked on

Hang man in C# Console App

I was looking at this code and trying to determine why is not working.... any thoughts?


 static void HangmanEasy()
        {

            //creates a new list of string values called word

            List<string> word = new List<string>();



            //adds words to the list

            word.Add("WOOD");

            word.Add("ACHE");

            word.Add("LAMB");

            word.Add("AERO");

            word.Add("AFRO");

            word.Add("AUTO");

            word.Add("BABE");

            word.Add("BAKE");

            word.Add("BALD");

            word.Add("BALL");

            word.Add("BAND");

            word.Add("BANG");

            word.Add("BANK");

            word.Add("BARK");

            word.Add("BARN");

            word.Add("BASE");

            word.Add("BASS");

            word.Add("BATH");

            word.Add("BEAR");

            word.Add("BEER");

            word.Add("BIKE");

            word.Add("BITE");

            word.Add("BIRD");

            word.Add("BODY");

            word.Add("BOMB");

            word.Add("BURN");

            word.Add("BUSH");

            word.Add("CAFE");

            word.Add("CAKE");

            word.Add("CALM");



            //creates a new string array called FourLetterWords with capacity of the count of the list

            string[] FourLetterWords = new string[word.Count];



            //loop to put the words in the list into the array

            for (int i = 0; i < word.Count; i++)
            {

                FourLetterWords[i] = word[i];

            }



            //creates a string variable and stores a random word from the array 

            string fourWord = FourLetterWords[r.Next(1, (word.Count + 1))];



            //puts the string into seperate characters and stores them in an array called letters

            char[] letters = fourWord.ToCharArray();



            //creates a string array called empty for the default display when no letters have been guessed

            //will end up looking like this _ _ _ _

            string[] empty = new string[4];



            //stores the empty underscore "_" into each slot in the array

            for (int a = 0; a < empty.Length; a++)
            {

                empty[a] = "_";

            }



             //starts the game of hangman by setting the default screen to four underscores _ _ _ _

             Play:

            Console.Clear();

            Console.WriteLine("Choose your letters and lets play!");

            Console.WriteLine("----------------------------------");

            Console.WriteLine("");

            Console.WriteLine("            " + empty[0] + " " + empty[1] + " " + empty[2] + " " + empty[3]);

            Console.WriteLine("");



            //stores the letter the user enters to a string variable called letterChoice

            string letterChoice = Console.ReadLine().ToUpper();



            //puts the choosen letter into a char array

            char[] letter = letterChoice.ToCharArray();



            ///THIS IS THE BIT WHERE IT SEEMS TO GO WRONG!!!



            //a for loop searches 4 times as the choosen random word is 4 letters (letters.Length = 4)

            //it gives a warning saying pos ++ is unreachable code?

            for (int pos = 0; pos < letters.Length; pos++)
            {

                //if the position of the choosen word is equal to the choosen letter

                if (letters[pos] == letter[0])
                {

                    //the underscore of that position is replaced by the letter they choose

                    empty[pos] = letterChoice;

                    //returns back to the default screen but the letter remains where the user guessed

                    goto Play;

                }

                //if the letter they choose isnt in the word it screws up

                //if they choose a correct letter then an incorrect letter then a correct letter again it doesnt work :S

                else
                {

                    goto Play;

                }
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of December2000
December2000

ASKER

Thank you
Play with this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        static Random r = new Random();
        static List<string> words = new List<string>();

        static void Main(string[] args)
        {
            LoadWords();
            HangmanEasy();
        }

        static void LoadWords()
        {
            words.Add("WOOD");
            words.Add("ACHE");
            words.Add("LAMB");
            words.Add("AERO");
            words.Add("AFRO");
            words.Add("AUTO");
            words.Add("BABE");
            words.Add("BAKE");
            words.Add("BALD");
            words.Add("BALL");
            words.Add("BAND");
            words.Add("BANG");
            words.Add("BANK");
            words.Add("BARK");
            words.Add("BARN");
            words.Add("BASE");
            words.Add("BASS");
            words.Add("BATH");
            words.Add("BEAR");
            words.Add("BEER");
            words.Add("BIKE");
            words.Add("BITE");
            words.Add("BIRD");
            words.Add("BODY");
            words.Add("BOMB");
            words.Add("BURN");
            words.Add("BUSH");
            words.Add("CAFE");
            words.Add("CAKE");
            words.Add("CALM");
        }

        static void HangmanEasy()
        {
            //creates a string variable and stores a random word from the array 
            string word = words[r.Next(words.Count)].ToUpper();

            //puts the string into seperate characters and stores them in an array called letters
            char[] letters = word.ToCharArray();

            //creates a char array called empty for the default display when no letters have been guessed
            //will end up looking like this _ _ _ _
            char[] board = new string('_', word.Length).ToCharArray();

            string letterChoice =  "";
            while (String.Join("", board) != word && letterChoice != "QUIT")
            {
                DisplayBoard(board);
                Console.Write("Your guess: (or \"quit\" to end) ");

                //stores the letter the user enters to a string variable called letterChoice
                 letterChoice = Console.ReadLine().Trim().ToUpper();
                if (letterChoice.Length > 0 && letterChoice != "QUIT")
                {
                    char letter = letterChoice.ToCharArray()[0];
                    letterChoice = letterChoice.Substring(0, 1);
                    for (int i = 0; i < word.Length; i++)
                    {
                        if (letters[i] == letter)
                        {
                            board[i] = letter;
                        }
                    }
                }
            }
            if (letterChoice != "QUIT")
            {
                DisplayBoard(board);
                Console.WriteLine("You got my word!");
            }
            else
            {
                Console.WriteLine("Maybe next time!");
            }
            Console.WriteLine("Press Enter to quit...");
            Console.ReadLine();
        }

        static void DisplayBoard(char[] board)
        {
            Console.Clear();
            Console.WriteLine("Choose your letters and lets play!");
            Console.WriteLine("----------------------------------");
            Console.WriteLine("");
            Console.WriteLine("\t" + String.Join(" ", board));
            Console.WriteLine("");
        }

    }
}

Open in new window