Venciity

[Jumpstart C#] - HANGMAN

Jan 22nd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Hangman
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string[] fruits = { "apple", "pear", "apricot", "peach", "cherry", "melon",
  13.                                 "avocado","banana","coconut","mango" };
  14.  
  15.             Random random = new Random();
  16.             int index = random.Next(fruits.Length);
  17.  
  18.             string word = fruits[index];
  19.  
  20.             int guessesLeft = 5;
  21.  
  22.             bool[] isLetterShown = new bool[word.Length];
  23.  
  24.             Console.WriteLine("Guess the word!");
  25.             int guessedLetters = 0;
  26.  
  27.             while (true)
  28.             {
  29.                 Console.Write("Result: ");
  30.                 for (int i = 0; i < word.Length; i++)
  31.                 {
  32.                     if (isLetterShown[i])
  33.                         Console.Write(word[i]);
  34.                     else
  35.                         Console.Write(" _ ");
  36.                 }
  37.                 Console.WriteLine();
  38.  
  39.                 Console.Write("Letter: ");
  40.                 ConsoleKeyInfo keyInfo = Console.ReadKey();
  41.  
  42.                 Console.WriteLine();
  43.                 Console.WriteLine();
  44.  
  45.                 bool hit = false;
  46.                 for (int i = 0; i < word.Length; i++)
  47.                 {
  48.                     if (Char.ToLower(word[i]) == Char.ToLower(keyInfo.KeyChar))
  49.                     {
  50.                         hit = true;
  51.                         if (!isLetterShown[i])
  52.                         {
  53.                             guessedLetters++;
  54.                             isLetterShown[i] = true;
  55.                         }                    
  56.                     }
  57.                 }
  58.  
  59.                 if (!hit)
  60.                 {
  61.                     guessesLeft--;
  62.                     Console.WriteLine("No hits! You have {0} guesses left!", guessesLeft);
  63.                     Console.WriteLine();
  64.                 }
  65.  
  66.                 if (guessesLeft == 0)
  67.                 {
  68.                     Console.WriteLine("Game Over! X__X");
  69.                     Console.WriteLine("The word is: {0}", word);
  70.                     break;
  71.                 }
  72.  
  73.                 if (guessedLetters == word.Length)
  74.                 {
  75.                     Console.WriteLine("Congratulations! You win!");
  76.                     Console.WriteLine("The word is: {0}", word);
  77.                     break;
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment