0

I am making a hangman in C# by my own and so far i am doing good. The game works but the letters organize depending on the users input and i want to erase the " _ " from the character everytime the word is found and is on the text.

This is how it looks on the game :

enter image description here

As you can see , the "_" only disapears for the first letter but not for the others and if the users put the correct answer correctly but radomly some words stick together in many cases and do not stay on their places. This my code for the game:

        string word;
        char letter_inserted;
        int correctanswer, incorrectanswer;
        int counter = 0;
        correctanswer = 5;

        Console.WriteLine("welcome to the game of hangman");
        Console.Write("First player please introduce the secret word: ");
        word = Console.ReadLine();

        Char[] sentenceChar = word.ToCharArray();

        Console.WriteLine("Second player you can already play.");
        foreach( char letter in senteceChar)
        {
            Console.Write(" _ "); 
        }
        Console.WriteLine("\n");
        Console.WriteLine("{0} correcct answers are allowed. \n",correctanswers); //lives
        Char[] correctletter = new Char[5];
        for (int i = 1; i > 0; i++)
        {
            Console.Write("\n\nWhat letter you want to play : ");
            letter_inserted = Convert.ToChar(Console.ReadLine());

            for (int j = 0; j < sentenceChar.Length; j++)
            {
                if (sentenceChar[j] != letter_inserted)
                {
                    incorrectanswer = correctanswer - 1; //lives
                    Console.Write(correctletter[j] + " _ ");                      
                }

                if (sentenceChar[j] == letter_inserted)
                {
                    correctletter[j] += sentnceChar[j]; //inserting the correct word
                    Console.Write(correctletter[j]);
                }
            }
        }

        Console.ReadKey();
    }
7
  • 1
    You should REALLY try to use English in code. Especially when asking questions here. Commented Feb 26, 2014 at 20:31
  • It would be so much easier if you were using english in your code. However, let's start from the beginning. Why do you use for (int i = 1; i > 0; i++). I'm not sure about the exact rules of hangman but didn't you have exact numbers of guess to find out the word after which you are supposed to lose the game? Commented Feb 26, 2014 at 20:32
  • It also looks like you're subtracting a life for each time a letter is not the entered input. For example, if you had a 5 letter word and entered 'S', and there were no S's, you'd lose 5 lives. You probably just want to subtract a life if the input does not appear anywhere in the array. Commented Feb 26, 2014 at 20:36
  • @LukaHorvat yes sorry for that , next time i will do it. Commented Feb 26, 2014 at 21:06
  • @Leron that loop is to take user inputs how many times is necesary depending on the lives Commented Feb 26, 2014 at 21:07

2 Answers 2

2

I would use a different approach for this. Create two arrays, one for the solution:

var solution = new char[] {'H', 'A', 'N', 'G', 'M', 'A', 'N'};

And one for the current hint. At the beginning, you'd initialize this to:

var hint = new char[] {'_', '_', '_', '_', '_', '_', '_'};

When a letter is guessed, you can loop through solution and each time solution[i] is equal to the guessed letter, replace the corrosponding index in hint:

for(int i = 0; i < solution.Length; i++)
{
   if(solution[i] == guess)
      hint[i] = guess;
}

At the end of this loop, just re-write hint onto the screen, which will always contain the current hint.

A few things you'll want to do:

  1. Have a Boolean within your loop to track if solution contained any instances of the letter. If not, at the end of the loop you'd subtract a life. You can also do this very easily using solution.Contains(guess) as well.

  2. Perhaps create a List<char> to track already attempted guesses. If this list already contains the entered letter, you can display "You already guessed that!", otherwise add the guess to the list.

This answer is meant as a hint to get you going in the right direction, rather than a complete solution. However, feel free to ask if any part of it is unclear.

Sign up to request clarification or add additional context in comments.

2 Comments

ok the contains(guess) , I undertand that but im using the array letracorrecta to catch the letters whats the difference in using a list?. Can you explain me that
No real difference. A List<> is implemented as a hash table, so you can very quickly see if it already contains a certain key without having to loop through each item. It's a bit faster (not noticeable in this case) and keeps the code a bit cleaner.
0

I will start with your first sentence - I am making a hangman in C# by my own and so far i am doing good. In fact as you can see in the comments there are a lot of things that you would want to consider before going to this exact problem but since you ask how to deal with the extra _ I'll jump to that.

And a method :

public static void printWord(char[] correctletters)
{
  for (int j = 0; j < correctletters.Length; j++)
   {
    Console.Write(correctletters[j]);
   }
}

And then rewrite your code like this:

namespace Hangman
{
    class Program
    {
    static void Main(string[] args)
    {
        string word;
        char letter_inserterd;
        int correctanswer = 5;
        int counter = 0;

        Console.WriteLine("welcome to the game of hangman");
        Console.Write("First player enter the secret word: ");
        word = Console.ReadLine();

        Char[] sentenceChar = word.ToCharArray();

        Console.WriteLine("Second player you can already play.");
        foreach( char letter in sentenceChar)
        {
            Console.Write(" _ "); 
        }
        Console.WriteLine("\n");
        Console.WriteLine("{0} correct answers are allowed. \n",correctanswer); //lives
        Char[] correctletter = new Char[sentenceChar.Length];

        for (int i = 0; i <sentenceChar.Length; i++)
        {
            correctletter[i] = '_'; 
        }

        while (correctanswer > 0)
        {
            Console.Write("\n\nWhat letter you want to play : ");
            letter_inserted = Convert.ToChar(Console.ReadLine());
            bool ContainsWord = false;

            for (int j = 0; j < sentenceChar.Length; j++)
            {
                if (sentnceChar[j] == letter_inserted)
                {
                    correctletter[j] = letter_inserted; //inserting the correct word
                    ContainsWord = true;
                }

            }       
            if (!ContainsWord)
            {
              correctanswer -= 1;
            }
            printWord(correctletter);

        }

    Console.ReadKey();
    }


    public static void printWord(char[] correctletter)
    {
        for (int j = 0; j < correctletter.Length; j++)
        {
            Console.Write(correctletter[j]);
        }
    }
}
}

P.S if you want to make it a little more clear in printWord method change the Console.Write line like this: Console.Write(letracorrecta[j] + " ");

10 Comments

I cant make the first method work , and the second one i already had it but not as a method but the thing is should i make a char of ' _ ' and for each letter assing it the char?
The important thing in my idea is to make your code more generic. And this current method is, one to help you overcome the problem with the extra _ and second - to not have to use hard coded value for the word length as it is 5 chars. See the update code, it should compile and work if everything else is fine.
OMG it works . thanks alot man . I never was thiking doing like that thanks !! . You are the man . I lke programming and some times it takes me alot to overcome a problem , im pretty sure this was so easy for you hahahaha
you say the messages of the code translate them all to engish?
First, to be honest I'm not very happy that I gave you a working code, because that's not the best way to find out how and why something is working, but I got confused myself several times so I ended up writing this but I really hope that you'll examine the code and look through the things that you couldn't figure out by yourself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.