Advertisement
IsakViste

Hangman: Random Words

Oct 15th, 2014
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.84 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3.  
  4. namespace GuessTheWord
  5. {
  6.     class MainClass
  7.     {
  8.         public static void Main (string[] args)
  9.         {
  10.             //Welcome Message
  11.             //Ask how many guesses the player wants
  12.             //amount of guesses == player input
  13.             //Loading Message
  14.             Console.WriteLine ("Hey, and welcome to this Hangman!");
  15.             System.Threading.Thread.Sleep(2000);
  16.             Console.Write ("How many guesses do you want? (10 recommended) ");
  17.             int amountOfGuesses = Convert.ToInt16 (Console.ReadLine ());
  18.             Console.WriteLine ("Please Wait...");
  19.  
  20.             //Start class Game with x amount of guesses
  21.             Game guessingGame = new Game (amountOfGuesses);
  22.             guessingGame.Initialization ();
  23.         }
  24.     }
  25.  
  26.     class Game
  27.     {
  28.         public Game (int guesses)
  29.         {
  30.             this.guesses = guesses;
  31.         }
  32.  
  33.         public int guesses {get; set;}
  34.  
  35.         //Declare Strings
  36.         public string wordToGuess;
  37.         public string guessedWord;
  38.         public string playerGuess;
  39.         public string alreadyGuessedLetters;
  40.         public string playAgainOrNot;
  41.  
  42.         //Declare Integers
  43.         public int numberOfLetters;
  44.         public int howManyGuessesDoYouWant;
  45.  
  46.         //Declare Booleans
  47.         public bool itIsThere;
  48.         public bool letterUsed;
  49.         public bool playAgain;
  50.  
  51.         public void Initialization ()
  52.         {
  53.             //Get the word to guess from the GetWord function
  54.             wordToGuess = GetWord ();
  55.  
  56.             //Put each letter of the word to uppercases
  57.             //Get the lenght of the word
  58.             wordToGuess = wordToGuess.ToUpper ();
  59.             numberOfLetters = wordToGuess.Length;
  60.  
  61.             //What you have guessed so far == "_" * the number of letters you have
  62.             //Start IsThereXLetter using the first letter of the word to guess
  63.             //=> check the positions of this letter and replace what's already there with the letter
  64.             //In this list of letters, add the first letter of the word to guess
  65.             guessedWord = new string ('_', numberOfLetters);
  66.             IsThereXLetter (wordToGuess [0]);
  67.             alreadyGuessedLetters = alreadyGuessedLetters + wordToGuess [0];
  68.  
  69.             //Clear the console, remove all text
  70.             //Write what the player has already guessed => the first letter + "_" for each other letter in the word
  71.             Console.Clear ();
  72.             Console.WriteLine ("Guess Me: " + guessedWord);
  73.  
  74.             //Start the Update function
  75.             Update ();
  76.         }
  77.  
  78.         public void Update ()
  79.         {
  80.             //As Long as this is true...
  81.             while (true)
  82.             {
  83.                 //If you don't have any more guesses...
  84.                 if (guesses == 0)
  85.                 {
  86.                     //Write the word you had to guess
  87.                     //Wait then clear the console
  88.                     Console.WriteLine ("You Lost!");
  89.                     Console.WriteLine ("The word was: " + wordToGuess);
  90.                     System.Threading.Thread.Sleep(4000);
  91.                     Console.Clear ();
  92.  
  93.                     //Start the DoYouWantToPlayAgain function...
  94.                     DoYouWantToPlayAgain ();
  95.                     if (playAgain == true)
  96.                     {
  97.                         //If you do want to play again...
  98.                         //Write-Ask how many guesses the player wants
  99.                         //amount of guesses == player input
  100.                         //Loading Message
  101.                         Console.Write ("How many guesses do you want? (10 recommended) ");
  102.                         howManyGuessesDoYouWant = Convert.ToInt16 (Console.ReadLine ());
  103.                         Console.WriteLine ("Please Wait...");
  104.  
  105.                         //Start class Game with x amount of guesses
  106.                         Game guessingGame = new Game (howManyGuessesDoYouWant);
  107.                         guessingGame.Initialization ();
  108.                     }
  109.                     else
  110.                     {
  111.                         //If you don't want to play again...
  112.                         //Exit the console
  113.                         Environment.Exit (1);
  114.                     }
  115.                 }
  116.  
  117.                 //If what you have guessed so far still contains "_" => Letters are still missing...
  118.                 if (guessedWord.Contains ("_"))
  119.                 {
  120.                     //Write-Ask to guess a letter
  121.                     //Put the letter to uppercase
  122.                     Console.Write ("Guess a Letter: ");
  123.                     playerGuess = Console.ReadLine ().ToUpper ();
  124.  
  125.                     //If the player input is only 1 letter long...
  126.                     if (playerGuess.Length == 1)
  127.                     {
  128.                         //Start IsThereXLetter using the letter the player inputed
  129.                         //=> check the positions of this letter and replace what's already there with the letter
  130.                         IsThereXLetter (playerGuess [0]);
  131.                         LetterAlreadyUsed ();
  132.  
  133.                         //If the letter the player inputed is correct, and this letter has not already been used...
  134.                         if (itIsThere == true && letterUsed == false)
  135.                         {
  136.                             //Write that the player is correct
  137.                             //Add the letter the player inputed to the list of letters which have already been guessed
  138.                             //Wait then clear the console
  139.                             Console.WriteLine ("Correct!\n");
  140.                             alreadyGuessedLetters = alreadyGuessedLetters + playerGuess [0];
  141.                             System.Threading.Thread.Sleep (1500);
  142.                             Console.Clear ();
  143.                         }
  144.                         //Else if the letter has already been used...
  145.                         else if (letterUsed == true)
  146.                         {
  147.                             //Write that the player has already used that letter
  148.                             //Wait then clear the console
  149.                             Console.WriteLine ("You've already guessed that letter. Try Again!\n");
  150.                             System.Threading.Thread.Sleep (1500);
  151.                             Console.Clear ();
  152.                         }
  153.                         //If none of the above are true
  154.                         // => If the letter is not there and has not already been used
  155.                         else
  156.                         {
  157.                             //Write that the player is wrong, and that he has x amount of guesses left
  158.                             //Add the letter the player inputed to the list of letters which have already been guessed
  159.                             //Wait then clear the console
  160.                             //Remove 1 from the amount of guesses
  161.                             Console.WriteLine ("Wrong! You have " + (guesses - 1) + " guesses left.\n");
  162.                             alreadyGuessedLetters = alreadyGuessedLetters + playerGuess [0];
  163.                             System.Threading.Thread.Sleep(1500);
  164.                             Console.Clear ();
  165.                             guesses--;
  166.                         }
  167.                     }
  168.                     //If the player input does not contain only 1 letter...
  169.                     else
  170.                     {
  171.                         //Write that you only accept 1 letter
  172.                         //Wait then clear the console
  173.                         Console.WriteLine ("Only 1 letter please.\n");
  174.                         System.Threading.Thread.Sleep(1500);
  175.                         Console.Clear ();
  176.                     }
  177.  
  178.                     //Write what the player has already guessed => the guessed letters + "_" for each other letter not found in the word
  179.                     Console.WriteLine ("Guess Me: " + guessedWord);
  180.                 }
  181.                 //If what you have guessed so far does not contain any "_" => You have found all the letters...
  182.                 else
  183.                 {
  184.                     //Write that the player has won
  185.                     //Wait the clear the console
  186.                     Console.WriteLine ("Congratulations, You've Won!" /*+ "\nThe word was: " + wordToGuess*/ + "\nYou had " + (guesses-1) + " guesses left");
  187.                     System.Threading.Thread.Sleep(4000);
  188.                     Console.Clear ();
  189.  
  190.                     //Start the DoYouWantToPlayAgain function...
  191.                     DoYouWantToPlayAgain ();
  192.                     if (playAgain == true)
  193.                     {
  194.                         //If you do want to play again...
  195.                         //Write-Ask how many guesses the player wants
  196.                         //amount of guesses == player input
  197.                         //Loading Message
  198.                         Console.Write ("How many guesses do you want? (10 recommended) ");
  199.                         howManyGuessesDoYouWant = Convert.ToInt16 (Console.ReadLine ());
  200.                         Console.WriteLine ("Please Wait...");
  201.  
  202.                         //Start class Game with x amount of guesses
  203.                         Game guessingGame = new Game (howManyGuessesDoYouWant);
  204.                         guessingGame.Initialization ();
  205.                     }
  206.                     else
  207.                     {
  208.                         //If you don't want to play again...
  209.                         //Exit the console
  210.                         Environment.Exit (1);
  211.                     }
  212.                 }
  213.  
  214.             }
  215.         }
  216.  
  217.         //IsThereXLetter Function
  218.         //Check if the the letter inputed (chatGettingGuessed) is in the word
  219.         //If it is, change it with where it should be
  220.         public void IsThereXLetter (char charGettingGuessed)
  221.         {
  222.             //Set the Positions to 0 by default
  223.             int wordToGuessStartPos = 0;
  224.             int wordToGuessCharPos = 0;
  225.  
  226.             //By default, the word does not contain the letter guessed, it is not there
  227.             itIsThere = false;
  228.  
  229.             //As long as the Start position is lower than the number of letters contained in the word
  230.             //AND the position of the guessed letter is bigger than -1
  231.             //(if it's -1, it means that it's not in the word, it does not have a position)
  232.             while ((wordToGuessStartPos < numberOfLetters) && (wordToGuessCharPos > -1))
  233.             {
  234.                 wordToGuessCharPos = wordToGuess.IndexOf (charGettingGuessed, wordToGuessStartPos);
  235.                 if (wordToGuessCharPos == -1)
  236.                 {
  237.                     //If the guessed letter is not found
  238.                     //Break out of the while loop = set it to false
  239.                     break;
  240.                 }
  241.                 //Set the Start Position to be, its current positon + 1
  242.                 //Change the player input/letter with its position in the word, without touching the other letters
  243.                 wordToGuessStartPos = wordToGuessCharPos+1;
  244.                 guessedWord = guessedWord.Substring (0, wordToGuessCharPos) + charGettingGuessed + guessedWord.Substring (wordToGuessCharPos + 1);
  245.  
  246.                 //Set that the word contain the letter guessed
  247.                 itIsThere = true;
  248.             }
  249.         }
  250.  
  251.         //LetterAlreadyUsed Function
  252.         //Check if the letter assigned has already been used
  253.         public void LetterAlreadyUsed ()
  254.         {
  255.             //If the the list of already guessed letters can't find the letter the player inputed
  256.             //Then set that the letter has not been used
  257.             //Else set that it has been used
  258.             if (alreadyGuessedLetters.IndexOf (playerGuess [0]) == -1)
  259.             {
  260.                 letterUsed = false;
  261.             }
  262.             else
  263.             {
  264.                 letterUsed = true;
  265.             }
  266.         }
  267.  
  268.         //DoYouWantToPlayAgain Function
  269.         //Ask if the player wants to play again, or not
  270.         public void DoYouWantToPlayAgain ()
  271.         {
  272.             //As long as this is true...
  273.             while (true)
  274.             {
  275.                 //Write-Ask if the player wants to play again
  276.                 //Get the player input, put it in uppercase letters only
  277.                 Console.Write ("Do you want to play again? ");
  278.                 playAgainOrNot = Console.ReadLine ().ToUpper ();
  279.  
  280.                 //If the player wants to play again...
  281.                 if (playAgainOrNot == "YES")
  282.                 {
  283.                     //Set that the player wants to play again
  284.                     //Wait then clear the console
  285.                     //Break out of the while loop = set it false
  286.                     Console.WriteLine ("Awesome!");
  287.                     playAgain = true;
  288.                     System.Threading.Thread.Sleep (2000);
  289.                     Console.Clear ();
  290.                     break;
  291.                 }
  292.                 //Else if the player does not want to play again...
  293.                 else if (playAgainOrNot == "NO")
  294.                 {
  295.                     //Set that the player does not want to play again
  296.                     //Wait then clear the console
  297.                     //Break out of the while loop = set it false
  298.                     Console.WriteLine ("BYE!");
  299.                     playAgain = false;
  300.                     System.Threading.Thread.Sleep (2000);
  301.                     Console.Clear ();
  302.                     break;
  303.                 }
  304.                 //If none of the above is true...
  305.                 //=> If the player input is not "yes" or "no"...
  306.                 else
  307.                 {
  308.                     //Write-Ask the player to try again
  309.                     //Wait then clear the console
  310.                     //Go back to the beginning of the while loop
  311.                     Console.WriteLine ("Please try again...");
  312.                     System.Threading.Thread.Sleep (2000);
  313.                     Console.Clear ();
  314.                 }
  315.             }
  316.         }
  317.  
  318.         //Get random word from a word list
  319.         public static string GetWord ()
  320.         {
  321.             WebClient wc = new WebClient ();
  322.             string wordList = wc.DownloadString ("https://raw.githubusercontent.com/Tom25/Hangman/master/wordlist.txt");
  323.             string[] words = wordList.Split ('\n');
  324.  
  325.             Random randomWordGen = new Random ();
  326.             return words [randomWordGen.Next (0, words.Length - 1)];
  327.         }
  328.     }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement