Full Version: Creating Word Scramblers
Dream.In.Code > Programming Tutorials > C# Tutorials
PixelCard
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:

IPB Image

2. On the form put two Label controls, a TextBox control and two Button controls:

IPB Image

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

using System.Xml;


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.



gbertoli3
How do you come up with these things PixelCard? Its cool that you can, but how do you even think these up? Does someone tell you? Do you joke around with someone? What?
PixelCard
It's all about interesting programming ideas. When I get one, I try to describe it in a program.
gbertoli3
QUOTE(PixelCard @ 1 Aug, 2008 - 09:51 AM) *

It's all about interesting programming ideas. When I get one, I try to describe it in a program.


OK I See

Your pretty knowledgeable in c#. Is that your best known programing language?
PixelCard
QUOTE
Is that your best known programing language?


Take a look in my profile to see the languages I know. I just like C# more.
gbertoli3
QUOTE(PixelCard @ 1 Aug, 2008 - 10:30 AM) *

QUOTE
Is that your best known programing language?


Take a look in my profile to see the languages I know. I just like C# more.


Yeah I like c# too.
So is Java hard / easy to learn?
PixelCard
QUOTE
So is Java hard / easy to learn?


Can't say it's very easy, but it's easier when the developer is familiar with any other programming language (especially such languages like C/C++ or C#).
gbertoli3
QUOTE(PixelCard @ 1 Aug, 2008 - 11:29 AM) *

QUOTE
So is Java hard / easy to learn?


Can't say it's very easy, but it's easier when the developer is familiar with any other programming language (especially such languages like C/C++ or C#).


OK I know what you mean. So what can you create in java that you can not in c#, c++
Kirth
QUOTE(gbertoli3 @ 1 Aug, 2008 - 12:14 PM) *

QUOTE(PixelCard @ 1 Aug, 2008 - 11:29 AM) *

QUOTE
So is Java hard / easy to learn?


Can't say it's very easy, but it's easier when the developer is familiar with any other programming language (especially such languages like C/C++ or C#).


OK I know what you mean. So what can you create in java that you can not in c#, c++


What's so great about Java you ask? Crossplatform support!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.