Sunday, 5 May 2013

You need to generate a program to play the Hangman game. The program asks a user to enter a category as Book or Movie. Based on the category provided, a book name or movie name is extracted from a file containing a list of movie and book names and the user is asked to guess the name by typing one character at a time. Develop the Hangman game application. Prerequisite: You will require the TextTest.txt file for this exercise. Click the TextTest.zip link to download the TextTest.txt file.


using System;
using System.IO;
namespace name_1
{
    public class Hangman
    {
        string randomString, userString;
        int dataLength;
        string Category;
        string[] bookData = new string[5];
        string[] movieData = new string[5];
        int bookCount = 0, movieCount = 0;
        public Hangman()
        {
            FillNameValues();
        }
        private void FillNameValues()
        {
            string firstLine;
            StreamReader sRead =new StreamReader("c:\\Texttest.txt");
            sRead.BaseStream.Seek (0,SeekOrigin.Begin);
            firstLine = sRead.ReadLine();
            while(firstLine != null)
            {
                if (firstLine.Substring (0,1) =="B")
                {
                    int stringStratPos=firstLine.IndexOf(':');
                    bookData[bookCount] = firstLine.Substring(stringStratPos+ 1);
                    bookCount++;
                }
                else
                {
                    int stringStartPos=firstLine.IndexOf(':');
                    movieData[movieCount]=firstLine.Substring(stringStartPos+1);
                    movieCount++;
                }
                firstLine=sRead.ReadLine();

           


    }
}
 public int AcceptCategory()
 {
     Console.WriteLine("enter the category to play-book/movie");
     Category=Category.ToUpper();
     if (Category!="BOOK"&& Category!="MOVIE")
     {
         Console.WriteLine("Invalid category.......\n");
         return 0;
     }
     else
     {
         ExtractName();
         return 1;
     }
 }
        public void ExtractName()
        {
             Random RandGen=new Random();
            if (Category=="BOOK")
            {
             
                int Rnd=RandGen.Next(0,bookCount-1);
                randomString=bookData[Rnd];
            }
        }
        public void StartGame()
        {
            dataLength=randomString.Length;
            char locatechar;
            int correctCnt=0,inCorrectCnt=0;
        int i,k;
        char [] s=new char[randomString.Length];
        InitializeUserString();
        showUserInputString();
        if (Category=="BOOK")
    {
        Console.WriteLine("the total number of characters in the Book:{0}",randomString.Length);
        Console.WriteLine("the total number of characters you can enter to guess the name of Book: {0}",randomString.Length+2);
    }
    else
{
    Console.WriteLine("the total number of characters in the Movie:{0}",randomString.Length);
     Console.WriteLine("the total number of characters you can enter to guess the name of Movie:{0}",randomString.Length+2);
 
}
    for (i =1,k=0;i<=dataLength+2||k==dataLength;i++)
{
 if (correctCnt == dataLength || inCorrectCnt ==dataLength)
    break;
    Console.WriteLine("Enter the char ");
    locatechar = Convert.ToChar(Console.ReadLine().ToLower());
    int foundPos=0;
    int foundChar=0;
        {
    if (c== locatechar)
{
    UpdateDtring (foundPos,locateche.ToString());
    k++;
    foundChar=1;
}
    foundPos++;
}
    if (foundChar == 0)
{
    inCorrectCnt++;}
    else
{
    correctCnt++;
}
    showUserInputString();
    Console.WriteLine("Total Correct Attempts:{0}\t",correctCnt);
    Console.WriteLine("Total InCorrect Attempts:{0}\n", inCorrectCnt);
    if (k == dataLength)
    break;
}
if (randomString == userString)
{
Console.WriteLine("You have Won \n");
}
else{
Console.WriteLine("The Correct name is {0}",randomString);
Console.WriteLine("You have lost\n");
}
}
private void UpdateDtring (int fPos, string updateStr)
{
string beforeString, afterString;
if(fPos!=0&&fPos!=dataLength-1)
{
if (fPos==1)
beforeString=userString.Substring(0,1);
else
beforeString=userString.Substring(0,fPos);
afterString=userString.Substring(fPos+1,dataLength-(fPos));
userString=updateStr+afterString;
}
if (fPos==dataLength-1)
{
beforeString.userString.Substring(0,fPos);
userString=beforeString+updateStr;
}    
}
public void InitializeUserString()
{
userString="           ";
for (int i=0;i<dataLength;i++)
{
userString=userString.Insert(i,"*");
}
}
public void showUserInputString()
{
Console.WriteLine("input value:{0}\n\n",userString);
}
    }
    class Game
{
string void Main (string[]args)
{
Console.Clear();
Hangman obj=new Hangman();
int returnVal=obj.AcceptCategory();
if (returnVal==1)
{
obj.StartGame();


Console.ReadLine();}
}
}
}

No comments:

Post a Comment