dot_net

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:

1

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
rivatevoidchkGuessedLetter(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:

2

You will reach here:

3

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.

4

Leave a Reply