Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- namespace GuessTheWord
- {
- class MainClass
- {
- public static void Main (string[] args)
- {
- //Welcome Message
- //Ask how many guesses the player wants
- //amount of guesses == player input
- //Loading Message
- Console.WriteLine ("Hey, and welcome to this Hangman!");
- System.Threading.Thread.Sleep(2000);
- Console.Write ("How many guesses do you want? (10 recommended) ");
- int amountOfGuesses = Convert.ToInt16 (Console.ReadLine ());
- Console.WriteLine ("Please Wait...");
- //Start class Game with x amount of guesses
- Game guessingGame = new Game (amountOfGuesses);
- guessingGame.Initialization ();
- }
- }
- class Game
- {
- public Game (int guesses)
- {
- this.guesses = guesses;
- }
- public int guesses {get; set;}
- //Declare Strings
- public string wordToGuess;
- public string guessedWord;
- public string playerGuess;
- public string alreadyGuessedLetters;
- public string playAgainOrNot;
- //Declare Integers
- public int numberOfLetters;
- public int howManyGuessesDoYouWant;
- //Declare Booleans
- public bool itIsThere;
- public bool letterUsed;
- public bool playAgain;
- public void Initialization ()
- {
- //Get the word to guess from the GetWord function
- wordToGuess = GetWord ();
- //Put each letter of the word to uppercases
- //Get the lenght of the word
- wordToGuess = wordToGuess.ToUpper ();
- numberOfLetters = wordToGuess.Length;
- //What you have guessed so far == "_" * the number of letters you have
- //Start IsThereXLetter using the first letter of the word to guess
- //=> check the positions of this letter and replace what's already there with the letter
- //In this list of letters, add the first letter of the word to guess
- guessedWord = new string ('_', numberOfLetters);
- IsThereXLetter (wordToGuess [0]);
- alreadyGuessedLetters = alreadyGuessedLetters + wordToGuess [0];
- //Clear the console, remove all text
- //Write what the player has already guessed => the first letter + "_" for each other letter in the word
- Console.Clear ();
- Console.WriteLine ("Guess Me: " + guessedWord);
- //Start the Update function
- Update ();
- }
- public void Update ()
- {
- //As Long as this is true...
- while (true)
- {
- //If you don't have any more guesses...
- if (guesses == 0)
- {
- //Write the word you had to guess
- //Wait then clear the console
- Console.WriteLine ("You Lost!");
- Console.WriteLine ("The word was: " + wordToGuess);
- System.Threading.Thread.Sleep(4000);
- Console.Clear ();
- //Start the DoYouWantToPlayAgain function...
- DoYouWantToPlayAgain ();
- if (playAgain == true)
- {
- //If you do want to play again...
- //Write-Ask how many guesses the player wants
- //amount of guesses == player input
- //Loading Message
- Console.Write ("How many guesses do you want? (10 recommended) ");
- howManyGuessesDoYouWant = Convert.ToInt16 (Console.ReadLine ());
- Console.WriteLine ("Please Wait...");
- //Start class Game with x amount of guesses
- Game guessingGame = new Game (howManyGuessesDoYouWant);
- guessingGame.Initialization ();
- }
- else
- {
- //If you don't want to play again...
- //Exit the console
- Environment.Exit (1);
- }
- }
- //If what you have guessed so far still contains "_" => Letters are still missing...
- if (guessedWord.Contains ("_"))
- {
- //Write-Ask to guess a letter
- //Put the letter to uppercase
- Console.Write ("Guess a Letter: ");
- playerGuess = Console.ReadLine ().ToUpper ();
- //If the player input is only 1 letter long...
- if (playerGuess.Length == 1)
- {
- //Start IsThereXLetter using the letter the player inputed
- //=> check the positions of this letter and replace what's already there with the letter
- IsThereXLetter (playerGuess [0]);
- LetterAlreadyUsed ();
- //If the letter the player inputed is correct, and this letter has not already been used...
- if (itIsThere == true && letterUsed == false)
- {
- //Write that the player is correct
- //Add the letter the player inputed to the list of letters which have already been guessed
- //Wait then clear the console
- Console.WriteLine ("Correct!\n");
- alreadyGuessedLetters = alreadyGuessedLetters + playerGuess [0];
- System.Threading.Thread.Sleep (1500);
- Console.Clear ();
- }
- //Else if the letter has already been used...
- else if (letterUsed == true)
- {
- //Write that the player has already used that letter
- //Wait then clear the console
- Console.WriteLine ("You've already guessed that letter. Try Again!\n");
- System.Threading.Thread.Sleep (1500);
- Console.Clear ();
- }
- //If none of the above are true
- // => If the letter is not there and has not already been used
- else
- {
- //Write that the player is wrong, and that he has x amount of guesses left
- //Add the letter the player inputed to the list of letters which have already been guessed
- //Wait then clear the console
- //Remove 1 from the amount of guesses
- Console.WriteLine ("Wrong! You have " + (guesses - 1) + " guesses left.\n");
- alreadyGuessedLetters = alreadyGuessedLetters + playerGuess [0];
- System.Threading.Thread.Sleep(1500);
- Console.Clear ();
- guesses--;
- }
- }
- //If the player input does not contain only 1 letter...
- else
- {
- //Write that you only accept 1 letter
- //Wait then clear the console
- Console.WriteLine ("Only 1 letter please.\n");
- System.Threading.Thread.Sleep(1500);
- Console.Clear ();
- }
- //Write what the player has already guessed => the guessed letters + "_" for each other letter not found in the word
- Console.WriteLine ("Guess Me: " + guessedWord);
- }
- //If what you have guessed so far does not contain any "_" => You have found all the letters...
- else
- {
- //Write that the player has won
- //Wait the clear the console
- Console.WriteLine ("Congratulations, You've Won!" /*+ "\nThe word was: " + wordToGuess*/ + "\nYou had " + (guesses-1) + " guesses left");
- System.Threading.Thread.Sleep(4000);
- Console.Clear ();
- //Start the DoYouWantToPlayAgain function...
- DoYouWantToPlayAgain ();
- if (playAgain == true)
- {
- //If you do want to play again...
- //Write-Ask how many guesses the player wants
- //amount of guesses == player input
- //Loading Message
- Console.Write ("How many guesses do you want? (10 recommended) ");
- howManyGuessesDoYouWant = Convert.ToInt16 (Console.ReadLine ());
- Console.WriteLine ("Please Wait...");
- //Start class Game with x amount of guesses
- Game guessingGame = new Game (howManyGuessesDoYouWant);
- guessingGame.Initialization ();
- }
- else
- {
- //If you don't want to play again...
- //Exit the console
- Environment.Exit (1);
- }
- }
- }
- }
- //IsThereXLetter Function
- //Check if the the letter inputed (chatGettingGuessed) is in the word
- //If it is, change it with where it should be
- public void IsThereXLetter (char charGettingGuessed)
- {
- //Set the Positions to 0 by default
- int wordToGuessStartPos = 0;
- int wordToGuessCharPos = 0;
- //By default, the word does not contain the letter guessed, it is not there
- itIsThere = false;
- //As long as the Start position is lower than the number of letters contained in the word
- //AND the position of the guessed letter is bigger than -1
- //(if it's -1, it means that it's not in the word, it does not have a position)
- while ((wordToGuessStartPos < numberOfLetters) && (wordToGuessCharPos > -1))
- {
- wordToGuessCharPos = wordToGuess.IndexOf (charGettingGuessed, wordToGuessStartPos);
- if (wordToGuessCharPos == -1)
- {
- //If the guessed letter is not found
- //Break out of the while loop = set it to false
- break;
- }
- //Set the Start Position to be, its current positon + 1
- //Change the player input/letter with its position in the word, without touching the other letters
- wordToGuessStartPos = wordToGuessCharPos+1;
- guessedWord = guessedWord.Substring (0, wordToGuessCharPos) + charGettingGuessed + guessedWord.Substring (wordToGuessCharPos + 1);
- //Set that the word contain the letter guessed
- itIsThere = true;
- }
- }
- //LetterAlreadyUsed Function
- //Check if the letter assigned has already been used
- public void LetterAlreadyUsed ()
- {
- //If the the list of already guessed letters can't find the letter the player inputed
- //Then set that the letter has not been used
- //Else set that it has been used
- if (alreadyGuessedLetters.IndexOf (playerGuess [0]) == -1)
- {
- letterUsed = false;
- }
- else
- {
- letterUsed = true;
- }
- }
- //DoYouWantToPlayAgain Function
- //Ask if the player wants to play again, or not
- public void DoYouWantToPlayAgain ()
- {
- //As long as this is true...
- while (true)
- {
- //Write-Ask if the player wants to play again
- //Get the player input, put it in uppercase letters only
- Console.Write ("Do you want to play again? ");
- playAgainOrNot = Console.ReadLine ().ToUpper ();
- //If the player wants to play again...
- if (playAgainOrNot == "YES")
- {
- //Set that the player wants to play again
- //Wait then clear the console
- //Break out of the while loop = set it false
- Console.WriteLine ("Awesome!");
- playAgain = true;
- System.Threading.Thread.Sleep (2000);
- Console.Clear ();
- break;
- }
- //Else if the player does not want to play again...
- else if (playAgainOrNot == "NO")
- {
- //Set that the player does not want to play again
- //Wait then clear the console
- //Break out of the while loop = set it false
- Console.WriteLine ("BYE!");
- playAgain = false;
- System.Threading.Thread.Sleep (2000);
- Console.Clear ();
- break;
- }
- //If none of the above is true...
- //=> If the player input is not "yes" or "no"...
- else
- {
- //Write-Ask the player to try again
- //Wait then clear the console
- //Go back to the beginning of the while loop
- Console.WriteLine ("Please try again...");
- System.Threading.Thread.Sleep (2000);
- Console.Clear ();
- }
- }
- }
- //Get random word from a word list
- public static string GetWord ()
- {
- WebClient wc = new WebClient ();
- string wordList = wc.DownloadString ("https://raw.githubusercontent.com/Tom25/Hangman/master/wordlist.txt");
- string[] words = wordList.Split ('\n');
- Random randomWordGen = new Random ();
- return words [randomWordGen.Next (0, words.Length - 1)];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement