How To Make A Hangman Game In C#

hello everyone,everyone loves game except me so i thought to make a tutorial about making a hangman game in c# and it's command line application since its simple. i guess some people dont know what is hangman ok actually what is hangman ?
it is a paper and pencil guessing game for two or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers.Wikipedia

sounds like a interesting game :) , now lets start working on start a new project a console application and you will be falling in to the coding spot and add all the codes


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

//Author : Mohamed Shimran
//Blog : http://www.ultimateprogrammingtutorials.blogspot.com

namespace Hangman
{
  class Program
  {
        static void Main(string[] args)
    {
      Console.Title = "Hangman Game";
      Console.WriteLine("let's Hang!");
      while (true)
      {
        int wordLengthInt = -1;
        int numGuessesInt = -1;
        while (wordLengthInt == -1)
        {
       
          pickWordLength(ref wordLengthInt);
        }
        while (numGuessesInt == -1)
        {
         
          pickNumGuesses(ref numGuessesInt);
        }
               string word = pickWord(wordLengthInt);
            
        List guessedLetters = new List();
        bool solved = false;
        while (solved == false)
        {
          string wordToDisplay = displayWord(guessedLetters, word);
          if (!wordToDisplay.Contains("_"))
          {
            solved = true;
            Console.WriteLine("You Win!  The word was " + word);
                       checkIfPlayAgain();
          }
          else if (numGuessesInt <= 0)
          {
            solved = true;
            Console.WriteLine("You Lose!  The word was " + word);
            checkIfPlayAgain();
          }
          else
          {
            guessLetter(guessedLetters, word, wordToDisplay, ref numGuessesInt);
          }
        }
      }
    }

    static void pickWordLength(ref int wordLengthInt)
    {
      string wordLengthString = "";
      Console.WriteLine("Pick a word length");
      wordLengthString = Console.ReadLine();
      try
      {
        wordLengthInt = Convert.ToInt32(wordLengthString);
        if (!(wordLengthInt <= 20 & wordLengthInt >= 3)) {
          throw new Exception();
        }
      }
      catch (Exception)
      {
        wordLengthInt = -1;
        Console.WriteLine("Error: Invalid Word Length");
      }
    }

    static void pickNumGuesses(ref int numGuessesInt)
    {
      string numGuessesString = "";
      Console.WriteLine("Pick a number of guesses");
      numGuessesString = Console.ReadLine();
      try
      {
        numGuessesInt = Convert.ToInt32(numGuessesString);
        if (!(numGuessesInt <= 20 & numGuessesInt >= 1))
        {
          throw new Exception();
        }
      }
      catch (Exception)
      {
        numGuessesInt = -1;
        Console.WriteLine("Error: Invalid Number of Guesses");
      }
    }

    static string pickWord(int wordLengthInt)
    {
      string returnword = "";
      TextReader file = new StreamReader("WordList.txt");
      List wordList = new List();
      string fileLine = file.ReadLine();
      while (fileLine != null)
      {
        if (fileLine.Length == wordLengthInt)
        {
          wordList.Add(fileLine);
        }       
        fileLine = file.ReadLine();
      }
           
      Random randomGen = new Random();
      returnword = wordList[randomGen.Next(0,wordList.Count-1)];
      return returnword;
    }

    static string displayWord(List guessedCharacters, string word)
    {
      string returnedWord = "";
      if (guessedCharacters.Count == 0)
      {
        foreach (char letter in word)
        {
          returnedWord += "_ ";
        }
        return returnedWord;
      }
      foreach (char letter in word)
      {
        bool letterMatch = false;
        foreach (char character in guessedCharacters)
        {
          if (character == letter)
          {
            returnedWord += character + " ";
            letterMatch = true;
            break;
          }
          else
          {
            letterMatch = false;
          }
        }
        if (letterMatch == false)
        {
            returnedWord += "_ ";
        }
      }
      return returnedWord;
    }

    static void guessLetter(List guessedCharacters, string word, string wordToDisplay, ref int numGuessesLeft)
    {
      string letters = "";
      foreach (char letter in guessedCharacters)
      {
        letters += " " + letter;
      }
      Console.WriteLine("Guess a letter");
      Console.WriteLine("Guessed Letters: " + letters);
      Console.WriteLine("Guesses Left: " + numGuessesLeft);
      Console.WriteLine(wordToDisplay);
      string guess = Console.ReadLine();
      char guessedLetter = 'a';
      try
      {
        guessedLetter = Convert.ToChar(guess);
        if (!Char.IsLetter(guessedLetter))
        {
          throw new Exception();
        }
      }
      catch (Exception)
      {
        Console.WriteLine("Error: Invalid Letter Choice");
           }
      bool repeat = false;
      for (int i = 0; i < guessedCharacters.Count; i++)
      {
        if (guessedCharacters[i] == guessedLetter)
        {
          Console.WriteLine("Error: Invalid Letter Choice");
          repeat = true;
         
        }      }
      if (repeat == false)
      {
        guessedCharacters.Add(guessedLetter);
        numGuessesLeft -= 1;
      }     
    }

    static void checkIfPlayAgain()
    {
      Console.WriteLine("Would you like to play again? (y/n)");
      string playAgain = Console.ReadLine();
      if (playAgain == "n")
      {
        Environment.Exit(1);
      }     
    }

  }
}


now you need a wordlist for the game so download the worldlist here and save the wordlist.txt file in the debug file whatever the wordlist should be with the executable of the game after that just debug and play it

preview :


Post a Comment

Note: Only a member of this blog may post a comment.