سؤال

I'm making a Simon Says game, but unlike normal Simon Says games (where random sequences are generated each time), I wish to make the sequences non-random and based on textbox1.text due to the nature of the game.

So for example: textbox1.text may include the line "RYRG". Which the game interprets as "Red, Yellow, Red, Green".

At the moment, the game randomly generates a sequence each time. I'm a bit stuck as to how I can change this. Essentially, I want to put the contents of the textbox textbox1.text into an Array. So that this Array would then serve as the sequence buffer for the game. Any help is appreciated, thank you in advance.

Update: it all works now (but for 1 line only)

Code:

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.Text.Length);
}

public Color[] newSequence(int length)
{
    Color[] array = new Color[length];
    //check dictionary has the char key or not
    for (int i = 0; i < textBox1.Text.Length; i++)
    {
        if (stringTocolor.ContainsKey(textBox1.Text[i]))
        {
             array[i] = stringTocolor[textBox1.Text[i]];
        }
        //give alert if wrong key
        else
        {
             MessageBox.Show("Wrong Colour input at index " + i + " of textbox string!");
        }
    }
    this.sequence = array;
    return array;
}

Could I possibly utilise this line?

        public void newSequence (Color [] sequence)
    {
    this.sequence= //read next line 

    }
هل كانت مفيدة؟

المحلول

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 newSequencesomewhere 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?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top