Thursday, April 10, 2014

Q 2 CHAPTER 7 ACTIVITY BOOK HANGMAN GAME

Q 2) you need to genrrate 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,book name or movie name is extracted from a file containing a list of book names and user is asked to guess the name by typing one character at a time.Develop the hangman game application.

ans 2)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace game
{
    class Hangman
    {
        string randomString, userString;
        int dataLength;
        string category;
        string[] bookData = new string[10];
        string[] movieData = new string[10];
        int bookCount = 0, movieCount = 0;

        public Hangman()
        {
            FillNameValues();
        }

        private void FillNameValues()
        {
            FileStream fs = new FileStream("D:\\TextTest.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            string firstLine;
            StreamReader sRead=new StreamReader(fs);
            sRead.BaseStream.Seek(0,SeekOrigin.Begin);
            firstLine =sRead.ReadLine();

            while (firstLine!=null)
            {
                if(firstLine.Substring (0,1)=="B")
                {
                    int stringStartPos=firstLine.IndexOf(':');
                    bookData[bookCount]=firstLine.Substring(stringStartPos + 1);
                    bookCount++;
                }
                else
                {
                    int stringStartPos=firstLine.IndexOf(':');
                    movieData[movieCount]=firstLine.Substring(stringStartPos+1);
                    movieCount++;
                }
                firstLine=sRead.ReadLine();
            }
        }
        public int AcceptCategory()
        {
            Console.WriteLine("enter the catagory to play book/movie");
            category = Console.ReadLine();
            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];
            }
            else
            {
                int Rnd = RandGen.Next(0, movieCount - 1);
                randomString = movieData[Rnd];
            }
        }

        public void startGame()
        {
            dataLength = randomString .Length;
            char locateChar;
            int correcetCnt=0,inCorrectCnt=0;
            int i,k;
            char [] s=new char[randomString.Length];
            initializeUserString();
            showUserInputString();
            if(category =="BOOK")
            {
                Console.WriteLine("Thje total number of characters in the book are :{0}",randomString.Length);
                Console.WriteLine("The total number of characters you can enter to guess the name of books:{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(inCorrectCnt == dataLength || inCorrectCnt==dataLength)
                    break ;
                Console.WriteLine("Enter the character");
                locateChar=Convert.ToChar(Console.ReadLine().ToLower());
                int foundPos=0;
                int foundChar=0;
                foreach (char c in randomString)
                {
                    if(c==locateChar)
                    {
                        UpdateString(foundPos,locateChar.ToString());
                        k++;
                        foundChar=1;
                    }
                    foundPos++;
                }
                if(foundChar==0)
                {
                    inCorrectCnt++;
                }
                else
                {
                    correcetCnt++;
                }
                showUserInputString();
                Console.WriteLine("Total correct attempts {0}\t",correcetCnt);
                Console.WriteLine("Total incorrect attempts {0}\t",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 UpdateString(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 + 1));
                userString = beforeString + updatestr + afterString;
            }
            if (fpos == 0)
            {
                afterString = userString.Substring(fpos + 1, dataLength - (fpos + 1));
                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.Clear();
            Console.WriteLine("input value: {0} \n \n", userString);
        }}
    class game
    {
        static void Main(string[] args)
        {
            Console.Clear();
            Hangman obj = new Hangman();
            int returnVal = obj.AcceptCategory();
            if (returnVal == 1)
            {
                obj.startGame();
            }
            Console.ReadLine();

        }
    }
}




texttest



book:otheloo
book:beguiled
book:macbeth
book:frankenstein
book:time
movie:renaissance
movie:matrix
movie:adrift
movie:garfield
movie:narnia

No comments:

Post a Comment