Skip to main content

Fruit Name Game Using Visual C#:



1.  Open Visual Studio.
2.  Create New Windows Form Application.
3.  Double click the Form1 on solution Explorer.
4.  In the Design Mode of Form1 right click > Properties changes its Text Property To “ GuessFruitName”  from property window.
5.  You can set its backcoloralso. Here it is Coral.
6.  Now Drag 2 textBoxes, 1 label and  1 Button Control To the form1 from Toolbox.
7.  Change the text of label to “Number of Remaining Gusses” ,button control  to “Next Fruit Name”  and textBox1 to “******”.


       Your Form Looks Like This:



 8.  Double click on form1 write the following code:





       public Form1()
        {
              InitializeComponent();
            }

       string[] fruitList = newstring[10];       

          privatevoid Form1_Load(object sender, EventArgs e)
            {
            fruitList[0] = "Orange";
            fruitList[1] = "Mango";
            fruitList[2] = "Apple";
            fruitList[3] = "Banana";
            fruitList[4] = "Guava";
            fruitList[5] = "Peach";
            fruitList[6] = "Pear";
            fruitList[7] = "Papaya";
            fruitList[8] = "Grapes";
            fruitList[9] = "Plum";

           }
         The above code simply creates the fruit list on form load.

  9.  Create following Method for Guessing the Fruit name Letter
   
   privatevoidchkGuessedLetter(stringfruitToGuess, stringguessedLetter)
       {
          tbWord = textBox2.Text;//Assigns the word typed in Textbox2 to string tbWord
           intstrLen = fruitToGuess.Length;
        intremainingGusses = int.Parse(textBox3.Text);
        int result = 0;
        int counter = 0;
        intfoundLenth = 0;
        stringnewChar = "";
        intCorrectGuessedCount = 0;

        for (inti = 0; i<strLen; i++)
          {
           result = fruitToGuess.IndexOf(guessedLetter, foundLenth, strLen - foundLenth);

          if (result != -1)
                {
             foundLenth = result + 1;
              counter++;
              newChar = fruitToGuess.Substring((result), 1);
                  tbWord = tbWord.Remove(result, 1);
              tbWord = tbWord.Insert(result, newChar);
              CorrectGuessedCount++;
                }
            }

          if (CorrectGuessedCount == 0)
            {
                remainingGusses = remainingGusses - 1;
                textBox3.Text = remainingGusses.ToString();
            }

         if (remainingGusses == 0)
            {
               MessageBox.Show("You Lost \n The word was " + fruitToGuess);

               New_Word.Enabled = true;
            }
          else
            {
                textBox2.Text = tbWord;

           if (tbWord == fruitToGuess)
                {
                   MessageBox.Show("Congrats!\n You Won!");

                   New_Word.Enabled = true;
                }
            }           
        }
         Also add following variables above form load event as Shown:

             int Count = 0;                        
             stringnextWord;
             privatestringtbWord;
              string[] fruitList = newstring[10];

             privatevoid Form1_Load(object sender, EventArgs e)
                 {
                      ……..
              ……

                     }

  10.  Now double click the Button. Here its Name is Set to “New_Word”:

      privatevoidNew_Word_Click(object sender, EventArgs e)
         {
         if (Count > 9)
            {
                Count = 0;
            }

        nextWord = fruitList[Count];  

            Count++;   

        New_Word.Enabled = false;

         if (nextWord.Length == 4)
            {
                textBox3.Text = "4";              
                textBox2.Text = "****";
           MessageBox.Show("It is 4 Letter Fruit Name");
            }




        elseif (nextWord.Length == 5)
            {
                textBox3.Text = "5";
                textBox2.Text = "*****";
              MessageBox.Show("It is 5 Letter Fruit Name");
            }
      elseif (nextWord.Length == 6)
            {
                textBox3.Text = "6";
                textBox2.Text = "******";
                MessageBox.Show("It is 6 Letter Fruit Name");
            }
        }

 11.  Change the “KeyPreview” Property of Form1 to True. Then click On Events Properties locate for “KeyDown” Event double Click there:




          You will reach here:


12.  Add following Code in KeyDown Event for retrieving the keypressed from keyboard:


         privatevoid Form1_KeyDown(object sender, KeyEventArgs e)
        {
         if (e.KeyCode == Keys.A)
            {
               chkGuessedLetter(nextWord.ToUpper(), Keys.A.ToString());
            }
         elseif (e.KeyCode == Keys.B)
            {
               chkGuessedLetter(nextWord.ToUpper(), Keys.B.ToString());
            }
         elseif (e.KeyCode == Keys.C)
            {
               chkGuessedLetter(nextWord.ToUpper(), Keys.C.ToString());
            }
         elseif (e.KeyCode == Keys.D)
            {
                chkGuessedLetter(nextWord.ToUpper(), Keys.D.ToString());
            }
          elseif (e.KeyCode == Keys.E)
            {
               chkGuessedLetter(nextWord.ToUpper(), Keys.E.ToString());
            }
         elseif (e.KeyCode == Keys.F)
            {
               chkGuessedLetter(nextWord.ToUpper(), Keys.F.ToString());
            }

 elseif (e.KeyCode == Keys.G)
            {
                chkGuessedLetter(nextWord.ToUpper(), Keys.G.ToString());
            }
         elseif (e.KeyCode == Keys.H)
            {
               chkGuessedLetter(nextWord.ToUpper(), Keys.H.ToString());
            }
        elseif (e.KeyCode == Keys.I)
            {
               chkGuessedLetter(nextWord.ToUpper(), Keys.I.ToString());
            }
        elseif (e.KeyCode == Keys.J)
            {
               chkGuessedLetter(nextWord.ToUpper(), Keys.J.ToString());
            }
        elseif (e.KeyCode == Keys.K)
            {
                  chkGuessedLetter(nextWord.ToUpper(), Keys.K.ToString());
            }
         elseif (e.KeyCode == Keys.L)
            {
                chkGuessedLetter(nextWord.ToUpper(), Keys.L.ToString());
            }
         elseif (e.KeyCode == Keys.M)
            {
                chkGuessedLetter(nextWord.ToUpper(), Keys.M.ToString());
            }
          elseif (e.KeyCode == Keys.N)
            {
               chkGuessedLetter(nextWord.ToUpper(), Keys.N.ToString());
            }
           elseif (e.KeyCode == Keys.O)
            {
                chkGuessedLetter(nextWord.ToUpper(), Keys.O.ToString());
            }
          elseif (e.KeyCode == Keys.P)
            {
                 chkGuessedLetter(nextWord.ToUpper(), Keys.P.ToString());
            }
          elseif (e.KeyCode == Keys.Q)
            {
                  chkGuessedLetter(nextWord.ToUpper(), Keys.Q.ToString());
            }
          elseif (e.KeyCode == Keys.R)
            {
                  chkGuessedLetter(nextWord.ToUpper(), Keys.R.ToString());
             }
          elseif (e.KeyCode == Keys.S)
            {
                   chkGuessedLetter(nextWord.ToUpper(), Keys.S.ToString());
            }
          elseif (e.KeyCode == Keys.T)
            {
                   chkGuessedLetter(nextWord.ToUpper(), Keys.T.ToString());
            }


elseif (e.KeyCode == Keys.U)
            {
                  chkGuessedLetter(nextWord.ToUpper(), Keys.U.ToString());
            }
       elseif (e.KeyCode == Keys.V)
            {
                chkGuessedLetter(nextWord.ToUpper(), Keys.V.ToString());
            }
       elseif (e.KeyCode == Keys.W)
            {
                chkGuessedLetter(nextWord.ToUpper(), Keys.W.ToString());
            }
       elseif (e.KeyCode == Keys.X)
            {
                chkGuessedLetter(nextWord.ToUpper(), Keys.X.ToString());
            }
        elseif (e.KeyCode == Keys.Y)
            {
                chkGuessedLetter(nextWord.ToUpper(), Keys.Y.ToString());
            }
        elseif (e.KeyCode == Keys.Z)
            {
                 chkGuessedLetter(nextWord.ToUpper(), Keys.Z.ToString());
            }
        }

    You are Finish Here!!! Run And Play.






Comments

Popular posts from this blog

Home automation using Arduino Board

Today’s homes require sophistication control in its different gadgets which are basically electronic appliances. This has revolutionized the area of home automation with respect to an increased level of affordability and simplicity through the integration of home appliances with smart phone and tablet connectivity. Smart phones are already feature-perfect and can be made to communicate to any other devices in an adhoc network with a connectivity options like Bluetooth . With the advent of mobile phones, Mobile applications development has seen a major outbreak. Utilizing the opportunity of automating tasks for a smart home, mobile phone commonly found in normal household can be joined in a temporary network inside a home with the electronic equipments. Android, by Google Inc. provides the platform for the development of the mobile applications for the Android devices . Home automation system is a mobile application developed using Android targeting its vast market which will be benefi...

Beyond the Chat Box: How Agentic AI is Quietly Automating Your Customer's Next Purchase Journey

We have all been there. You land on an e-commerce storefront, a generic chat bubble pops up in the bottom right corner, and a robot named "ShopBot" asks if you need help finding shoes. You type in a specific problem—say, your last order arrived damaged and you need a replacement shipped to a temporary holiday address. The chatbot instantly gives you a link to the standard returns policy page. You are stuck in a dead-end conversational loop. For years, e-commerce has leaned on these traditional chatbots to deflect customer service tickets. But in 2026, a fundamental structural shift is happening. E-commerce is moving away from reactive, prompt-based chat interfaces and entering the era of Agentic Commerce (McKinsey, 2026). If you are still treating AI as a glorified answering machine, you aren't just falling behind—you are optimizing for an outdated version of the internet. Here is what separates traditional chatbots from agentic AI systems, and how the shift is changi...

Tips for Building Android Mobile and Tablet Apps

1. Check Popular Apps Before you actually start writing your own apps, you definitely want to resear ch the market if there are already exisiting apps providing the planned features. If there are, then take notes about what they provide and ask yourself if you are confident in doing better. 2. Model After Existing Sample Codes If you installed the Android SDK, there are several great sample projects inside sdk/extras/android/support/samples showcasing almost all the user interface examples and major functionalities, which you can use as templates to build and modify for your apps. 3. Avoid Undocumented APIs Some code worked at first but after a few Android SDK updates, it stopped working. 4. Layout for Device Orientation and Various Screen Sizes Since devices have embedded orientation sensors and they are usually defaulted to automatic mode, we need to account for that to provide a better user experience.