Advertisement
AlexanderStefanov

HangmanCode

Sep 30th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication6
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Random random = new Random((int)DateTime.Now.Ticks);
  13.  
  14.             string[] wordBank = { "Blue", "Black", "Yellow", "Orange", "Green", "Purple" };
  15.  
  16.             string wordToGuess = wordBank[random.Next(0, wordBank.Length)];
  17.             string wordToGuessUppercase = wordToGuess.ToUpper();
  18.  
  19.             StringBuilder displayToPlayer = new StringBuilder(wordToGuess.Length);
  20.             for (int i = 0; i < wordToGuess.Length; i++)
  21.                 displayToPlayer.Append('_');
  22.  
  23.             List<char> correctGuesses = new List<char>();
  24.             List<char> incorrectGuesses = new List<char>();
  25.  
  26.             int lives = 5;
  27.             bool won = false;
  28.             int lettersRevealed = 0;
  29.  
  30.             string input;
  31.             char guess;
  32.  
  33.             while (!won && lives > 0)
  34.             {
  35.                 Console.Write("Guess a letter: ");
  36.  
  37.                 input = Console.ReadLine().ToUpper();
  38.                 guess = input[0];
  39.  
  40.                 if (correctGuesses.Contains(guess))
  41.                 {
  42.                     Console.WriteLine("You've already tried '{0}', and it was correct!", guess);
  43.                     continue;
  44.                 }
  45.                 else if (incorrectGuesses.Contains(guess))
  46.                 {
  47.                     Console.WriteLine("You've already tried '{0}', and it was wrong!", guess);
  48.                     continue;
  49.                 }
  50.  
  51.                 if (wordToGuessUppercase.Contains(guess))
  52.                 {
  53.                     correctGuesses.Add(guess);
  54.  
  55.                     for (int i = 0; i < wordToGuess.Length; i++)
  56.                     {
  57.                         if (wordToGuessUppercase[i] == guess)
  58.                         {
  59.                             displayToPlayer[i] = wordToGuess[i];
  60.                             lettersRevealed++;
  61.                         }
  62.                     }
  63.  
  64.                     if (lettersRevealed == wordToGuess.Length)
  65.                         won = true;
  66.                 }
  67.                 else
  68.                 {
  69.                     incorrectGuesses.Add(guess);
  70.  
  71.                     Console.WriteLine("There's no '{0}' in it!", guess);
  72.                     lives--;
  73.                 }
  74.  
  75.                 Console.WriteLine(displayToPlayer.ToString());
  76.             }
  77.  
  78.             if (won)
  79.                 Console.WriteLine("You won!");
  80.             else
  81.                 Console.WriteLine("You lost! It was '{0}'", wordToGuess);
  82.  
  83.             Console.Write("Press ENTER to exit...");
  84.             Console.ReadLine();
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement