C# A simple Hangman game
- by Radostin Angelov
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.