Advertisement
coding_giants

hangman basic

May 17th, 2023
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Hangman
  6. {
  7. class Program
  8. {
  9. static bool checkIfWin(string phrase, List<char> letterList)
  10. {
  11. foreach (char letter in phrase)
  12. {
  13. if (!letterList.Contains(letter) && letter != ' ')
  14. {
  15. return false;
  16. }
  17. }
  18. return true;
  19. }
  20.  
  21. static void showEnteredLetters(List<char> letterList)
  22. {
  23. Console.Write("The letters entered are: ");
  24. foreach (char letter in letterList)
  25. {
  26. Console.Write(letter + ", ");
  27. }
  28. Console.WriteLine();
  29. }
  30.  
  31. static void showPhrase(string phrase, List<char> letterList)
  32. {
  33. foreach (var letter in phrase)
  34. {
  35. if (letterList.Contains(letter))
  36. {
  37. Console.Write(letter);
  38. }
  39. else if (letter == ' ')
  40. {
  41. Console.Write(" ");
  42. }
  43. else
  44. {
  45. Console.Write("_ ");
  46. }
  47. }
  48. }
  49.  
  50. static void Main(string[] args)
  51. {
  52. List<string> phraseList = new List<string>();
  53.  
  54. phraseList.Add("hogwards legacy");
  55. phraseList.Add("god of war");
  56. phraseList.Add("fortnite");
  57. phraseList.Add("minecraft");
  58. phraseList.Add("animal crossing");
  59.  
  60. Random numberGenerator = new Random();
  61. int phraseCount = phraseList.Count;
  62. int drawnPhaseIndex = numberGenerator.Next(phraseCount);
  63. string phrase = phraseList[drawnPhaseIndex];
  64. List<char> enteredLettersList = new List<char>();
  65.  
  66. int guessesLeft = 5;
  67. bool isWin = false;
  68.  
  69. while (guessesLeft > 0 && isWin == false)
  70. {
  71. showPhrase(phrase, enteredLettersList);
  72. Console.WriteLine();
  73. showEnteredLetters(enteredLettersList);
  74. Console.WriteLine("You have " + guessesLeft + " guesses left");
  75. Console.Write("Enter a letter: ");
  76. char letterEntered = Console.ReadLine()[0];
  77. Console.Clear();
  78.  
  79. if (phrase.Contains(letterEntered))
  80. {
  81. enteredLettersList.Add(letterEntered);
  82. isWin = checkIfWin(phrase, enteredLettersList);
  83. }
  84. else
  85. {
  86. enteredLettersList.Add(letterEntered);
  87. guessesLeft--;
  88. }
  89.  
  90. }
  91.  
  92. if (isWin)
  93. {
  94. Console.WriteLine("You win!!!");
  95. }
  96. else
  97. {
  98. Console.WriteLine("Unfortunately, you didn't guess the phrase this time. The phrase is: " + phrase);
  99. }
  100. }
  101. }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement