Share via


Hangman Console C#

Question

Thursday, June 13, 2013 12:49 PM

Hello,

I am fairly new to C# and I have this hangman game pretty much working. The only thing I need help with is adding "lives" to the game. Something which would exit the game if the player goes beyond their limit. Any help is always appreciated. 

Thanks a lot.      

Hyrax.

 if (uservalue == "1")
            {
                string[] arrayofwords = { "Blue", "Black", "Yellow", "Orange", "Green", "Purple" };        // my word bank

                Random ran = new Random();   // created variable to randomize word bank
                string mywords = arrayofwords[ran.Next(0, arrayofwords.Length)];          // retrievd this peice of code from http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/dc4bd2ad-166b-416c-875d-5c21984c4796
                Console.WriteLine("You chose colours");

                char[] guess = new char[mywords.Length];                      // cite help from this website http://www.dreamincode.net/forums/topic/173101-c%23-hangman-hang-up/
                for (int p = 0; p < mywords.Length; p++)

                    guess[p] = '*';          // this is for the squiglly lines you usually see on hangman games

                    while (true)
                    {
                        Console.Write("Please enter your guess: ");
                        char playerGuess = char.Parse(Console.ReadLine());
                        for (int j = 0; j < mywords.Length; j++)
                        {

                           if( char.ToLower( playerGuess ) == char.ToLower( mywords[j]) )
                               guess[j] = mywords[j];      // http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/e0c38f78-4cc3-43cc-922d-bca598ffb035

                        }

                        Console.WriteLine(guess);
                        

                    }

                
           
                
                 

               
            }

All replies (5)

Thursday, June 13, 2013 1:21 PM âś…Answered | 3 votes

For lives, you just need an integer (int) that is decremented each time a letter is guessed that doesn't appear in the word.

I've just written a very simple version for you to try - if it works for you then look at what I've done and try implementing a similar solution into your own program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random((int)DateTime.Now.Ticks);

            string[] wordBank = { "Blue", "Black", "Yellow", "Orange", "Green", "Purple" };

            string wordToGuess = wordBank[random.Next(0, wordBank.Length)];
            string wordToGuessUppercase = wordToGuess.ToUpper();

            StringBuilder displayToPlayer = new StringBuilder(wordToGuess.Length);
            for (int i = 0; i < wordToGuess.Length; i++)
                displayToPlayer.Append('_');

            List<char> correctGuesses = new List<char>();
            List<char> incorrectGuesses = new List<char>();

            int lives = 5;
            bool won = false;
            int lettersRevealed = 0;

            string input;
            char guess;

            while (!won && lives > 0) 
            {
                Console.Write("Guess a letter: ");

                input = Console.ReadLine().ToUpper();
                guess = input[0];

                if (correctGuesses.Contains(guess)) 
                {
                    Console.WriteLine("You've already tried '{0}', and it was correct!", guess);
                    continue;
                }
                else if (incorrectGuesses.Contains(guess)) 
                {
                    Console.WriteLine("You've already tried '{0}', and it was wrong!", guess);
                    continue;
                }

                if (wordToGuessUppercase.Contains(guess)) 
                {
                    correctGuesses.Add(guess);

                    for (int i = 0; i < wordToGuess.Length; i++) 
                    {
                        if (wordToGuessUppercase[i] == guess)
                        {
                            displayToPlayer[i] = wordToGuess[i];
                            lettersRevealed++;
                        }
                    }

                    if (lettersRevealed == wordToGuess.Length)
                        won = true;
                }
                else 
                {
                    incorrectGuesses.Add(guess);

                    Console.WriteLine("Nope, there's no '{0}' in it!", guess);
                    lives--;
                }

                Console.WriteLine(displayToPlayer.ToString());
            }

            if (won)
                Console.WriteLine("You won!");
            else
                Console.WriteLine("You lost! It was '{0}'", wordToGuess);

            Console.Write("Press ENTER to exit...");
            Console.ReadLine();
        }
    }
}

Thursday, June 13, 2013 9:04 PM

Wow, you gave me more code then I really asked for! I will try my best to understand this and implement it into my own code. Thanks again!


Friday, January 18, 2019 3:43 AM

hi,

I don't know if you will get this, but how did you implement the code above into your original code for hangman 


Friday, January 18, 2019 3:44 AM

<g class="gr_ gr_24 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="24" id="24">Hi ,</g> 

I <g class="gr_ gr_36 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="36" id="36">dont</g> know if you will get this, but how did you implement the code above into your original code


Friday, January 25, 2019 6:42 PM

What