C#Hangman游戏错误

时间:2013-12-31 09:27:18

标签: c#

我一直有这个错误。

ArgumentOutOfRangeException未处理。 指数超出范围。必须是非负数且小于集合的大小。 参数名称:index

以下是有问题的表单的完整代码,它是Hangman游戏发生的地方。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hangman_APPD_Assignment
{
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

List labels = new List();是makeLabels()方法将使用它的标签。

    String w = "";
    List<Label> labels = new List<Label>();
    int score = 0, missed = 0, correctCount = 0, gameCount = 1;

下面的代码是当用户点击QUIT按钮时,应用程序将关闭。

    private void btnQuit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

下面的代码是表单加载或显示时,使用了makeLabels()方法。

    private void Form2_Load(object sender, EventArgs e)
    {
        makeLabels();
    }

下面的代码是使用随机单词并转换为一个字符串,用符号“_”替换每个字母。转换后的字符串将放在名为Labels的标签内。

    private void makeLabels()
    {
        w = getRandomWord().ToLower();
        w.Replace(" ", "");

        char[] letters = w.ToCharArray();
        int space = 569 / letters.Length - 1;

        for (int i = 0; i < letters.Length; i++)
        {
            labels.Add(new Label());
            labels[i].Location = new Point((i * space) + 10, 109);
            labels[i].Parent = groupBox2;
            labels[i].Text = "_" + i;
            labels[i].BringToFront();
            labels[i].CreateControl();
        }


        lblLength.Text = letters.Length.ToString();
    }

下面的代码是一种创建新游戏的方法:1)用户完全猜到单词或2)用户用尽猜测。

    private void newGame()
    {
        gameCount++;

        if (gameCount == 15)
        {
            this.Hide();
            if (score >= 7)
            {
                Form4 f4 = new Form4();
                f4.ShowDialog();
            }
            else
            {
                Form6 f6 = new Form6();
                f6.ShowDialog();
            }
        }
        else
        {
            getRandomWord();
            makeLabels();
            enableLetterButtons();
            lblMissed.Text = "";
            hangPic.Image = Hangman_APPD_Assignment.Properties.Resources.part0;
        }
    }

下面的代码从字符串数组中获取一个随机单词,用作游戏中的神秘单词。

    public string getRandomWord()
    {
        Random randomNum = new Random();

        String[] words = {"virus", "network", "syntax", "router", "switch", "worms", "trojan", "email", "bios",
                             "cmos", "ram", "cipher", "malware", "botnet", "cookies", "patches", "cryptograph",
                             "metamorphic", "polymorphic", "rootkit", "logicbomb", "spam", "spyware", "keyloggers",
                             "adware", "software", "hardware", "botherder", "phishing", "whaling", "pharming", 
                             "vishing", "spim", "topology", "tailgating", "loop", "java", "motherboard", "unique",
                             "parameter"};
        int randomNumber = randomNum.Next(0, (words.Length - 1));

        return words[randomNumber];
    }

以下代码是每次用户猜错时更改图片。

    private void setPicture(int wrongTry)
    {
        switch(wrongTry)
        {
            case 0: 
                hangPic.Image = Hangman_APPD_Assignment.Properties.Resources.part0;
                break;
            case 1: 
                hangPic.Image = Hangman_APPD_Assignment.Properties.Resources.part1;
                break;
            case 2:
                hangPic.Image = Hangman_APPD_Assignment.Properties.Resources.part2;
                break;
            case 3:
                hangPic.Image = Hangman_APPD_Assignment.Properties.Resources.part3;
                break;
            case 4:
                hangPic.Image = Hangman_APPD_Assignment.Properties.Resources.part4;
                break;
            case 5:
                hangPic.Image = Hangman_APPD_Assignment.Properties.Resources.part5;
                break;
            case 6:
                hangPic.Image = Hangman_APPD_Assignment.Properties.Resources.part6;
                break;
        }
    }

以下代码是在用户点击其中一个字母按钮时调用方法。

    private void checkGuessedLetter(string wordToGuess, string guessedLetter, Button buttonName)
    {
        int strLength = wordToGuess.Length;

        char letter = guessedLetter.ToCharArray()[0];
        buttonName.Enabled = false;

        if (w.Contains(guessedLetter))
        {
            char[] LS = w.ToCharArray();
            for (int i = 0; i < LS.Length; i++)
            {
                if (LS[i] == letter)
                {
                    MessageBox.Show("The value of w is " + w + " AND " + i);
                    labels[i].Text = letter.ToString();
                    conditionPic.Image = Hangman_APPD_Assignment.Properties.Resources.correctpic;
                    correctCount++;
                    MessageBox.Show("You got correct " + correctCount + " time(s).");
                    if (correctCount == strLength)
                    {
                        MessageBox.Show("Good job! Keep it up, matey!", "Victory!", MessageBoxButtons.OK, MessageBoxIcon.None);
                        score++;
                        lblScore.Text = score.ToString();
                        newGame();
                        missed = 0;
                        correctCount = 0;
                        labels.Clear();
                    }
                }

                foreach (Label l in labels)
                    if (l.Text == "__") return;
            }
        }
        else
        {
            conditionPic.Image = Hangman_APPD_Assignment.Properties.Resources.wrong;
            lblMissed.Text += " " + letter.ToString() + " |";
            missed++;
            MessageBox.Show("You missed " + missed + " time(s).");
            setPicture(missed);

            if (missed == 6)
            {
                MessageBox.Show("Unfortunately, you lost this round... Make sure you won't let this happen again, "
                + "or else you will end up in Davy Jones' Locker.", "Defeat!", MessageBoxButtons.OK, 
                MessageBoxIcon.Error);
                newGame();
                missed = 0;
                correctCount = 0;
                labels.Clear();
            }
        }
    }

以下代码是为了获得在lblScore标签处积累的分数,以便在下一个表单中显示。

    public int getScore()
    {
        int score = int.Parse(lblScore.Text);
        return score;
    }

以下代码是在用户点击包含字母表的按钮时调用方法来猜测Hangman游戏中的单词。

    private void Abtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "a", Abtn);
    }

    private void Bbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "b", Bbtn);
    }

    private void Cbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "c", Cbtn);
    }

    private void Dbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "d", Dbtn);
    }

    private void Ebtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "e", Ebtn);
    }

    private void Fbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "f", Fbtn);
    }

    private void Gbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "g", Gbtn);
    }

    private void Hbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "h", Hbtn);
    }

    private void Ibtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "i", Ibtn);
    }

    private void Jbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "j", Jbtn);
    }

    private void Kbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "k", Kbtn);
    }

    private void Lbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "l", Lbtn);
    }

    private void Mbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "m", Mbtn);
    }

    private void Nbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "n", Nbtn);
    }

    private void Obtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "o", Obtn);
    }

    private void Pbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "p", Pbtn);
    }

    private void Qbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "q", Qbtn);
    }

    private void Rbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "r", Rbtn);
    }

    private void Sbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "s", Sbtn);
    }

    private void Tbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "t", Tbtn);
    }

    private void Ubtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "u", Ubtn);
    }

    private void Vbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "v", Vbtn);
    }

    private void Wbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "w", Wbtn);
    }

    private void Xbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "x", Xbtn);
    }

    private void Ybtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "y", Ybtn);
    }

    private void Zbtn_Click(object sender, EventArgs e)
    {
        checkGuessedLetter(w, "z", Zbtn);
    }

以下代码是为了启用上一个游戏中点击过的所有按钮。

    private void enableLetterButtons()
    {
        Abtn.Enabled = true;
        Bbtn.Enabled = true;
        Cbtn.Enabled = true;
        Dbtn.Enabled = true;
        Ebtn.Enabled = true;
        Fbtn.Enabled = true;
        Gbtn.Enabled = true;
        Hbtn.Enabled = true;
        Ibtn.Enabled = true;
        Jbtn.Enabled = true;
        Kbtn.Enabled = true;
        Lbtn.Enabled = true;
        Mbtn.Enabled = true;
        Nbtn.Enabled = true;
        Obtn.Enabled = true;
        Pbtn.Enabled = true;
        Qbtn.Enabled = true;
        Rbtn.Enabled = true;
        Sbtn.Enabled = true;
        Tbtn.Enabled = true;
        Ubtn.Enabled = true;
        Vbtn.Enabled = true;
        Wbtn.Enabled = true;
        Xbtn.Enabled = true;
        Ybtn.Enabled = true;
        Zbtn.Enabled = true;
    }

以下代码是当用户点击应用程序右上角的X按钮时关闭应用程序。

    private void btnOut_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

}

P.S。对不起,我忘了把错误放在哪里。 所以错误发生在

labels[i].Text = letter.ToString();

在checkGuessedLetter(字符串wordToGuess,字符串guessedLetter,Button buttonName)方法中。

1 个答案:

答案 0 :(得分:4)

问题在于以下几点:

newGame();
missed = 0;
correctCount = 0;
labels.Clear();

关键是要停下来思考newGame做了什么,然后想想你之后做了什么?由于这是家庭作业(我假设从一些名称)真的去思考它。

完成?好。所以希望你意识到你的newGame方法会调用MakeLabels来生成标签,然后你会在几行之后清除labels。这意味着对于后续游戏,您的labels列表将始终为空。

对此的正确解决方法是我引用的最后三行都是创建新游戏的一部分,因此应该使用该方法。事实上,当你制作新的labels时,你只需要清除{{1}},这样也可以使用那个方法。这样做,你的程序将更清晰,希望也可以工作! :)