Latest
I tried to use Dictionary for matching the char to Color to shorten the matching source code part. Then pass TextBox instead of its length.
private Color[] sequence;
//Declare dictionary
private Dictionary<char,Color> stringTocolor = new Dictionary<char,Color>();
public SimonSays ()
{
//add content to Dictionary
stringTocolor.Add('R', Color.Red);
stringTocolor.Add('G', Color.Green);
stringTocolor.Add('B', Color.Blue);
stringTocolor.Add('Y', Color.Yellow);
Color[] colourset = newSequence(textBox1);
}
public Color[] newSequence(TextBox textBox)
{
int length = textBox.Text.Length;
Color[] array = new Color[length];
//check dictionary has the char key or not
for (int i = 0; i < length; i++)
{
if (stringTocolor.ContainsKey(textBox.Text[i]))
{
array[i] = stringTocolor[textBox.Text[i]];
}
//give alert if wrong key
else
{
MessageBox.Show("Wrong Colour input at index " + i + " of textbox string!");
}
}
this.sequence = array;
return array;
}
Old
Call newSequence
somewhere by passing the textbox text length as its argument (length)
Color[] colourset = newSequence(textBox1.Text.Length);
then try to check each character inside the textbox text then change them to corresponding colour and return them in colourset.
public Color[] newSequence(int length)
{
Color[] array = new Color[length];
//Random rand = new Random(DateTime.Now.Millisecond);// don't inline w/ colors[] - wont be random
for (int i = 0; i < textBox1.Text.Length; i++)
{
//array[i] = colors[rand.Next(0, 4)];
if (textBox1.Text[i]=='R')
{
array[i] = Color.Red;
}
else if (textBox1.Text[i] == 'G')
{
array[i] = Color.Green;
}
else if (textBox1.Text[i] == 'B')
{
array[i] = Color.Blue;
}
else if (textBox1.Text[i] == 'Y')
{
array[i] = Color.Yellow;
}
else
{
MessageBox.Show("Wrong colour input found!");
}
}
//why stored to sequence? further use in current class?
this.sequence = array;
return array;
}
seem like you generating a class, so may be your newSequence need to have one more argument to receive the textBox string from outside of the class but not directly access to the textbox?