EDN Admin
Well-known member
Here you have to crate two text files named "BookList.txt" and "MovieList.txt" respectivly.Which has the list of names in string.Seprate each name by enter.Space is allowed. Following are the classes designed by me.Kindly give me as many suggestions you can to improve my coding skills.Give refference material if you need itNecessary.
Program.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace HangmanGame
{
class Program
{
public enum MainMenuSelection { movie, book ,invalidInput};
public static MainMenuSelection mainUserSelection()
{
int inputFromUser=0;
//Taking Input from User.
try
{
inputFromUser = Convert.ToInt32(Console.ReadLine());
if ( Char.IsDigit(Convert.ToChar(inputFromUser)))
throw (new InvalidInputUserException("Please Enter number only."));
if (inputFromUser == 1)
return MainMenuSelection.movie;
else if (inputFromUser == 2)
return MainMenuSelection.book;
else
return MainMenuSelection.invalidInput;
}
catch (FormatException exception)
{
Console.WriteLine(exception.Message);
return MainMenuSelection.invalidInput;
}
catch (InvalidInputUserException exception)
{
Console.WriteLine(exception.Message);
return MainMenuSelection.invalidInput;
}
}
static void Main(string[] args)
{
Game game = new Game(3);
//Selecting random string from file.
string originalString;
//Creating random blanks in string.
string encodedString;
Console.WriteLine("---Welcome to Hangman GAME---");
Console.WriteLine("Press 1 to Play with the movie names");
Console.WriteLine("Press 2 to Play with the book names");
MainMenuSelection inputFromUser;
inputFromUser= mainUserSelection();
for (int i = 0; i < 5; i++)
{
if (inputFromUser == MainMenuSelection.book)
{
//Selecting random string from file.
originalString = game.SelectRandomStringFromList(game.bookList);
//Creating random blanks in string.
encodedString = game.CreateRandomBlanks(originalString);
}
else if (inputFromUser == MainMenuSelection.movie)
{
//Selecting random string from file.
originalString = game.SelectRandomStringFromList(game.movieList);
//Creating random blanks in string.
encodedString = game.CreateRandomBlanks(originalString);
}
else
{
Console.WriteLine("Please Enter Valid input!");
break;
}
Console.WriteLine(encodedString);
bool result = game.playGame(encodedString, originalString);
if (result == true)
{
Console.WriteLine("Correct answer ! Press any key to continue...");
}
else
{
Console.WriteLine("Game Over! Press any key to exit.");
Console.ReadLine();
break;
}
Console.ReadLine();
Console.Clear();
}
Console.WriteLine("Your final Score is : {0} . Press any key to Exit from Game.", game.ScoreOfUser);
Console.ReadLine();
}
}
}
NotCharacterException.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HangmanGame
{
public class NotCharacterException : SystemException
{
private string p;
public NotCharacterException(string message)
: base(message)
{
}
}
}
InvalidInputUserException.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HangmanGame
{
class InvalidInputUserException : SystemException
{
public InvalidInputUserException(string message)
: base(message)
{
}
}
}
Game.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace HangmanGame
{
class Game
{
//Note:You will see better justification in fullscreen mode.Fullscreen mode = ALT+SHIFT+ENTER.
int chanceOfUser;
public int ScoreOfUser { get; private set; }
public List<string> bookList;
public List<string> movieList;
public Game(int chance)
{
this.chanceOfUser = chance;
bookList = new List<string>();
movieList = new List<string>();
//Calling function which will copy all the entries of file into list.
CopyFilesToList(bookList, "bookList.txt");
CopyFilesToList(movieList, "movieList.txt");
//CopyFilesToList(refferenceOfList,"NameOfTheFile.Extension");
}
public void CopyFilesToList(List<string> refferenceList, string filename)
{
FileStream fileStream=null;
StreamReader streamReader=null;
try
{
fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
streamReader = new StreamReader(fileStream);
string lineFromFile;
lineFromFile = streamReader.ReadLine();
// Converting string to upper case.
lineFromFile = lineFromFile.ToUpper();
while (lineFromFile != null)//Reading untile EOF in file.
{
//Adding name of Movie or Book from file into the reffered list.
refferenceList.Add(lineFromFile);
lineFromFile = streamReader.ReadLine();
//Converting string to upper case.
if (lineFromFile != null)
lineFromFile = lineFromFile.ToUpper();
}
}
catch (FileNotFoundException)
{
streamReader.Close();
fileStream.Close();
Console.WriteLine("The file :{0} not found.", filename);
}
catch (FileLoadException)
{
streamReader.Close();
fileStream.Close();
Console.WriteLine("There is some error.Unable to load file : {0}", filename);
}
}
public string SelectRandomStringFromList(List<string> refferenceList)
{
Random random = new Random();
int indexValue = random.Next(0, refferenceList.Count);
//random.Next(1,5)-> Will return random integer from the range 1 to 5.
//Returning string from refferenceList by random IndexValue.
return refferenceList[indexValue];
}
public string CreateRandomBlanks(string inputString)
{
Random randomValue = new Random();
int gapeOfBlank = randomValue.Next(2, inputString.Length);
//random.Next(1,5)-> Will return random integer from the range 1 to 5.
//inputString is "HELLO" if gapeOfBlank =1 ----> return "_E_L_"
//inputString is "HELLO" if gapeOfBlank =2 ----> return "_EL_O"
for (int i = 0; i < inputString.Length; i += gapeOfBlank)
{
//Replacing character from string with _
inputString = inputString.Remove(i, 1);
inputString = inputString.Insert(i, "_");
}
return inputString;
}
int CountBlanksFromString(string inputString)
{
int numberOfEstricInString = 0;
foreach (char loopItrator in inputString)
{
if (loopItrator == _)
numberOfEstricInString++;
}
return numberOfEstricInString;
}
public bool playGame(string encodedString, string originalString)
{
int NumberOfBlanksInString = CountBlanksFromString(encodedString);
char guessedCharacter =b ; //<----The character which is guessed by Player in game.
for (int i = 0; i < (NumberOfBlanksInString + chanceOfUser); i++)
{
//Taking input of character from User.
Console.WriteLine("Enter Guessed Character:");
try
{
guessedCharacter = Convert.ToChar(Console.ReadLine());
if (!Char.IsLetterOrDigit(guessedCharacter))
{
throw (new NotCharacterException("Please input Character or digit only!"));
}
guessedCharacter = Convert.ToChar((guessedCharacter.ToString()).ToUpper());
foreach (char CharacterItrator in encodedString)
{
if (CharacterItrator == _)
{
int indexOfEstric = encodedString.IndexOf(CharacterItrator);
/*Checking the position of the Blank in the origialString with guessed input.
* Example: "LAGAAN" is encoded string. If user guess "A" then there are three "A" in string.
* For avoiding errors we also check the perticular position of gueesed character with
* Blank(_) in original string*/
if (originalString[indexOfEstric] == guessedCharacter)
{
//Replacing the _ with the original character.
encodedString = encodedString.Remove(indexOfEstric, 1);
encodedString = encodedString.Insert(indexOfEstric, Convert.ToString(guessedCharacter));
ScoreOfUser += 2;
Console.WriteLine("Correct !");
Console.Clear();
Console.WriteLine(encodedString);
//If two strings are equal then instant return.
if (originalString == encodedString)
{
Console.WriteLine("Your Score is: {0}", ScoreOfUser);
return true; // The whole string is guessed.
}
else
{
break; // Breaking the foreach-loop.
}
}
else
{
chanceOfUser--;
if (chanceOfUser == -1)//<-- When no chance are remaining and the game is over.
{
return false;
}
else
{
Console.WriteLine("Wrong answer ! {0} Chance remaining.", chanceOfUser);
break;//Breacking the foreach-loop.
}
}
}
}
}
catch (NotCharacterException exception)
{
Console.WriteLine(exception.Message);
}
catch (FormatException exception)
{
Console.WriteLine(exception.Message);
}
}
//Finally again checking both of the strings.
if (originalString == encodedString)
return true;
else
return false;
}
}
}
View the full article
Program.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace HangmanGame
{
class Program
{
public enum MainMenuSelection { movie, book ,invalidInput};
public static MainMenuSelection mainUserSelection()
{
int inputFromUser=0;
//Taking Input from User.
try
{
inputFromUser = Convert.ToInt32(Console.ReadLine());
if ( Char.IsDigit(Convert.ToChar(inputFromUser)))
throw (new InvalidInputUserException("Please Enter number only."));
if (inputFromUser == 1)
return MainMenuSelection.movie;
else if (inputFromUser == 2)
return MainMenuSelection.book;
else
return MainMenuSelection.invalidInput;
}
catch (FormatException exception)
{
Console.WriteLine(exception.Message);
return MainMenuSelection.invalidInput;
}
catch (InvalidInputUserException exception)
{
Console.WriteLine(exception.Message);
return MainMenuSelection.invalidInput;
}
}
static void Main(string[] args)
{
Game game = new Game(3);
//Selecting random string from file.
string originalString;
//Creating random blanks in string.
string encodedString;
Console.WriteLine("---Welcome to Hangman GAME---");
Console.WriteLine("Press 1 to Play with the movie names");
Console.WriteLine("Press 2 to Play with the book names");
MainMenuSelection inputFromUser;
inputFromUser= mainUserSelection();
for (int i = 0; i < 5; i++)
{
if (inputFromUser == MainMenuSelection.book)
{
//Selecting random string from file.
originalString = game.SelectRandomStringFromList(game.bookList);
//Creating random blanks in string.
encodedString = game.CreateRandomBlanks(originalString);
}
else if (inputFromUser == MainMenuSelection.movie)
{
//Selecting random string from file.
originalString = game.SelectRandomStringFromList(game.movieList);
//Creating random blanks in string.
encodedString = game.CreateRandomBlanks(originalString);
}
else
{
Console.WriteLine("Please Enter Valid input!");
break;
}
Console.WriteLine(encodedString);
bool result = game.playGame(encodedString, originalString);
if (result == true)
{
Console.WriteLine("Correct answer ! Press any key to continue...");
}
else
{
Console.WriteLine("Game Over! Press any key to exit.");
Console.ReadLine();
break;
}
Console.ReadLine();
Console.Clear();
}
Console.WriteLine("Your final Score is : {0} . Press any key to Exit from Game.", game.ScoreOfUser);
Console.ReadLine();
}
}
}
NotCharacterException.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HangmanGame
{
public class NotCharacterException : SystemException
{
private string p;
public NotCharacterException(string message)
: base(message)
{
}
}
}
InvalidInputUserException.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HangmanGame
{
class InvalidInputUserException : SystemException
{
public InvalidInputUserException(string message)
: base(message)
{
}
}
}
Game.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace HangmanGame
{
class Game
{
//Note:You will see better justification in fullscreen mode.Fullscreen mode = ALT+SHIFT+ENTER.
int chanceOfUser;
public int ScoreOfUser { get; private set; }
public List<string> bookList;
public List<string> movieList;
public Game(int chance)
{
this.chanceOfUser = chance;
bookList = new List<string>();
movieList = new List<string>();
//Calling function which will copy all the entries of file into list.
CopyFilesToList(bookList, "bookList.txt");
CopyFilesToList(movieList, "movieList.txt");
//CopyFilesToList(refferenceOfList,"NameOfTheFile.Extension");
}
public void CopyFilesToList(List<string> refferenceList, string filename)
{
FileStream fileStream=null;
StreamReader streamReader=null;
try
{
fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
streamReader = new StreamReader(fileStream);
string lineFromFile;
lineFromFile = streamReader.ReadLine();
// Converting string to upper case.
lineFromFile = lineFromFile.ToUpper();
while (lineFromFile != null)//Reading untile EOF in file.
{
//Adding name of Movie or Book from file into the reffered list.
refferenceList.Add(lineFromFile);
lineFromFile = streamReader.ReadLine();
//Converting string to upper case.
if (lineFromFile != null)
lineFromFile = lineFromFile.ToUpper();
}
}
catch (FileNotFoundException)
{
streamReader.Close();
fileStream.Close();
Console.WriteLine("The file :{0} not found.", filename);
}
catch (FileLoadException)
{
streamReader.Close();
fileStream.Close();
Console.WriteLine("There is some error.Unable to load file : {0}", filename);
}
}
public string SelectRandomStringFromList(List<string> refferenceList)
{
Random random = new Random();
int indexValue = random.Next(0, refferenceList.Count);
//random.Next(1,5)-> Will return random integer from the range 1 to 5.
//Returning string from refferenceList by random IndexValue.
return refferenceList[indexValue];
}
public string CreateRandomBlanks(string inputString)
{
Random randomValue = new Random();
int gapeOfBlank = randomValue.Next(2, inputString.Length);
//random.Next(1,5)-> Will return random integer from the range 1 to 5.
//inputString is "HELLO" if gapeOfBlank =1 ----> return "_E_L_"
//inputString is "HELLO" if gapeOfBlank =2 ----> return "_EL_O"
for (int i = 0; i < inputString.Length; i += gapeOfBlank)
{
//Replacing character from string with _
inputString = inputString.Remove(i, 1);
inputString = inputString.Insert(i, "_");
}
return inputString;
}
int CountBlanksFromString(string inputString)
{
int numberOfEstricInString = 0;
foreach (char loopItrator in inputString)
{
if (loopItrator == _)
numberOfEstricInString++;
}
return numberOfEstricInString;
}
public bool playGame(string encodedString, string originalString)
{
int NumberOfBlanksInString = CountBlanksFromString(encodedString);
char guessedCharacter =b ; //<----The character which is guessed by Player in game.
for (int i = 0; i < (NumberOfBlanksInString + chanceOfUser); i++)
{
//Taking input of character from User.
Console.WriteLine("Enter Guessed Character:");
try
{
guessedCharacter = Convert.ToChar(Console.ReadLine());
if (!Char.IsLetterOrDigit(guessedCharacter))
{
throw (new NotCharacterException("Please input Character or digit only!"));
}
guessedCharacter = Convert.ToChar((guessedCharacter.ToString()).ToUpper());
foreach (char CharacterItrator in encodedString)
{
if (CharacterItrator == _)
{
int indexOfEstric = encodedString.IndexOf(CharacterItrator);
/*Checking the position of the Blank in the origialString with guessed input.
* Example: "LAGAAN" is encoded string. If user guess "A" then there are three "A" in string.
* For avoiding errors we also check the perticular position of gueesed character with
* Blank(_) in original string*/
if (originalString[indexOfEstric] == guessedCharacter)
{
//Replacing the _ with the original character.
encodedString = encodedString.Remove(indexOfEstric, 1);
encodedString = encodedString.Insert(indexOfEstric, Convert.ToString(guessedCharacter));
ScoreOfUser += 2;
Console.WriteLine("Correct !");
Console.Clear();
Console.WriteLine(encodedString);
//If two strings are equal then instant return.
if (originalString == encodedString)
{
Console.WriteLine("Your Score is: {0}", ScoreOfUser);
return true; // The whole string is guessed.
}
else
{
break; // Breaking the foreach-loop.
}
}
else
{
chanceOfUser--;
if (chanceOfUser == -1)//<-- When no chance are remaining and the game is over.
{
return false;
}
else
{
Console.WriteLine("Wrong answer ! {0} Chance remaining.", chanceOfUser);
break;//Breacking the foreach-loop.
}
}
}
}
}
catch (NotCharacterException exception)
{
Console.WriteLine(exception.Message);
}
catch (FormatException exception)
{
Console.WriteLine(exception.Message);
}
}
//Finally again checking both of the strings.
if (originalString == encodedString)
return true;
else
return false;
}
}
}
View the full article