[英]Random Sequence to Deterministic Sequence (based on the contents of textbox)
我正在制作Simon Says游戏,但与正常的Simon Says游戏(每次都会生成随机序列)不同,由于游戏的性质,我希望使序列为非随机且基于textbox1.text
。
因此,例如: textbox1.text
可能包含“ RYRG”行。 游戏将其解释为“红色,黄色,红色,绿色”。
此刻,游戏每次都会随机生成一个序列。 我对如何更改此方法有些困惑。 本质上,我想将文本框textbox1.text
的内容放入一个数组中。 这样,此Array就可以用作游戏的序列缓冲区。 任何帮助表示赞赏,在此先感谢您。
更新:现在一切正常(但仅适用于1行)
码:
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;
}
我可以利用这条线吗?
public void newSequence (Color [] sequence)
{
this.sequence= //read next line
}
我尝试使用Dictionary将char与Color匹配,以缩短匹配的源代码部分。 然后传递TextBox而不是其长度。
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;
}
通过传递文本框文本长度作为其参数(长度)在某处调用newSequence
Color[] colourset = newSequence(textBox1.Text.Length);
然后尝试检查文本框文本内的每个字符,然后将其更改为相应的颜色,并以颜色设置返回它们。
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;
}
好像您正在生成一个类,所以newSequence可能需要更多的参数才能从类外部接收textBox 字符串 ,但不能直接访问该文本框吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.