Advertisement
Guest User

Untitled

a guest
May 30th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Project
  5. {
  6. class MainClass
  7. {
  8. static List<string> wordList;
  9. static Random rnd;
  10.  
  11. public static void Main (string[] args)
  12. {
  13. wordList = new List<string>();
  14. rnd = new Random ();
  15.  
  16. bool playing = handleMenu ();
  17. while (playing)
  18. playing = handleMenu ();
  19. }
  20.  
  21. private static void pause() {
  22. Console.Write("Tryck en knapp för att gå vidare");
  23. Console.ReadKey ();
  24. }
  25.  
  26. private static void addWordToList() {
  27. Console.WriteLine ("Lägg till ord:");
  28. Console.Write ("> ");
  29. string word = Console.ReadLine ();
  30. word = word.ToLower ();
  31.  
  32. if (wordList.Contains (word))
  33. Console.WriteLine ("Ordet är redan inlagt!");
  34. else if (word.Trim() == String.Empty)
  35. Console.WriteLine ("Ordet får inte vara tomt");
  36. else
  37. wordList.Add (word);
  38.  
  39. pause ();
  40. }
  41.  
  42. private static void printWordList() {
  43. wordList.Sort ();
  44. foreach (string word in wordList) {
  45. Console.WriteLine (word);
  46. }
  47.  
  48. pause ();
  49. }
  50.  
  51. private static void printHangMan(int stage) {
  52. string line;
  53. System.IO.StreamReader file =
  54. new System.IO.StreamReader(String.Format("{0}.txt", stage));
  55. while((line = file.ReadLine()) != null)
  56. {
  57. Console.WriteLine (line);
  58. }
  59.  
  60. file.Close();
  61. }
  62.  
  63.  
  64. private static string generateHiddenWordLine(string hiddenWord, List<char> triedCharacters) {
  65. string result = "";
  66. foreach (char wordChar in hiddenWord) {
  67. if (triedCharacters.Contains(wordChar))
  68. result += wordChar.ToString();
  69. else
  70. result += "-";
  71. }
  72.  
  73. return result;
  74. }
  75.  
  76. private static void playGame() {
  77. /* Let user decide how many tries he should get */
  78. int amountOfGuesses = 0;
  79. try {
  80. Console.WriteLine("Ange antalet max gissningar:");
  81. Console.Write("> ");
  82. amountOfGuesses = Convert.ToInt32(Console.ReadLine ());
  83.  
  84. if (!(amountOfGuesses >= 1 && amountOfGuesses <= 30)) {
  85. Console.WriteLine("Antalet gissningar måste vara mellan 1 och 30");
  86. pause();
  87. return;
  88. }
  89. } catch (Exception ex) {
  90. Console.WriteLine ("Du måste ange ett numeriskt tal");
  91. pause ();
  92. return;
  93. }
  94.  
  95. /* Randomize a word to use */
  96. int r = rnd.Next (wordList.Count);
  97. string word = wordList [r];
  98.  
  99. int currentTry = 1;
  100. int badTries = 0;
  101. /* Play until a correct word was guessed or tries depleted */
  102. bool playing = true;
  103. List<char> triedCharacters = new List<char> ();
  104.  
  105. while (playing) {
  106. Console.Clear ();
  107.  
  108. /* Print hangman based on percent of bad tries */
  109. double foo = (float)badTries / (float)amountOfGuesses;
  110. //if (badTries == (amountOfGuesses - 1))
  111. // printHangMan (5);
  112. if (foo >= 0 && foo <= 0.25)
  113. printHangMan (1);
  114. else if (foo >= 0.26 && foo <= 0.5)
  115. printHangMan(2);
  116. else if (foo >= 0.51 && foo <= 0.75)
  117. printHangMan(3);
  118. else if (foo >= 0.76 && foo <= 0.99)
  119. printHangMan(4);
  120. else
  121. printHangMan(5);
  122.  
  123. /* Print statistics */
  124. Console.Write(String.Format (
  125. "Felgissningar: {0}/{1}\n" +
  126. "Gissade bokstäver: ",
  127. badTries,
  128. amountOfGuesses
  129. ));
  130.  
  131. /* Collect list of used characters */
  132. foreach (char triedChar in triedCharacters)
  133. Console.Write (triedChar.ToString ());
  134.  
  135. Console.Write ("\n"); // Insert line break
  136.  
  137. Console.WriteLine ("Aktuellt ord: " + generateHiddenWordLine (word, triedCharacters));
  138.  
  139. if (badTries == amountOfGuesses) {
  140. Console.WriteLine ("Du har förbrukat dina försök och förlorat spelet!");
  141. Console.WriteLine ("Ordet var:");
  142. Console.WriteLine (word);
  143. break;
  144. }
  145.  
  146. Console.Write ("Gissa bokstav: ");
  147. string guessedChar = Console.ReadLine ();
  148. guessedChar = guessedChar.ToLower ();
  149.  
  150. if (guessedChar.Length > 1) {
  151. if (guessedChar.Equals (word)) {
  152. Console.WriteLine ("DU VANN! GRATTIS");
  153. break;
  154. } else {
  155. badTries++;
  156. }
  157. } else if (guessedChar.Length == 1) {
  158. //if (triedCharacters.Contains (Char.Parse (guessedChar))) {
  159. if (word.Contains (guessedChar)) {
  160. triedCharacters.Add (Char.Parse (guessedChar));
  161.  
  162. if (generateHiddenWordLine (word, triedCharacters) == word) {
  163. Console.WriteLine ("DU VANN! GRATTIS");
  164. break;
  165. }
  166. } else {
  167. triedCharacters.Add (Char.Parse (guessedChar));
  168. badTries++;
  169. }
  170. }
  171.  
  172. //badTries++;
  173. }
  174. pause ();
  175. }
  176.  
  177. private static bool handleMenu() {
  178. Console.Clear();
  179. Console.Write(String.Format(
  180. "Hänga hen!\nBy Fredrik Lorensson\n\n" +
  181. " 1) Lägg till ord\n" +
  182. " 2) Lista alla ord\n" +
  183. " 3) Spela\n" +
  184. " 4) Avsluta\n\n" +
  185. "Välj 1-4: "
  186. ));
  187.  
  188. int menuChoice = Convert.ToInt32(Char.GetNumericValue(Console.ReadKey ().KeyChar));
  189. Console.Write ("\n");
  190. switch (menuChoice) {
  191. case 1:
  192. addWordToList ();
  193. break;
  194. case 2:
  195. printWordList ();
  196. break;
  197. case 3:
  198. playGame ();
  199. break;
  200. case 4:
  201. return false;
  202. }
  203.  
  204. return true;
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement