Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 109,547 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,177 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Creating Word Scramblers

 
Reply to this topicStart new topic

> Creating Word Scramblers, Word Scramblers using C# and WinForms (XML used).

PixelCard
Group Icon



post 13 Jul, 2008 - 12:13 PM
Post #1


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.





Attached File(s)
Attached File  xmlWords.zip ( 41.66k ) Number of downloads: 53
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

gbertoli3
Group Icon



post 1 Aug, 2008 - 08:25 AM
Post #2
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?
Go to the top of the page
+Quote Post

PixelCard
Group Icon



post 1 Aug, 2008 - 09:51 AM
Post #3
It's all about interesting programming ideas. When I get one, I try to describe it in a program.
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 1 Aug, 2008 - 10:06 AM
Post #4
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?
Go to the top of the page
+Quote Post

PixelCard
Group Icon



post 1 Aug, 2008 - 10:30 AM
Post #5
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.
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 1 Aug, 2008 - 10:31 AM
Post #6
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?
Go to the top of the page
+Quote Post

PixelCard
Group Icon



post 1 Aug, 2008 - 11:29 AM
Post #7
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#).
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 1 Aug, 2008 - 12:14 PM
Post #8
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++
Go to the top of the page
+Quote Post

Kirth
*



post 15 Aug, 2008 - 05:23 AM
Post #9
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!
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 9/7/08 10:17PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month