Code to Program - MasterXander/HangMan-Game GitHub Wiki
namespace Hang_Man_Game { class Program { static void Main(string[] args) { bool backtop = true; while (backtop) { Console.Clear();
ConsoleKeyInfo Cho;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Welcome to HangManGame!");
Console.SetCursorPosition(2, 2);
Console.WriteLine("Tryk på en af tasterne (P/Q)");
Console.SetCursorPosition(2, 4);
Console.WriteLine("[P] Play [Q] Quit");
Console.SetCursorPosition(4, 6);
Console.Write("Vælg funktion: ");
Cho = Console.ReadKey(true);
if (Cho.Key == ConsoleKey.P)
{
Console.Clear();
string[] randword = new string[10];
{
randword[0] = "skate";
randword[1] = "apple";
randword[2] = "computer";
randword[3] = "water";
randword[4] = "watermelon";
randword[5] = "ice";
randword[6] = "henrik";
randword[7] = "Power";
randword[8] = "bottle";
randword[9] = "bananasplit";
};
// randomly choose a word from the randword and assign it to a variable called choWord
Random random = new Random();
int wordListInt = random.Next(0, randword.Length - 1);
string choWord = randword[wordListInt].ToLower();
Console.WriteLine("The word has {0} characters", choWord.Length);
//down in the text (int remaiAttem = 7;) can you decide how many try’s you have
int remaiAttem = 7;
List<string> alreadyGuessedLetters = new List<string>();
string displayWord = "";
while (remaiAttem > 0 && displayWord != choWord)
{
Console.WriteLine("Remaining attempts: {0}", remaiAttem);
// ask the user to guess a letter and assign their answer it to a variable called
// (guess). i made guess lowercase.
Console.WriteLine("Enter a letter: ");
char[] input = Console.ReadLine().ToCharArray();
string guess = "";
if (input.Length > 0)
{
guess = input[0].ToString();
guess = guess.ToLower();
}
alreadyGuessedLetters.Add(guess);
//This check if the letter the user guessed (guess) is one of the letters in the chosen word (choWord)
displayWord = "";
string contaiMessa = "";
if (choWord.Contains(guess))
{
Console.Clear();
contaiMessa = "{0} is contained in the word.";
}
else
{
Console.Clear();
contaiMessa = "{0} is NOT contained in the word.";
remaiAttem--;
}
Console.WriteLine(contaiMessa, guess);
foreach (var letter in choWord)
{
if (alreadyGuessedLetters.Contains(letter.ToString()))
{
displayWord += letter;
}
else
{
displayWord += "*";
}
}
Console.WriteLine(displayWord);
Console.WriteLine();
}
if (remaiAttem == 0)
{
Console.WriteLine("You don't have any more attempts. You lose!");
Console.WriteLine("The word was: {0}", choWord);
Console.ReadKey();
}
else
{
Console.WriteLine("You have won the game!");
Console.ReadKey();
}
}
if (Cho.Key == ConsoleKey.Q)
{
Console.Clear();
ConsoleKeyInfo Qui;
Console.SetCursorPosition(2, 2);
Console.WriteLine("Are you sure you want to Quit?");
Console.SetCursorPosition(2, 4);
Console.WriteLine("[Y] Yes [N] No");
Console.SetCursorPosition(4, 6);
Console.Write("Vælg funktion: ");
Qui = Console.ReadKey(true);
if (Qui.Key == ConsoleKey.Y)
{
break;
}
if (Qui.Key == ConsoleKey.N)
{
//skal ikke være noget
}
}
}
backtop = false;
}
}
}