Click here to Skip to main content
15,891,942 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am trying to get this game to stop if the user guesses wrong 3 times. It is checking a random word character by character and updating if the guess matches any of the letters.

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

namespace Hangman_2
{
    class Program
    {
        static int wrongGuess, lettersLeft;

        static void Main(string[] args)
        {
            string wordToGuess = GetWordToGuess();

            char[] maskedWord = GetHiddenLetters(wordToGuess, '-');

            lettersLeft = wordToGuess.Length;
            char userGuess;

            wrongGuess = 3;

            while (wrongGuess > 0 && lettersLeft > 0)
            {
                DisplayCharacters(maskedWord);

                Console.WriteLine("Enter a letter?");
                userGuess = char.Parse(Console.ReadLine());

                maskedWord = CheckGuess(userGuess, wordToGuess, maskedWord);
            }

            Console.WriteLine("Well done! Thanks for playing.");
            Console.ReadLine();
        }

        static string GetWordToGuess()
        {
            Random number = new Random();
            int wordNumber = number.Next(0, 9);

            string[] words = { "hyperbaric", "temporal", "quantum", "radiation", "aardvark", "accident", "dracolich", "professional", "properties", "collections" };

            string selectWord = words[wordNumber];
            return selectWord;
        }

        static char[] GetHiddenLetters(string word, char mask)
        {
            char[] hidden = new char[word.Length];

            for (int i = 0; i < word.Length; i++)
            {
                hidden[i] = mask;
            }

            return hidden;
        }

        static void DisplayCharacters(char[] characters)
        {
            foreach (char letter in characters)
            {
                Console.Write(letter);
            }
            Console.WriteLine();
        }

        static char[] CheckGuess(char letterToCheck, string word, char[] characters)
        {
            if (wrongGuess > 0)
            {
                for (int i = 0; i < word.Length; i++)
                {
                    if (word[i] == letterToCheck)
                    {
                        characters[i] = word[i];
                        lettersLeft--;
                    }
                }
            }
            else
            {
                wrongGuess--;
            }

            return characters;
        }
    }
}
Posted
Comments
shakil0304003 9-Oct-10 23:40pm    
What is your question here? If you want to show your code or your theory. You can write an article. :D
Abhinav S 10-Oct-10 6:00am    
So - whats the question?
Toli Cuturicu 11-Oct-10 7:31am    
No question whatsoever.

Are you running into any problems with the code above? If so, you have not mentioned what it is.

If you are just mentioning that you have written this code, then you can do so by posting a new tip/trick in this[^] section.
 
Share this answer
 
Your CheckGuess code will never decrement wrongGuess.
Instead of:

if (wrongGuess > 0)


try

if (word.Contains(letterToCheck))
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900