Advertisement
SEBatt

Hangman in C# Console

Apr 9th, 2017
1,240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Hangman
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Random random = new Random(); //Make this first to ensure randomness!
  14.  
  15.             string ThisAnswer;
  16.             string Input;
  17.  
  18.             bool HasWon = false;
  19.  
  20.             // Make answers for the game to pick from, then choose one and get its length for the next part.
  21.             List<string> GameAnswers = new List<string> { "Apple", "Pear", "Banana", "Mango", "Apricot", "Plum" };
  22.             ThisAnswer = GameAnswers[random.Next(GameAnswers.Count)].ToUpper();
  23.             int length = ThisAnswer.Length;
  24.  
  25.             // Let's welcome our player and let them know the rules, as well as give them a clue for our answer.
  26.             Console.WriteLine("Welcome to Hangman!");
  27.             Console.WriteLine("Discover the word in 5 attempts by guessing using letters.");
  28.             string TellLength = "This word has: " + length + " letters. Good luck!";
  29.             Console.WriteLine(TellLength);
  30.  
  31.             List<string> GuessDisplay = new List<string>(ThisAnswer.Length);
  32.  
  33.             // Set up the underscores so we can show the player how many letters they need to guess.
  34.             for (int i = 0; i < ThisAnswer.Length; i++)
  35.             {
  36.                 GuessDisplay.Add("_ ");
  37.             }
  38.            
  39.             while (HasWon == false)
  40.             {
  41.                 // For each letter left to guess, show it to the player so they can see their progress.
  42.                 foreach (string letter in GuessDisplay)
  43.                 {
  44.                     Console.Write(letter);
  45.                 }
  46.  
  47.                 Console.WriteLine();
  48.  
  49.                 // Get the user's guess.
  50.                 Input = Console.ReadLine().ToUpper();
  51.  
  52.                 if (ThisAnswer.Contains(Input) == true) // If the letter appears in the answer, they're correct!
  53.                 {
  54.                     Console.WriteLine("Correct!"); // Let the player know how clever they are.
  55.                     char guess = Input[0];
  56.  
  57.                     // Now we'll go through each letter of the answer and check our player's answer against it.
  58.                     // If the guess and the letter matches, replace the guess display with the letter, so the player can see where they got it right.
  59.  
  60.                     for (int i = 0; i < ThisAnswer.Length; i++)
  61.                     {
  62.                         if (ThisAnswer[i].Equals(guess) == true)
  63.                         {
  64.                             GuessDisplay[i] = Input;
  65.                         }
  66.                     }
  67.  
  68.                     // If there's no more gaps left in the guess display, the player won!
  69.                     if (GuessDisplay.Contains("_ ") == false)
  70.                     {
  71.                         HasWon = true; // Tell the program they won.
  72.                     }
  73.                 }
  74.                 else
  75.                 {
  76.                     Console.WriteLine("Incorrect!");
  77.                 }            
  78.             }
  79.             // When the player has won, we leave the game loop and enter the end game preparation.
  80.             // This is also a good place to put the code that tells the player they lost the game.
  81.             // Think about how you'd code such a system!
  82.             if (HasWon == true)
  83.             {
  84.                 Console.WriteLine("YOU WON! Press any key to quit.");
  85.                 Console.ReadKey();
  86.                 System.Environment.Exit(0);
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement