|
-
August 5th, 2009, 11:03 PM
#1
Help needed with word scramble program!
Sorry to bother any of you but I need help regarding a word scramble program I am working on for class.
Instructions stated that I am supposed to generate a random number to use as the index of the word in an array, then retrieve the word from the array, using the first random number as an index. Store the word in another string variable. And then to generate a second random number to store the index of a character to be moved.
I am supposed to use a for statement to iterate through a word 20 times. Each time the loop executes, pass the second random number created earlier to the string indexer. Append the character returned by the idexer to the end of the string, and remove it from its original position.
Next, generate a new random number to move a different character during the next iteration of the loop.
This is what I have done so far.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace anagram_
{
public partial class Form1 : Form
{
int intRandom = 0;
int count = 0;
string[] strWord = { "engineer", "toenails", "penguins", "asterisk", "appetite", "backstab", "junkyard", "werewolf", "princess", "trespass" };
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Random r = new Random();
intRandom = r.Next(0, 10);
for (int i = 0; i < 20; i++)
{
string strAnagram;
string strAnagram1;
string strAnagram2;
strAnagram1 = strWord[intRandom];
strAnagram2 = strWord[intRandom];
strAnagram1 = strWord[intRandom].Substring(1, 1);
strAnagram2 = strWord[intRandom].Remove(1, 1);
strAnagram = strAnagram2.Insert(7, (strWord[intRandom].Substring(1,1)));
lblAnagram.Text = strAnagram;
}
txtGuess.Focus();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string strGuess;
strGuess = txtGuess.Text;
if (strGuess.ToLower() == strWord[intRandom])
{
lblResult.Text = "You are CORRECT!";
Random r = new Random();
intRandom = r.Next(0, 10);
for (int count = 0; count < 20; count++)
{
string strAnagram;
string strAnagram1;
string strAnagram2;
strAnagram1 = strWord[intRandom];
strAnagram2 = strWord[intRandom];
strAnagram1 = strWord[intRandom].Substring(1, 1);
strAnagram2 = strWord[intRandom].Remove(1, 1);
strAnagram = strAnagram2.Insert(7, (strWord[intRandom].Substring(1, 1)));
lblAnagram.Text = strAnagram;
}
txtGuess.Clear();
}
else
{
lblResult.Text = "Wrong! Please try again!";
}
txtGuess.Focus();
}
}
}
This is done in microsoft visual studio 2005. When the user opens the word scramble program, they're presented with 2 labels, a textbox and a button. The top is the label which contains the 'scrambled' word, the textbox is the place the player is supposed to input his guess, the bottom is the label where the player will know if they guessed the word correctly and the button is submit.
It works but not in the way the instruction stated. I would appreciate all the help I can get Thank you very much!
-
August 6th, 2009, 01:07 AM
#2
Re: Help needed with word scramble program!
I did not get what your Word Scrambler should do exactly.
What is aim of your app?
-
August 6th, 2009, 01:19 AM
#3
Re: Help needed with word scramble program!
The aim is to scramble a random word, generate a random number to store the index of a character to be moved.
And to use a for statement to iterate through a word 20 times. Each time the loop executes, pass the random number created earlier to the string indexer. Append the character returned by the indexer to the end of the string, and remove it from its original position.
-
August 6th, 2009, 01:28 AM
#4
Re: Help needed with word scramble program!
*shudders at the awful form of Hungarian notation schools force upon us*
As this is for school, perhaps you should tell us what it does correctly, what it does not do correctly, and what you have tried so far?
Last edited by BigEd781; August 6th, 2009 at 01:36 AM.
-
August 7th, 2009, 04:30 PM
#5
Re: Help needed with word scramble program!
One problem you have is that you are trying to initialize the string to store the temporary anagram inside the for loop. What you need to do is store the partially mixed up word in a string and then change that word each pass though the for loop, not just constantly change the characters around in one word 20 times. I will try writing what you need now and post what I get.
-
August 7th, 2009, 04:47 PM
#6
Re: Help needed with word scramble program!
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace anagram_
{
public partial class Form1 : Form
{
int intRandomWordInd = 0;
int count = 0;
string[] strWord = { "engineer", "toenails", "penguins", "asterisk", "appetite", "backstab", "junkyard", "werewolf", "princess", "trespass" };
public Form1()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string strGuess;
strGuess = txtGuess.Text;
if (strGuess.ToLower() == strWord[intRandomWordInd])
{
lblResult.Text = "You are CORRECT!";
SetAnagram();
txtGuess.Clear();
}
else
{
lblResult.Text = "Wrong! Please try again!";
}
txtGuess.Focus();
}
private void SetAnagram()
{
//Initalize an instance of the random class
Random r = new Random();
//get a random index for the word
intRandomWordInd = r.Next(strWord.Length);
//set the current word to a string to be used as a holder of the parcial anagram for each pass of the for loop
string strAnagram = strWord[intRandomWordInd];
for (int i = 0; i < 20; i++)
{
//generate a random index for a character to be removed
int intRemoveInd = r.Next(strAnagram.Length);
//remove the character at that index
string temp1 = strAnagram.Remove(intRemoveInd, 1);
//get what that character, that was removed was
string temp2 = strAnagram.Substring(intRemoveInd, 1);
//combine the two, putting the character at the end
strAnagram = temp1 + temp2;
}
//set the anagram to be the label's text
lblAnagram.Text = strAnagram;
}
private void Form1_Load(object sender, EventArgs e)
{
SetAnagram();
txtGuess.Focus();
}
}
}
Should work how you wanted, I move the anagram to a method to save some work. I also had no idea what the count implications were going to be so I just left it alone.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|