Vocabulary Enhancer (VocEnhancer) Application Using C#

Background

A student asked me how to create a Vocabulary Enhancer (VocEnhancer) application using C#. A VocEnhancer application is nothing but an application that selects words randomly from a specified multiple options, so in consideration of those requirements and to help the student I have decided to write this article.

Before developing the application let us understand the conditions of VocEnhancer application.
 
Case
 
XYZ Inc. is a software development company that specializes in creating educational games for children. After doing a survey of the current market, the marketing team found that there is a great demand for software that helps children to improve their English vocabulary. Therefore, the marketing team proposed that the company should develop a new game that helps sharpen English vocabulary of children. One of the game designers in the company has suggested a game called VocEnhancer.

Rules of VocEnhancer

  1. It is a single-player game.
  2. There are three levels in the game, Beginner, Average, and Expert. The players would choose from these levels depending on their vocabulary.
  3. Depending upon the level, a few characters from an English word appear on the screen. The total number of characters in the word, the number of chances to guess the word, the marks scored by the player, and the seconds elapsed also appear on the screen.
  4. The marks, number of chances to guess the word, and the time elapsed is to be updated on the screen.
  5. The player must guess each word within the given chances. If a player is unable to guess any five words in the given number of chances, the game should be over.

 Design Specifications

The design of the game should be as per the following specifications:

  • When the game starts, a menu should be displayed with the following options:
  • Level 1: Beginner.
  • Level 2: Average.
  • Level 3: Expert .
  • The player can choose any level. As the player selects the level, details of the highest score for that level should be displayed on the screen and then the game should start.
  • Each level has a different sets of words.

  • A few characters of a word are displayed on the screen each time. These characters are chosen randomly and are placed at the position where they occur in the word. The missing characters are indicated by blanks or underlines.

  • The number of characters displayed on the screen depends upon the length of the word to be guessed. If the word is long then the characters displayed should be more and vice versa.

  • The sequence of the words displayed should be random.

  • The number of chances to guess each word should be five more than the number of missing characters in the word.

  • The player gets one mark for guessing the correct word.

  • The total marks scored by the user should be displayed on the screen and the score needs to be updated with each word guessed correctly.

Now by using the design specification above let us start creating the VocEnhancer application using a C# console application using Visual Studio 2010 as in the following:
  1. Open Visual Studio from Start -> All programs -> Microsoft Visual Studio.
  2. Then go to to "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows" -> "Console application".
  3. After that specify the name such as VocEnhancer or whatever name you wish and the location of the project and click on the "OK" button. The new project is created.
 Now create the two text files for storing words, one is for entire words and the other is for the same guess words with underscore. I have created the two files in my system at the locations:
  • "C:\\Users\\vithal\\Desktop\\Level1words.txt";
  • "C:\\Users\\vithal\\Desktop\\guess.txt";

In the same way  you can create files for Level1 and Level2.

Note:
  • There is a need to create separate files for guessing words you can dynamically create them using C# string functions but to make it understandable for the student I have created separate files.

The words of these text files will look such as follows.

 
 
 Note:
  • Keep the guess words sequence the same as the answer words file.

Now right-click on VocEnhancer Solution Explorer and add the following class files.

  • hint_Words_utility.cs: for reading hint or guess words from text files.
  • CountUtility.cs: for counting the marks, chances and time.

Now the VocEnhancer  application Solution Explorer will look such as follows.

 
Now open the "hint_Words_utility class" file and write the following code:
  1. using System;  
  2. using System.IO;  
  3.   
  4. namespace VohEnhancer  
  5. {  
  6.     public class hint_Words_utility  
  7.     {  
  8.         public static int ReadWordsFromHintFile(string[] Hintwords)  
  9.         {  
  10.             //geting file path from system desktop location which i have created  
  11.             string guessfilename = "C:\\Users\\vithal\\Desktop\\guess.txt";  
  12.   
  13.             if (File.Exists(guessfilename) == false)  
  14.   
  15.                 return -1;  
  16.             //reading the text file words  
  17.             StreamReader guessRead = new StreamReader(guessfilename);  
  18.   
  19.             int cguessount = 0;  
  20.   
  21.             for (int i = 0; i < 50; i++)  
  22.             {  
  23.   
  24.                 if (guessRead.EndOfStream == true)  
  25.   
  26.                     break;  
  27.                 Hintwords[cguessount++] = guessRead.ReadLine();  
  28.   
  29.   
  30.             }  
  31.   
  32.             guessRead.Close();  
  33.   
  34.             return cguessount;  
  35.   
  36.         }  
  37.   
  38.   
  39.         public static string GetHints()  
  40.         {  
  41.   
  42.             string[] Hintwords = new string[50];  
  43.             int count = ReadWordsFromHintFile(Hintwords);  
  44.             Random r = new Random();//for getting the random words from text files  
  45.             int guessX = (int)(r.Next(count));  
  46.             String secretWord = Hintwords[guessX];  
  47.   
  48.             return secretWord; //returning the random words  
  49.         }  
  50.     }  

Now open the "CountUtility.cs" file where we have created some entities, in other words varibales, to do a count then write the following code in it.
  1. namespace VohEnhancer  
  2. {  
  3.    public static class CountUtility  
  4.     {  
  5.        private static int _SetChances = 5, _Setmarks=0;  
  6.   
  7.        public static int Setmarks  
  8.         {  
  9.             get { return _Setmarks; }  
  10.             set { _Setmarks = value; }  
  11.         }  
  12.   
  13.        public static int SetChances  
  14.         {  
  15.             get { return _SetChances; }  
  16.             set { _SetChances = value; }  
  17.         }  
  18.     }  

 Now open the "program.cs" file with the main functionality of the VocEnhancer application with the following code in it.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;    
  6. using System.Timers;                                                                 
  7. namespace VohEnhancer  
  8. {  
  9.     public  class Program  
  10.     {  
  11.         public static string NewLine = Environment.NewLine;  
  12.         public static string Space = "\t"+"\t"+"\t"+"\t";  
  13.         public static int Chances,Marks;  
  14.         
  15.        
  16.         static int ReadWordsFromFile(string[] words)  
  17.         {  
  18.             //getting path of Level1 words from the following location  
  19.             string filename = "C:\\Users\\vithal\\Desktop\\Level1words.txt";  
  20.        
  21.   
  22.             if (File.Exists(filename) == false)  
  23.   
  24.                 return -1;  
  25.             //reading the words from  file  
  26.             StreamReader s = new StreamReader(filename);  
  27.   
  28.             int count = 0;  
  29.   
  30.             for (int i = 0; i < 50; i++)  
  31.             {  
  32.   
  33.                 if (s.EndOfStream == true)  
  34.   
  35.                     break;  
  36.   
  37.                 words[count++] = s.ReadLine();  
  38.   
  39.             }  
  40.   
  41.             s.Close();  
  42.   
  43.             return count;  
  44.   
  45.         }  
  46.         static void Main(string[] args)  
  47.         {  
  48.             
  49.             GameMode();         
  50.   
  51.               
  52.         }  
  53.   
  54.         private static void Level1Mode()  
  55.         {  
  56.            //level1 game mode logic or as same you can do it for Level2 and Level3  
  57.                 Chances = CountUtility.SetChances;  
  58.                 Marks = CountUtility.Setmarks; ;  
  59.                 
  60.                 Console.WriteLine("Chances:" + " " + Chances + "\t" + "\t" + " Marks:" + " " + Marks+"\t"+"\t"+"\t"+"Time Elapsed:");  
  61.                 Console.WriteLine(NewLine + Space + "Welcome to VocEnhancer" + NewLine + NewLine + NewLine);  
  62.                 if (Chances == 0)  
  63.                 {  
  64.                     Console.Clear();  
  65.                     Console.WriteLine("Number of Chances Over,Please Press P to Play again or Press Q to Quit");  
  66.                     string Option = Console.ReadLine();  
  67.                     if (Option == "P" || Option == "p")  
  68.                     {  
  69.                         Console.Clear();  
  70.                         GameMode();  
  71.                          
  72.                     }  
  73.                                        
  74.                 }  
  75.                 string[] words = new string[50];  
  76.   
  77.                 int count = ReadWordsFromFile(words);  
  78.                 Random r = new Random();  
  79.   
  80.                 string hint = hint_Words_utility.GetHints();  
  81.                 int guessX = (int)(r.Next(count));  
  82.             //getting guess words  
  83.                 String secretWord = words[guessX];  
  84.              //getting the length of guess word  
  85.                 int numChars = secretWord.Length;  
  86.                 Console.Write(Space + hint + NewLine + NewLine + NewLine + NewLine + NewLine + NewLine);  
  87.                 Console.Write("\t" + "Total Charcters:" + " " + numChars + NewLine);  
  88.                 Console.WriteLine();  
  89.                 bool bGuessedCorrectly = false;  
  90.   
  91.                 Console.WriteLine("\t" + "Write flip for Next word: " + NewLine + NewLine + "\t" + "Guess:");  
  92.                 while (true & Chances > 0)  
  93.                 {  
  94.                     //for changing  word by wrtting flip  
  95.                     string choice = Console.ReadLine();  
  96.                     if (choice == "flip")  
  97.                     {  
  98.                         Console.Clear();  
  99.                         Level1Mode();  
  100.                       
  101.                     }  
  102.   
  103.                     if (choice == secretWord)  
  104.                     {  
  105.   
  106.                         bGuessedCorrectly = true;  
  107.   
  108.                         break;  
  109.   
  110.                     }  
  111.                     else  
  112.                     {  
  113.   
  114.                         bGuessedCorrectly = false;  
  115.   
  116.                         break;  
  117.   
  118.                     }  
  119.   
  120.                 }  
  121.   
  122.                 if (bGuessedCorrectly == false)  
  123.                 {  
  124.                     Chances--; //decreasing chances on every wrong attempt  
  125.                     CountUtility.SetChances = Chances;  
  126.                     Console.WriteLine(" your Guess Is Not correct"+NewLine);  
  127.                     Console.WriteLine("Please Enter to stay Playing");  
  128.                 }  
  129.   
  130.   
  131.                 else  
  132.                 {  
  133.                     Marks++; //counting marks for each correct answer  
  134.                     CountUtility.Setmarks = Marks;  
  135.                     Console.WriteLine(NewLine + "Congrats!" + NewLine);  
  136.                     Console.WriteLine("Please Enter to stay Playing....");  
  137.   
  138.                 }  
  139.                 Console.ReadLine();  
  140.   
  141.                 Console.Clear();  
  142.   
  143.                 Level1Mode();           
  144.              
  145.         }  
  146.         public static void GameMode()  
  147.         {  
  148.            
  149.                 Console.WriteLine(Space + "Welcome to VocEnhancer" + NewLine + NewLine + NewLine);  
  150.                 Console.WriteLine("Please Select Game Mode" + NewLine);  
  151.                 Console.WriteLine("\t" + "1) Press 1 for Level 1- Beginner" + NewLine);  
  152.                 Console.WriteLine("\t" + "2) Press 2 for Level 2-Average" + NewLine);  
  153.                 Console.WriteLine("\t" + "3) Press 3 for Level 3-Expert" + NewLine);  
  154.                 int Mode = int.Parse(Console.ReadLine());  
  155.             //for selecting the modes  
  156.                 switch (Mode)  
  157.                 {  
  158.                     case 1:  
  159.                         Console.Clear();  
  160.                        // Level1 Mode  
  161.                         Level1Mode();  
  162.                         break;  
  163.                     case 2:  
  164.                         Console.Clear();  
  165.                         // Level2 Mode,please write separate function as same Level1 with different file  
  166.                         Level1Mode();  
  167.                         break;  
  168.                     case 3:  
  169.                         Console.Clear();  
  170.                         // Level3 Mode,please write separate function as same Level1 with different file  
  171.                         Level1Mode();  
  172.                         break;  
  173.                     default:  
  174.                         Console.WriteLine("Please Select Game Mode between 1 to 3");  
  175.                         Console.ReadLine();  
  176.                         Console.Clear();  
  177.                         GameMode();  
  178.                         break;  
  179.                 }  
  180.             }  
  181.          
  182. //end of class  
  183.     }  

Now in the preceding class file we have written all the logic required to create the VocEnhancer application.

Note:
  • I have used the same function for all three modes, please use the same logic as level one but provide a different word file path for each function, I hope you will do that since you are smart enough for that.

We are now done with the code, now run the console application in any of the following ways:

  • Press F5
  • Directly clicking on the .exe file from the bin folder of  the VocEnhancer application.
  • Using any option you wish to and that you know

After running the VocEnhancer application the following Level selection console screen will be as:

 
 
Now select any mode from the above, I will select Level1 Mode, so I need to press 1 from my keyboard. The following console screen will be displayed with options and words to be guessed.


Now, if you want to change the word then write "flip" and press Enter, it will show the next word as:
 
 
Now enter an incorrect guess, it will show the following message:

 
And the chances will be reduced by one and after four incorrect attempts:

 
If you attempt five incorrect guesses then the VocEnhancer application will exit from mode and show the following message.
 
 
Now type "P" and press Enter because we want to play again. It will start again from the start of the application, now after guessing the correct option, it will show the following screen:
 
 
From the preceding screen it's clear that our application is working and after each correct answer, the marks are increased by one. I hope you have done it.

Note:

  • For detailed code please download the Zip file attached above.
  • Don't forget to create Text Files for words.
Request and suggestion for students
  • Please don't use the code as is, use the example above as a guide and try improve it so in the future and for your career it's useful for you.
  • Don't buy any project from an outsider, do it as your own skill, it will definitely be useful for getting a job quickly and easily.
  • Invest your time and money in technical skill improvement instead of investing money for buying projects and if possible choose Microsoft technology for your career.
  • For free guidance for any college project, you can contact with me or refer to the C# Corner site but don't buy any project in your final year.

Summary

I hope this application is useful for students. If you have any suggestion related to this article or if you want any additional requirements in the above application then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all