proggcat
August 5th, 2009, 11:03 PM
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.
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!
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.
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!