Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Hangman
- {
- class Program
- {
- static bool checkIfWin(string phrase, List<char> letterList)
- {
- foreach (char letter in phrase)
- {
- if (!letterList.Contains(letter) && letter != ' ')
- {
- return false;
- }
- }
- return true;
- }
- static void showEnteredLetters(List<char> letterList)
- {
- Console.Write("The letters entered are: ");
- foreach (char letter in letterList)
- {
- Console.Write(letter + ", ");
- }
- Console.WriteLine();
- }
- static void showPhrase(string phrase, List<char> letterList)
- {
- foreach (var letter in phrase)
- {
- if (letterList.Contains(letter))
- {
- Console.Write(letter);
- }
- else if (letter == ' ')
- {
- Console.Write(" ");
- }
- else
- {
- Console.Write("_ ");
- }
- }
- }
- static void Main(string[] args)
- {
- List<string> phraseList = new List<string>();
- phraseList.Add("hogwards legacy");
- phraseList.Add("god of war");
- phraseList.Add("fortnite");
- phraseList.Add("minecraft");
- phraseList.Add("animal crossing");
- Random numberGenerator = new Random();
- int phraseCount = phraseList.Count;
- int drawnPhaseIndex = numberGenerator.Next(phraseCount);
- string phrase = phraseList[drawnPhaseIndex];
- List<char> enteredLettersList = new List<char>();
- int guessesLeft = 5;
- bool isWin = false;
- while (guessesLeft > 0 && isWin == false)
- {
- showPhrase(phrase, enteredLettersList);
- Console.WriteLine();
- showEnteredLetters(enteredLettersList);
- Console.WriteLine("You have " + guessesLeft + " guesses left");
- Console.Write("Enter a letter: ");
- char letterEntered = Console.ReadLine()[0];
- Console.Clear();
- if (phrase.Contains(letterEntered))
- {
- enteredLettersList.Add(letterEntered);
- isWin = checkIfWin(phrase, enteredLettersList);
- }
- else
- {
- enteredLettersList.Add(letterEntered);
- guessesLeft--;
- }
- }
- if (isWin)
- {
- Console.WriteLine("You win!!!");
- }
- else
- {
- Console.WriteLine("Unfortunately, you didn't guess the phrase this time. The phrase is: " + phrase);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement