我对c#有点新,并收到错误消息。我正在尝试重新创建一个发布在youtube上的程序: https://www.youtube.com/watch?v=E8XQ9x-7yYk
我似乎遇到了一些问题。据说这是一个超出范围的错误。我一直在尝试不同的方法来解决问题,我似乎无能为力。
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
string word = "";
List<Label> labels = new List<Label>();
int amount = 0;
enum BodyParts
{
Head,
Left_Eye,
Right_Eye,
Mouth,
Right_Arm,
Left_Arm,
Body,
Left_Leg,
Right_Leg,
}
void drawhangpost()
{
Graphics g = panel1.CreateGraphics();
Pen p = new Pen(Color.Brown, 10);
g.DrawLine(p, new Point(130, 218), new Point(130, 5));
g.DrawLine(p, new Point(135, 5), new Point(65, 5));
g.DrawLine(p, new Point(60, 0), new Point(60, 50));
//DrawBodyPart(BodyParts.Head);//just for show
//DrawBodyPart(BodyParts.Left_Eye);
//DrawBodyPart(BodyParts.Right_Eye);
//DrawBodyPart(BodyParts.Mouth);
//DrawBodyPart(BodyParts.Right_Arm);
//DrawBodyPart(BodyParts.Left_Arm);
//DrawBodyPart(BodyParts.Body);
//DrawBodyPart(BodyParts.Left_Leg);
//DrawBodyPart(BodyParts.Right_Leg);
//MessageBox.Show(GetRandomWord());
}
void DrawBodyPart(BodyParts bp)
{
Graphics g = panel1.CreateGraphics();
Pen p = new Pen(Color.Blue, 2);
if (bp == BodyParts.Head)
g.DrawEllipse(p, 40, 50, 40, 40);
else if (bp == BodyParts.Left_Eye)
{
SolidBrush s = new SolidBrush(Color.Black);
g.FillEllipse(s, 50, 60, 5, 5);
}
else if (bp == BodyParts.Right_Eye)
{
SolidBrush s = new SolidBrush(Color.Black);
g.FillEllipse(s, 63, 60, 5, 5);
}
else if (bp == BodyParts.Mouth)
{
g.DrawArc(p, 50, 60, 20, 20, 45, 90);
}
else if (bp == BodyParts.Body)
g.DrawLine(p, new Point(60, 90), new Point(60, 170));
else if (bp == BodyParts.Left_Arm)
g.DrawLine(p, new Point(60, 100), new Point(30, 85));
else if (bp == BodyParts.Right_Arm)
g.DrawLine(p, new Point(60, 100), new Point(90, 85));
else if (bp == BodyParts.Left_Leg)
g.DrawLine(p, new Point(60, 170), new Point(30, 190));
else if (bp == BodyParts.Right_Leg)
g.DrawLine(p, new Point(60, 170), new Point(90, 190));
}
void MakeLables()
{
word = GetRandomWord();
char[] chars = word.ToCharArray();
int between = 330 / chars.Length - 1;
for(int i = 0; i < chars.Length - 1; i++)
{
labels.Add(new Label());
labels[i].Location = new Point((i * between) + 10, 80);
labels[i].Text = "_";
labels[i].Parent = groupBox2;
labels[i].BringToFront();
labels[i].CreateControl();
}
label1.Text = "Word Length: " + (chars.Length - 1).ToString();
}
string GetRandomWord()
{
WebClient wc = new WebClient();
string wordList = wc.DownloadString("https://raw.githubusercontent.com/Tom25/Hangman/master/wordlist.txt");
string[] words = wordList.Split('\n');
Random ran = new Random();
return words[ran.Next(0, words.Length - 1)];
}
private void form3_shown(object sender, EventArgs e)
{
drawhangpost();
MakeLables();
}
private void button1_Click(object sender, EventArgs e)
{
char letter = textBox1.Text.ToLower().ToCharArray()[0];
if (!char.IsLetter(letter))
{
MessageBox.Show("You can only submit letters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (word.Contains(letter))
{
char[] letters = word.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
labels[i].Text = letter.ToString();//Line gives out of rage error
}
foreach (Label l in labels)
if (l.Text == "_") return;
MessageBox.Show("You have won", "Congrats");
ResetGame();
}
else
{
//MessageBox.Show("The letter you guessed is wrong", "Sorry");
label2.Text += " " + letter.ToString() + ",";
DrawBodyPart((BodyParts)amount);
amount++;
if (amount == 9)
{
MessageBox.Show("Sorry but you lost, the word was " + word);
ResetGame();
}
}
}
void ResetGame()
{
amount = 0;// testing not sure
Graphics g = panel1.CreateGraphics();
g.Clear(panel1.BackColor);
GetRandomWord();
MakeLables();
drawhangpost();
label2.Text = "Missed: ";
textBox1.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text == word)
{
MessageBox.Show("You Have won", "Congrats");
ResetGame();
}
else
{
MessageBox.Show("The word you guest was wrong", "Sorry");
DrawBodyPart((BodyParts)amount);
amount++;
if (amount == 9)
{
MessageBox.Show("Sorry but you lost, the word was " + word);
ResetGame();
}
}
}
}
}
答案 0 :(得分:2)
当你制作标签时,你错过了最后一个:
for(int i = 0; i < chars.Length - 1; i++)
{
labels.Add(new Label());
labels[i].Location = new Point((i * between) + 10, 80);
labels[i].Text = "_";
labels[i].Parent = groupBox2;
labels[i].BringToFront();
labels[i].CreateControl();
}
当i小于单词长度小于1时,循环停止。如果你的单词是6个字符长,你的标签将停在索引4,因为5不小于6 - 1.将它改为&lt; =或者只是&lt;并放弃-1。
当您遍历标签以将文本设置为字母时,您正在正确地执行此操作:
if (word.Contains(letter))
{
char[] letters = word.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
labels[i].Text = letter.ToString();//Line gives out of rage error
}
foreach (Label l in labels)
if (l.Text == "_") return;
MessageBox.Show("You have won", "Congrats");
ResetGame();
}
所以,如果你的单词是6个字母长,当你猜到单词i中的最后一个字母将是5时,你没有为其创建标签,并且它失败了。
答案 1 :(得分:0)
我原来的回答是完全错误的。
这是问题所在。 labels是List类型的List。列表长度在MakeLabels()中定义,此处为:
char[] chars = word.ToCharArray();
...
for(int i = 0; i < chars.Length - 1; i++)
{
labels.Add(new Label());
...
labels[i].Text = "_";
...
因此,列表长度为chars.Length - 1
但是在你的button1_Click函数中,你有:
char[] letters = word.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
labels[i].Text = letter.ToString();
...
在这里,你可以找到letters.Length,它大于标签列表的长度。如果要为label [i] .Text指定一个字母(与letters [i]相同的索引),则应该有与潜在字母一样多的标签。这意味着您应该将MakeLabels更改为:
for(int i = 0; i < chars.Length; i++)