?
Solved

Hang man in C# Console App

Posted on 2012-09-08
3
Medium Priority
?
2,542 Views
Last Modified: 2012-09-09
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

0
Comment
Question by:December2000
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 38379921
The 'unreachable code' is caused by the goto statements. Those should not be inside the for loop anyway because a letter can appear more than once in a word. Using goto statements is usually discouraged in general anyway. You can change it to a while or do loop and define a boolean variable or function that will end the loop when the game is done, based on user input (for example "enter . to quit") or the fact that the maximum number of guesses has been used or all letters have been guessed correctly. I assume you wanted to add something like that anyway ;-)
0
 

Author Closing Comment

by:December2000
ID: 38380061
Thank you
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38380092
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question