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.
It works but not in the way the instruction stated. I would appreciate all the help I can getCode: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; }Thank you very much!
Last edited by WingedPanther; 08-05-2009 at 01:01 PM. Reason: add code tags (the # button)
and the count thing doesnt work, unfortunatelyit only removes the 2nd character and add it to the end of the word
Can you give an example of the desired input/output and what you're getting? You may want to include a little more code, as many of your variables and defined/initialized in the code block listed.
hey thanks for replyingthis is my entire code.
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.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(); } } }
So basically they get the see the "scrambled" word, and they have to guess what the real word is?
You are having a bit of trouble generating the scrambled word right? Well you could use the same way of doing it, make a bunch of scrambled words and pick, etc etc etc...
There seemed to be quite a few issues with the code you had. I fixed it up a bit for yaYou should probably learn how to turn instructions into usable code on your own in order to pass the class or just become a more proficient programmer. I dont usually help with homework but I am because you had most of what you needed there, it just needed some tweaking.
You could also use to add a keyup event handler so that when the user is done typing they just have to hit enter to click the submit button...just a thought. Hope this helps you out!Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WordScramble { public partial class Form1 : Form { Random r = new Random(); Random r2 = new Random(); private string strStoreWord; int intRandom = 0; string[] strWord = { "engineer", "toenails", "penguins", "asterisk", "appetite", "backstab", "junkyard", "werewolf", "princess", "trespass" }; public Form1() { InitializeComponent(); } private void wordScramble() { string strAnagram = null; intRandom = r.Next(10); strAnagram = strWord[intRandom]; strStoreWord = strAnagram; for (int i = 0; i < 20;i++ ) { string strAnagram2; int intRandom2 = r2.Next(strAnagram.Length); strAnagram2 = strAnagram.Substring(intRandom2, 1); strAnagram = strAnagram.Remove(intRandom2, 1); strAnagram = strAnagram.Insert(strAnagram.Length, strAnagram2); } lblAnagram.Text = strAnagram; txtGuess.Focus(); } private void Form1_Load_1(object sender, EventArgs e) { wordScramble(); } private void btnSubmit_Click_1(object sender, EventArgs e) { string strGuess; strGuess = txtGuess.Text.ToString(); if (strGuess.ToLower() == strStoreWord) { lblResult.Text = "You are CORRECT!"; txtGuess.Clear(); wordScramble(); } else { lblResult.Text = "Wrong! Please try again!"; txtGuess.Clear(); } txtGuess.Focus(); } } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks