8 Replies - 668 Views - Last Post: 09 September 2012 - 02:11 AM Rate Topic: -----

#1 Serenity_Trinity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 31-August 12

Stumped on Hangman console game.

Posted 06 September 2012 - 09:34 PM

My error is at the first if statement (playHangman.guesedLetters[](guessedLetters.KeyChar))

I have changed it a couple times and gotten a couple different error messages. Could you give me some insight?

A couple of the errors I have gotten, "i" does not exist in current context, and guessedLetters cant be used like a method.

while (playHangman.Result() == Hangman_Game.GAMERESULT.CONTINUE)
                        {
                            Console.Write("Pick a letter ---> ");
                            ConsoleKeyInfo guessedLetters = Console.ReadKey();
                            
                            if (playHangman.guessedLetters[](guessedLetters.KeyChar))
                                playHangman.Play();
                        }
                        if (playHangman.Result() == Hangman_Game.GAMERESULT.LOSE)
                        {
                            Console.WriteLine("Sorry, you didn't guess correctly. ");
                            makeTextBlink("The mystery word was '" + pickedWord.Content.ToUpper() + "'", 500);
                            Console.WriteLine("Do you want to play again ? Y/N");
                            yesNo = Console.ReadKey();
                            return yesNo.KeyChar.ToString();
                        }



Is This A Good Question/Topic? 0
  • +

Replies To: Stumped on Hangman console game.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3538
  • View blog
  • Posts: 10,952
  • Joined: 05-May 12

Re: Stumped on Hangman console game.

Posted 07 September 2012 - 12:04 AM

You need to paste in the exact error you are getting instead of summarizing. Often, the error will not only have the line number, but the character index as well.

Right of the top of my head, the syntax you have there is just plain old wrong. There should be some kind of index value between the square brackets. Perhaps it was the KeyChar that you were intending to use as the index?
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Stumped on Hangman console game.

Posted 07 September 2012 - 12:16 AM

This line looks interesting
if (playHangman.guessedLetters[](guessedLetters.KeyChar))



I think you have a compilation error there. I'm not really sure what you wan't the if-block to check.

I wouldn't name this variable 'gussedLetters' since the object only contains one key
ConsoleKeyInfo guessedLetters = Console.ReadKey();


Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

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

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Stumped on Hangman console game.

Posted 07 September 2012 - 06:29 AM

There are numerous issues with this. Some in logic. Some in just presuming the user will do exactly as expected. Some with syntax. Some with naming as Skydiver pointed out.

I would suggest you read through 20 or so other hangman threads and learn some better techniques for most of these things. You're not the first person to do this for class. There is a wealth of examples here for hangman. You just need to read through them.

Go to the C# page, scroll to the bottom, and enter 'hangman' in the search field.

Attached Image
Was This Post Helpful? 0
  • +
  • -

#5 Serenity_Trinity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 31-August 12

Re: Stumped on Hangman console game.

Posted 07 September 2012 - 03:43 PM

Error 1 Syntax error; value expected
I have had multiple problems with this one. I have tried many "fixes" and no luck. I have even looked around to try and figure it out, but I am still stumped. Does anyone have any suggestions to get this program built and running.


using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;


namespace Hangman_Game
    {
        /// <summary>
        /// Class Word
        /// </summary>
        public class Word
        {
            /// <summary>
            /// Class word
            /// </summary>
            public Word()
            {
            }

            /// <summary>
            /// Class word
            /// </summary>
            /// <param name="content">Content of the word</param>
            public Word(string content)
            {
                this.Content = content;
            }

            /// <summary>
            /// Get or set word content
            /// </summary>
            public string Content { get; set; }

            /// <summary>
            /// Get or set word length
            /// </summary>
            public int WordLength
            {
                get { return this.Content.Length; }
            }
        }
    }

    namespace Hangman_Game
    {
        /// <summary>
        /// Class which holds a collection of words for the hangman game
        /// </summary>
        public class Words : List<Word>
        {
            public Words()
            {
                this.Add(new Word() { Content = "VACATION" });
                this.Add(new Word() { Content = "INFORMATION" });
                this.Add(new Word() { Content = "TECHNOLOGY" });
                this.Add(new Word() { Content = "COMPUTER" });
                this.Add(new Word() { Content = "ROUTER" });
                this.Add(new Word() { Content = "PRINTER" });
                this.Add(new Word() { Content = "SOFTWARE" });
                this.Add(new Word() { Content = "HARDWARE" });
                this.Add(new Word() { Content = "EMPLOYEE" });
                this.Add(new Word() { Content = "SECURITY" });
                this.Add(new Word() { Content = "DATA" });
                this.Add(new Word() { Content = "REPORT" });
                this.Add(new Word() { Content = "PROPERTY" });
                this.Add(new Word() { Content = "OWNERSHIP" });
            }

            /// <summary>
            /// Pick a random word
            /// </summary>
            /// <returns></returns>
            public Word Pick
            {
                get
                {
                    Random RandomPick = new Random();
                    int index = (int)(RandomPick.NextDouble() * this.Count);
                    Word word = this[index];
                    word.Content = word.Content.ToUpper();
                    return word;
                }
            }
        }
    }

    namespace Hangman_Game
    {
        /// <summary>
        /// Game result enumeration
        /// </summary>
        public enum GAMERESULT
        {
            WIN,// Game is finished and player won the game
            LOSE,// Game is finished and player lose the game
            CONTINUE,// Continue play
        }
    }

            namespace Hangman_Game
            {
                /// <summary>
                /// Main Program class
                /// </summary>
                class playHangman
                {
                    private static object guessedLetters;
                    static void Main(string[] args)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Welcome to hangman. You get seven chances to guess the mystery word.");
                        string yesNo = string.Empty;
                        do
                        {
                            Console.WriteLine();
                            yesNo = playGame();
                        } while (yesNo.ToUpper().Equals("Y"));
                    }
                  
                    /// <summary>
                    /// Make text blink
                    /// </summary>
                    /// <param name="text">Text to be blinked</param>
                    /// <param name="delay">Deley time value</param>
                    static void makeTextBlink(string text, int delay)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            writeBlinkingText(text, delay, true);
                            writeBlinkingText(text, 500, false);
                        }
                    }

                    /// <summary>
                    /// Write blinking text
                    /// </summary>
                    /// <param name="text">Text to be blinked</param>
                    /// <param name="delay">Delay time value</param>
                    /// <param name="visible">Set visiblity of the text</param>
                    private static void writeBlinkingText(string text, int delay, bool visible)
                    {
                        if (visible)
                            Console.Write(text);
                        else
                            for (int i = 0; i < text.Length; i++)
                                Console.Write(" ");
                        Console.CursorLeft -= text.Length;
                        Thread.Sleep(delay);
                    }
                    
                    /// <summary>
                    /// Play game
                    /// </summary>
                    public static string playGame()
                    {
                        Words words = new Words();
                        Word pickedWord = words.Pick;
                        PlayHangman playHangman = new PlayHangman();
                        Word PickedWord = pickedWord;
                        ConsoleKeyInfo yesNo = new ConsoleKeyInfo();
                        
                        for (int i = 0; i < pickedWord.WordLength; i++)
                        {
                            Console.Write(" _ ");
                        }
                        Console.WriteLine();
                        Console.WriteLine();
                        Console.WriteLine();
                        while (playHangman.Result() == Hangman_Game.GAMERESULT.CONTINUE)
                        {
                            Console.Write("Pick a letter ---> ");
                            ConsoleKeyInfo guessedLetters = Console.ReadKey();
                            
                            if (playHangman.guessedLetters[i](guessedLetters.KeyChar))
                                playHangman.Play();
                        }
                        if (playHangman.Result() == Hangman_Game.GAMERESULT.LOSE)
                        {
                            Console.WriteLine("Sorry, you didn't guess correctly. ");
                            makeTextBlink("The mystery word was '" + pickedWord.Content.ToUpper() + "'", 500);
                            Console.WriteLine("Do you want to play again ? Y/N");
                            yesNo = Console.ReadKey();
                            return yesNo.KeyChar.ToString();
                        }
                        else
                        {
                            makeTextBlink("You won!", 500);
                            Console.WriteLine("Do you want to play again ? Y/N");
                            yesNo = Console.ReadKey();
                            return yesNo.KeyChar.ToString();
                        }
                    }
                }
            }
            namespace Hangman_Game
            {
                /// <summary>
                /// Class play hangman game
                /// </summary>
                public class PlayHangman
                {
                    /// <summary>
                    /// Get or set the random picked word
                    /// </summary>       
                    public Word PickedWord
                    {
                        get;
                        set;
                    }

                    /// <summary>
                    /// Variable used to hold guessed and found letters
                    /// </summary>
                    public List<string> guessed_FoundLetters;

                    /// <summary>
                    /// Variable used to hold user guessed letters
                    /// </summary>
                    public List<string> guessedLetters;

                    /// <summary>
                    /// Variable used to hold user missed words from the word
                    /// </summary>
                    public List<string> MissedLetters;

                    /// <summary>
                    /// Class play hang man game
                    /// </summary>
                    public PlayHangman()
                    {
                        guessed_FoundLetters = new List<string>();
                        guessedLetters = new List<string>();
                        MissedLetters = new List<string>();
                    }

                    /// <summary>
                    /// Process the user guessed letter against the random picked word
                    /// </summary>
                    public void Play()
                    {
                        guessed_FoundLetters = new List<string>();

                        for (int i = 0; i < PickedWord.WordLength; i++) // Add underscore to the guessed and found string collection
                        {
                            guessed_FoundLetters.Add(" _ ");
                        }

                        for (int i = 0; i < PickedWord.WordLength; i++)
                        {
                            string letter = PickedWord.Content.Substring(i, 1);
                            if (guessedLetters.Count > 0)
                            {
                                foreach (string guessedLetter in this.guessedLetters)
                                {
                                    if (letter.Equals(guessedLetter.Trim().ToUpper())) // If the guessed letter is found from the picked word then replace underscore with the letter
                                    {
                                        guessed_FoundLetters.RemoveAt(i);
                                        guessed_FoundLetters.Insert(i, " " + letter + " ");
                                    }
                                }
                            }
                        }
                        drawHangMan();
                        Console.WriteLine(buildString(guessed_FoundLetters, false));
                        Console.WriteLine();
                    }

                    /// <summary>
                    /// Add user guessed letter
                    /// </summary>
                    /// <param name="letter">Letter guessed by the user</param>
                    /// <returns>true/false</returns>
                    public bool AddGuessedLetters(char letter)
                    {
                        if (char.IsDigit(letter))  // Check if the letter is a digit
                        {
                            Console.WriteLine();
                            Console.WriteLine("'" + letter.ToString().ToUpper() + "' is not a valid letter");
                            return false;
                        }
                        else if (!this.guessedLetters.Contains(letter.ToString().ToUpper())) // Add guessed letter if it does not exist
                        {
                            this.guessedLetters.Add(letter.ToString().ToUpper());
                            Console.WriteLine();
                            Console.WriteLine("Guessed Letters : " + buildString(guessedLetters, true));
                            return true;
                        }
                        else // letter exists
                        {
                            Console.WriteLine();
                            Console.WriteLine("Sorry, you already guessed '" + letter.ToString().ToUpper() + "'");
                        }
                        return false;
                    }

                    /// <summary>
                    /// Check a letter is found from the random picked word
                    /// </summary>
                    /// <param name="letter">Letter to be checked</param>
                    /// <returns>true/false</returns>
                    public bool checkLetter(string letter)
                    {
                        for (int i = 0; i < PickedWord.WordLength; i++)
                        {
                            string splitedletter = PickedWord.Content.Substring(i, 1).ToUpper();
                            if (splitedletter.Equals(letter.Trim().ToUpper()))
                            {
                                return true;
                            }
                        }
                        return false;
                    }

                    /// <summary>
                    /// Build a string from string collection
                    /// </summary>
                    /// <param name="inPutString">Collection of strings to be build</param>        
                    /// <param name="space">Space to be appended</param>
                    /// <returns>an appended string</returns>
                    public string buildString(List<string> inPutString, bool space)
                    {
                        StringBuilder outPut = new StringBuilder();
                        foreach (object item in inPutString)
                        {
                            if (space)
                                outPut = outPut.Append(item.ToString().ToUpper() + " ");
                            else
                                outPut = outPut.Append(item.ToString().ToUpper());

                        }
                        return outPut.ToString();
                    }

                    /// <summary>
                    /// Draw hang man based on the missed letters from the used guessed letters
                    /// </summary>
                    private void drawHangMan()
                    {

                        MissedLetters = new List<string>();
                        foreach (string item in guessedLetters)
                        {
                            if (!checkLetter(item))
                            {
                                MissedLetters.Add(item);
                            }
                        }

                        Console.WriteLine();
                        if (MissedLetters.Count == 1)
                        {
                            Console.WriteLine("   _____");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |     O");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("__|__");
                        }
                        else if (MissedLetters.Count == 2)
                        {
                            Console.WriteLine("   _____");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |     O");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("__|__");
                        }
                        else if (MissedLetters.Count == 3)
                        {
                            Console.WriteLine("   _____");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |     O");
                            Console.WriteLine("  |    \\|");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("__|__");
                        }
                        else if (MissedLetters.Count == 4)
                        {
                            Console.WriteLine("   _____");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |     O");
                            Console.WriteLine("  |    \\|/");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("__|__");

                        }
                        else if (MissedLetters.Count == 5)
                        {
                            Console.WriteLine("   _____");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |     O");
                            Console.WriteLine("  |    \\|/");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |");
                            Console.WriteLine("  |");
                            Console.WriteLine("__|__");
                        }
                        else if (MissedLetters.Count == 6)
                        {
                            Console.WriteLine("   _____");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |     O");
                            Console.WriteLine("  |    \\|/");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |    /");
                            Console.WriteLine("  |");
                            Console.WriteLine("__|__");
                        }
                        else if (MissedLetters.Count == 7)
                        {
                            Console.WriteLine("   _____");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |     O");
                            Console.WriteLine("  |    \\|/");
                            Console.WriteLine("  |     |");
                            Console.WriteLine("  |    / \\");
                            Console.WriteLine("  |");
                            Console.WriteLine("__|__");
                        }
                        else
                            Console.WriteLine();
                        Console.WriteLine();
                    }

                    /// <summary>
                    /// Process win lose the game
                    /// </summary>
                    /// <returns>returns an enumeration of win, lose or proceed</returns>
                    public GAMERESULT Result()
                    {
                        if (MissedLetters.Count == 7)//If full hang man is built then, you lose the game
                        {
                            return GAMERESULT.LOSE;
                        }
                        else if (PickedWord.Content.ToUpper().Equals(buildString(guessed_FoundLetters, false).Replace(" ", ""))) // If the guessed letters match all the letters from random picked word, then you win the game
                        {
                            return GAMERESULT.WIN;
                        }
                        else
                            return GAMERESULT.CONTINUE; // Else play the game
                    }
                }
            }



The syntax error is on line 175. I have taken the "i" out of the brackets, so it is no longer there.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

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

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Stumped on Hangman console game.

Posted 07 September 2012 - 05:05 PM

450+ lines for HANGMAN????? Oh dear god.

Please do a search for Hangman in the C# section. You're not the first to work on this assignment. Not even the first this week to post a thread for this. Read through 20 or so threads for this and you should see ways of GREATLY reducing this.

Attached Image

View PostSerenity_Trinity, on 07 September 2012 - 04:43 PM, said:

Error 1 Syntax error; value expected
I have had multiple problems with this one. I have tried many "fixes" and no luck.


You don't even tell us where the error happens. Do you expect us to scrub through 450+ lines of code to find it for you?

I'm sure I've pointed you to these "How to debug" tutorials and "What does this error mean" article before. This time please actually work through them.

Serenity_Trinity: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2. (Click the SHOW button below)



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2012
Spoiler




[Topic title changed... This is console not GUI. The G in GUI stands for Graphical, which Console.Writeline is not]

[Duplicate topics merged] Please don't open new threads when you have one open already - especially when it is getting active responses.
Was This Post Helpful? 0
  • +
  • -

#7 Serenity_Trinity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 31-August 12

Re: Stumped on Hangman console game.

Posted 09 September 2012 - 12:29 AM

Thank you for helping me realize the code was extremely to long. I have made some changes and fixed a couple problems. My main issue now, is I end up in a infinite loop asking to pick a letter. After looking it over multiple times, I'm almost positive all I need is maybe a couple extra lines of code. Which I can't seem to come across or think of. If anyone has any pointers or tips, it would be greatly appreciated.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Hangman_Game
    {
        /// Class Word
        public class Word
        {
            /// Class word
            public Word()
            {
            }
            /// Class word
            public Word(string content)
            {
                this.Content = content;
            }
            /// Get or set word content
            public string Content { get; set; }
            /// Get or set word length
            public int WordLength
            {
                get { return this.Content.Length; }
            }
        }
    }
    namespace Hangman_Game
    {
        /// Class which holds a collection of words for the hangman game
        public class Words : List<Word>
        {
            public Words()
            {
                this.Add(new Word() { Content = "VACATION" });
                this.Add(new Word() { Content = "INFORMATION" });
                this.Add(new Word() { Content = "TECHNOLOGY" });
                this.Add(new Word() { Content = "COMPUTER" });
                this.Add(new Word() { Content = "ROUTER" });
                this.Add(new Word() { Content = "PRINTER" });
                this.Add(new Word() { Content = "SOFTWARE" });
                this.Add(new Word() { Content = "HARDWARE" });
                this.Add(new Word() { Content = "EMPLOYEE" });
            }
            /// Pick a random word
            public Word Pick
            {
                get
                {
                    Random RandomPick = new Random();
                    int index = (int)(RandomPick.NextDouble() * this.Count);
                    Word word = this[index];
                    word.Content = word.Content.ToUpper();
                    return word;
                }
            }
        }
    }
    namespace Hangman_Game
    {
        /// Game result enumeration
        public enum GAMERESULT
        {
            WIN,// Game is finished and player won the game
            LOSE,// Game is finished and player lose the game
            CONTINUE,// Continue play
        }
    }
            namespace Hangman_Game
            {
                /// Main Program class
                class playHangman
                {
                    static void Main(string[] args)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Welcome to hangman. You get seven chances to guess the mystery word.");
                        string yesNo = string.Empty;
                        do
                        {
                            Console.WriteLine();
                            yesNo = playGame();
                        } while (yesNo.ToUpper().Equals("Y"));
                    }
                    /// Play game
                    public static string playGame()
                    {
                        Words words = new Words();
                        Word pickedWord = words.Pick;
                        PlayHangman playHangman = new PlayHangman();
                        playHangman.PickedWord = pickedWord;
                        Word PickedWord = pickedWord;
                        ConsoleKeyInfo yesNo = new ConsoleKeyInfo();

                        for (int i = 0; i < pickedWord.WordLength; i++)
                        {
                            Console.Write(" * ");
                        }
                        Console.WriteLine();
                        Console.WriteLine();
                        Console.WriteLine();
                        while (playHangman.Result() == Hangman_Game.GAMERESULT.CONTINUE)
                        {
                            Console.Write("Pick a letter ---> ");
                            ConsoleKeyInfo guessedLetters = Console.ReadKey();
                            break;
                        }
                        if (playHangman.Result() == Hangman_Game.GAMERESULT.LOSE)
                        {
                            Console.WriteLine("Sorry, you didn't guess correctly. ");
                            Console.WriteLine("The mystery word was '" + pickedWord.Content.ToUpper() + "'", 500);
                            Console.WriteLine("Do you want to play again ? Y/N");
                            yesNo = Console.ReadKey();
                            return yesNo.KeyChar.ToString();
                            
                        }
                        else
                        {
                            Console.WriteLine("You won!", 500);
                            Console.WriteLine("Do you want to play again ? Y/N");
                            yesNo = Console.ReadKey();
                            return yesNo.KeyChar.ToString();
                        }
                    }
                }
            }
            namespace Hangman_Game
            {
                /// Class play hangman game
                public class PlayHangman
                {
                    /// Get or set the random picked word
                    public Word PickedWord
                    {
                        get;
                        set;
                    }
                    /// Variable used to hold guessed and found letters
                    public List<string> guessed_FoundLetters;
                    /// Variable used to hold user guessed letters
                    public List<string> guessedLetters;
                    /// Variable used to hold user missed words from the word
                    public List<string> MissedLetters;
                    /// Class play hangman game
                    public PlayHangman()
                    {
                        guessed_FoundLetters = new List<string>();
                        guessedLetters = new List<string>();
                        MissedLetters = new List<string>();
                    }
                    /// Process the user guessed letter against the random picked word
                    public void Play()
                    {
                        for (int i = 0; i < PickedWord.WordLength; i++) // Add asterick to the guessed and found string collection
                        {
                            guessed_FoundLetters.Add(" * ");
                        }
                        for (int i = 0; i < PickedWord.WordLength; i++)
                        {
                            string letter = PickedWord.Content.Substring(i, 1);
                            if (guessedLetters.Count > 0)
                            {
                                foreach (string guessedLetter in this.guessedLetters)
                                {
                                    if (letter.Equals(guessedLetter.Trim().ToUpper())) // If the guessed letter is found from the picked word then replace asterick with the letter
                                    {
                                        guessed_FoundLetters.RemoveAt(i);
                                        guessed_FoundLetters.Insert(i, "'" + letter + "'");
                                    }
                                }
                            }
                        }
                        Console.WriteLine(buildString(guessed_FoundLetters, false));
                        Console.WriteLine();
                    }
                    /// Add user guessed letter
                    public bool AddGuessedLetters(char letter)
                    {
                        if (char.IsDigit(letter))  // Check if the letter is a digit
                        {
                            Console.WriteLine();
                            Console.WriteLine("'" + letter.ToString().ToUpper() + "' is not a valid letter");
                            return false;
                        }
                        else if (!this.guessedLetters.Contains(letter.ToString().ToUpper())) // Add guessed letter if it does not exist
                        {
                            this.guessedLetters.Add(letter.ToString().ToUpper());
                            Console.WriteLine();
                            Console.WriteLine("Guessed Letters: " + buildString(guessedLetters, true));
                            return true;
                        }
                        else // letter exists
                        {
                            Console.WriteLine();
                            Console.WriteLine("Sorry, you already guessed '" + letter.ToString().ToUpper() + "'");
                        }
                        return false;
                    }
                    /// Check if letter is found from the random picked word
                    public bool checkLetter(string letter)
                    {
                        for (int i = 0; i < PickedWord.WordLength; i++)
                        {
                            string splitedletter = PickedWord.Content.Substring(i, 1).ToUpper();
                            if (splitedletter.Equals(letter.Trim().ToUpper()))
                            {
                                return true;
                            }
                        }
                        return false;
                    }
                    /// Build a string from string collection
                    public string buildString(List<string> inPutString, bool space)
                    {
                        StringBuilder outPut = new StringBuilder();
                        foreach (object item in inPutString)
                        {
                            if (space)
                                outPut = outPut.Append(item.ToString().ToUpper() + " ");
                            else
                                outPut = outPut.Append(item.ToString().ToUpper());
                        }
                        return outPut.ToString();
                    }
                    /// Process win or lose the game
                    public GAMERESULT Result()
                    {
                        if (MissedLetters.Count == 7)//If full hang man is built then, you lose the game
                        {
                            return GAMERESULT.LOSE;
                        }
                        else if (PickedWord.Equals(buildString(guessed_FoundLetters, false))) // If the guessed letters match all the letters from random picked word, then you win the game
                        {
                            return GAMERESULT.WIN;
                        }
                        else
                            return GAMERESULT.CONTINUE; // Else play the game
                    }
                }
            }


Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3538
  • View blog
  • Posts: 10,952
  • Joined: 05-May 12

Re: Stumped on Hangman console game.

Posted 09 September 2012 - 01:57 AM

What infinite loop? I see a break on line 107 that stops any kind of infinite loop while trying to read a key.

What you actually need is less lines of code. You have over complicated things.

You don't need a Word class. A string will suffice. You are just accessing Word.Content anyway to get the string.

You don't need a Words class to hold a collection of words. The only extra thing you are doing is exposing a Pick property. A stand along function would have sufficed. The constructor that fills in the list could have easily stood alone as well.

You needlessly convert characters to strings. This happens when you ask if the player wants to play again. This happens when you are inspecting the characters of the picked word.

You needlessly convert strings to objects and then back to strings in your buildCharacter() method.

You are holding the guess letters in a list of strings. A list of characters would have been sufficient.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

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

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Stumped on Hangman console game.

Posted 09 September 2012 - 02:11 AM

View PostSerenity_Trinity, on 09 September 2012 - 01:29 AM, said:

Which I can't seem to come across ...


That's the real problem, isn't it? You're not really writing this. You're lost in comprehending the coding and just assembling it from other people's works.

How else can anyone explain the numerous uses of
006 namespace Hangman_Game
029 namespace Hangman_Game
060 namespace Hangman_Game
070 namespace Hangman_Game
[...]


Duplicate stuff like this
073 class playHangman
131 public class PlayHangman


and all the stuff that Skydiver pointed out.

You're lost. We get that. You need to go to your professor and explain to them that you are drowning. Programming is not like history class where you can be lost on the chapter for the 1700's but still do fine on the next chapter about the 1900's. Programming builds on top of each unit. If you don't understand chapter 1 then it makes it hard during chapter 2. If you are lost in both chapters, then you have nothing to base on during chapter 3.

You need to go to your professor and lay it all out for him/her about how lost you are and ask for help, extra time, a tutor... something. Also don't think you are limited to just the course textbook. There are dozens of great "How to learn C# in 30 days" type books out there. Get a couple and work them cover-to-cover in every waking moment you have so you can catch up.

This post has been edited by tlhIn`toq: 09 September 2012 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1