Search code examples
c#projectwords

C# A simple Hangman game


I'm trying to create a simple Hangman game and i have gotten so far, to make it read all words from a text file, but I don't know how to make the code work for every single word. I have another project, working with 3/4 words but with repeating nested if statements. I want to make it as shorter as possible. This is the code i have so far :

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] words = System.IO.File.ReadAllLines(@"C:\Users\ADMIN\Desktop\Letters\Letters.txt");
        int LengthOfArray = words.Length;
        Random rnd = new Random();
        int random = rnd.Next(1, 3);
        char[] letters = words[random].ToCharArray();
        bool WordIsHidden = true;
        char hiddenChar = '_';
        char GuessedLetter = hiddenChar;

        var retry = true;
        while (retry = true)
        {
            Console.WriteLine(letters);
            letters = GuessedLetter.ToString().ToCharArray();
            for (int i = 1; i <= LengthOfArray; i++)
            {
                Console.Write("{0} ", GuessedLetter);
            }
            Console.WriteLine("Enter a letter!");
            char letter = char.Parse(Console.ReadLine());
            if (words[random].Contains<char>(letter))
            {
                WordIsHidden = false;
                GuessedLetter = letter;
                Console.Write(letters);
            }
            else
            {
                 if (WordIsHidden == true)
                {
                    Console.Write("You guessed wrong!");

                }
            }

        }
    }
}

Also I'm trying to make the game show each letter, the user has guessed on it's corresponding position, but now the letter is one line higher than the rest of the word and it's not in it's right position.

Edited:

Here is the result :
cat
___Enter a letter!
a
__
aaaEnter a letter!
t
aa
tttEnter a letter!

IF anyone have a clue for where does this come from and how can I fix it, any help will be greatly appreciated.


Solution

  • Ok, I'm guessing at the problem here but I think you are describing an off by one error here:

    for (int i = 1; i <= LengthOfArray; i++)
    

    should be:

    for (int i = 0; i < LengthOfArray; i++)
    

    As indexing in C# starts at 0. This is probably causing the issue you are seeing. However a foreach loop here would be better as you are not using the value 'i' at all.

    foreach(var c in words)
      Console.Write(GuessedLetter);
    

    as for making the program shorter, don't worry too much right now, get the program working and then refactor later. I'd advice starting with looking into LINQ/IEnumerable extensions, also type inference via the var keyword.

    EDIT:

    Ok I spent 5 minutes looking over your code, I put in a couple of fixes (see comments). Please review against the original code. It is not a pretty, efficient or elegant solution but hopefully it behaves more inline with what you were expecting.

    using System;
    using System.Linq
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
            string[] words = System.IO.File.ReadAllLines(@"C:\Users\ADMIN\Desktop\Letters\Letters.txt");
            int LengthOfArray = words.Length;
            Random rnd = new Random();
            int random = rnd.Next(1, 3);
            char[] letters = words[random].ToCharArray();
            bool WordIsHidden = true;
            char hiddenChar = '_';
            //fix 1 use a list or hashset or similar to store the selected chars
            var guessed = new List<char>();
            var retry = true;
            while (retry = true)
            {
                Console.WriteLine(letters);
                //fix 2, loop over the letters, checking whether they have been guessed
                foreach(var c in letters)
                {
                    if (guessed.Contains(c))
                        Console.Write(c);
                    else
                        Console.Write("_");
                }
                Console.WriteLine("\nEnter a letter!");
                char letter = char.Parse(Console.ReadLine());
                if (words[random].Contains<char>(letter))
                {
                    WordIsHidden = false;
                    guessed.Add(letter);
                }
                else
                {
                    if (WordIsHidden == true)
                    {
                        guessed.Clear();
                        Console.WriteLine("You guessed wrong!");
                    }
                }
    
            }
        }
    };