Saturday, July 26, 2008

public void Disclaimer();

The 'Hangman' Code which I've pu it here has few errors..however these are minor and I vil rectify them soon..If ur good at programmin(c#)..then do it yourself..It needs a lot. of patience.

C# Code for 'Hangman'.

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

namespace ConsoleApplication21
{
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()
{
FillNameValue();
}
//stores movie names and book names in repective strings
private void FillNameValue()
{
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 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 AccdeptCategory()
{
Console.WriteLine("enter the category to play-book/movie");
Category=Console.ReadLine();
Category=Category.ToUpper();
if(Category!=Book&&Category!=Movie)
{
Console.WriteLine("invalid");
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 locateChat;
int correctCnt=0,inCorrectCnt=0;
int i,k;
char[] s=new char[randomString.Length];
InitializeUserString();
ShowUserInputString();
if(Category=="Book")
{
Console.WriteLine("the total charecyers in the book is:{0}",randomString.Length);
Console.WriteLine("the total charecters u can enter to guess the name of the book is:{0}", randomString.Length+2);
}
else
{
Console.WriteLine("the total charecyers in the movie is:{0}",randomString.Length);
Console.WriteLine("the total charecters u can enter to guess the name of the movie is:{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;
foreach(char c in randomString)
if(c==locateChar)
{
UpdateString(foundPos,locateChar.ToString());
k++;
foundChar=1;
}
foundPos++;
}
if(foundChar==0)
{
Console.WriteLine("wrong attempt");
inCorrectCnt++;
}
ShowUserInputString();
Console.WriteLine("Total correct attempts :{0}",correctCnt);
Console.WriteLine("Total incorrect attempts : {0}",inCorrectCnt);
if(k==dataLength)
{
break;
}
if(randomString==userString)
{
Console.WriteLine("You have won");
}
else
{
Console.WriteLine("The correct name is : {0}",randomString);
Console.WriteLine("You ve lost");
}
}
private void UpdateString(int fPos,string updateStr)
{
string beforeString,aftterString;
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(Pos==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{
userString=userString.Insert(i,"*");
}
}
public void ShowUserInputString()
{
Console.Clear();
Console.WriteLine("Input value : {0}",userString);
}

class Game
{
static void Main(string[] args)
{
Console.Clear();
Console.WriteLine("You have to complete the Game within 60sec");
Hangman obj=new Hangman();
int returnVal=obj.AcceptCategory();
if(returnVal==1)
{
//obj.StartGame();
Thread t=new Thread(new ThreadStart(obj.StartGame));
t.Start();
Thread.Sleep(60000);//Making the main thread sleep for 90sec
try
{
t.Abort();
Console.WriteLine("Time over");
}//killing th new thread
catch(ThreadAbortException e)
{
Console.WriteLine(e.Message);
}
}
Console.ReadLine();
}
}
}
}