In this tutorial I will show how to create a simple game, called Word Scramblers. The main game idea is: a word is given, then the letters are mixed and the player must guess the word.
In my project, a word library is stored in a XML file. Plus, to make the game a little bit easier, I will include some word definitions, so the file contains both words and their definitions.
Special Tutorial Requirements:- C# IDE (Visual Studio 2008 used in this tutorial)
- .NET Framework 1.0
So, here we go.
1. Create a standard C# Windows Form application:
2. On the form put two Label controls, a TextBox control and two Button controls:
Seems pretty easy. Now, let's pass to the code.
As the words and definitions are stored in a XML file, I will declare an additional namespace (System.Xml):
csharp
The first word will be loaded at the same time, while the form starts, so I will put the word generation code directly in the Form1_Load event:
csharp
private void Form1_Load(object sender, EventArgs e)
{
// The XML document that contains the words.
XmlDocument wordsDoc = new XmlDocument();
// This opens the XML document.
wordsDoc.Load(Application.StartupPath + "\\words.xml");
// For each item in the document that contains the words create specific
// dictionary entries (the word and the definition).
foreach (XmlNode node in wordsDoc.SelectNodes("/words/word"))
{
wordsDic.Add(id,node.Attributes["text"].InnerText);
defsDic.Add(id,node.Attributes["def"].InnerText);
id++;
}
// Generate a random number that will identify the word from the dictionary.
rand = randNum.Next(0,wordsDic.Count);
// Get the word with the specified index.
word = wordsDic[rand];
// Display the word definition.
label2.Text = defsDic[rand];
// Split the chosen word in letters and add them to the list.
for (int n = 0; n< word.Length; n++)
{
letters.Add(word.Substring(n,1));
}
// Randomly generate letters from the list and display them in the specified label.
for (int q = 0; q < letters.Count; q++)
{
lp:letNum = randLet.Next(0, word.Length);
for (int z = 0; z < restricted.Count; z++)
{
if (restricted[z] == letNum)
{
goto lp;
}
}
label1.Text += letters[letNum];
restricted.Add(letNum);
}
}
Now, the word is also generated when the user will press the Another Word button:
csharp
private void button2_Click(object sender, EventArgs e)
{
// Clear all specific fields and dictionaries/lists.
label1.Text = "";
label2.Text = "";
word = "";
restricted.Clear();
letters.Clear();
// Generate a random number that will identify the word from the dictionary.
rand = randNum.Next(0, wordsDic.Count);
// Get the word with the specified index.
word = wordsDic[rand];
// Display the word definition.
label2.Text = defsDic[rand];
// Split the chosen word in letters and add them to the list.
for (int n = 0; n < word.Length; n++)
{
letters.Add(word.Substring(n, 1));
}
// Randomly generate letters from the list and display them in the specified label.
for (int q = 0; q < letters.Count; q++)
{
lp:
letNum = randLet.Next(0, word.Length);
for (int z = 0; z < restricted.Count; z++)
{
if (restricted[z] == letNum)
{
goto lp;
}
}
label1.Text += letters[letNum];
restricted.Add(letNum);
}
}
You see, that the code has some modifications, like the functionality to clean the filled collections and fields.
The user should also guess the word, so when he enters the word in the TextBox and clicks on the Guess button, a message box should appear, saying if the guessed word is correct or not:
csharp
private void button1_Click(object sender, EventArgs e)
{
// Verify if the entered word is correct or not.
if (textBox1.Text == word)
MessageBox.Show("Correct!");
else
MessageBox.Show("Incorrect!");
}
So, that's all. I must mention, that the general part of the code can be introduced in a separate class, so the developer won't have to enter the same code twice.