尝试捕捉骰子滚动问题和复选框问题

时间:2011-02-21 00:06:44

标签: c# .net

嘿大家我在项目上遇到麻烦,试试看块。我需要它来做以下事情:

  

首先检查第一次掷骰,如果是真实的跳过保持检查并掷出所有骰子

     

如果为false,请尝试以下操作('锁定'新近举行的骰子(以某种方式)并滚动未支持的骰子)

     

如果没有骰子,则抛出异常

我附上了我的代码。我很迷茫可以有人帮忙....

public partial class FrmBupkis1 : Form
{
    private PictureBox[] diceImages;
    private CheckBox[] holds;
    private Random rnd = new Random();


    public Frm1()
    {
        InitializeComponent();
        diceImages = new PictureBox[6];
        diceImages[0] = pbxDie0;
        diceImages[1] = pbxDie1;
        diceImages[2] = pbxDie2;
        diceImages[3] = pbxDie3;
        diceImages[4] = pbxDie4;
        diceImages[5] = pbxDie5;

        holds = new CheckBox[6];
        holds[0] = chbHold0;
        holds[1] = chbHold1;
        holds[2] = chbHold2;
        holds[3] = chbHold3;
        holds[4] = chbHold4;
        holds[5] = chbHold5;
    }

    private void rollBtn_Click(object sender, EventArgs e)
    {

        //First Check for first roll, if true skip hold checks and roll all dice
        //If false, Try the following ( 'lock' newly held dice (somehow) AND roll unheld dice )
        //If no dice are held, throw exception


            for (int i = 0; i < 6; i++)
            {
                //if die is not held, then assign random number to image box
                if (holds[i].Checked == false)
                    diceImages[i].Image = iglDice.Images[rnd.Next(6)];

                try
                {
                    Random = random.Next(0, 6);
                    diceImages[i].Image = dieImages[Random];
                    rollBtn = true;
                }

                catch (FormatException ex)
                {
                    Console.WriteLine(ex.Message, "Error. Try Again.");
                }

            }

        }

    private void gameOverBtn_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 6; i++)
        {

            diceImages[i].Image = null;
            holds[i].Checked = false;
            holds[i].Enabled = true;
        }
    }


    private void quitBtn_Click(object sender, EventArgs e)
    {
        //btnQuit
        this.Close();

    }
}

5 个答案:

答案 0 :(得分:1)

此代码存在许多问题:

Random = random.Next(0, 6);
diceImages[i].Image = dieImages[Random];

假设dieImages应该是diceImages(否则将无法编译) 您正在使用与.NET类同名的变量 - 令人困惑。此变量似乎也没有在任何地方声明 - 只需使用您直接全局定义的Random实例:

diceImages[i].Image = diceImages[rnd.Next(6)];

你也使用0到6的范围 - 使用单参数方法Next来获得0到5的范围(上边界是独占的),如上所示,否则你会得到{{1最终当你访问IndexOutofRangeException时。

答案 1 :(得分:1)

我看到你试图捕获异常的位置,但我没有看到代码中你实际抛出异常的地方。抛出异常如下所示:

throw new ApplicationException("No dice are held");

或者您可以创建自己的异常并将其抛出。例外是这样的:

class NoDiceHeldException : ApplicationException
{
    // define constructors here
}

然后你可以抛出这样的异常:

throw new NoDiceHeldException();

答案 2 :(得分:1)

所以我在下面有这个尝试捕获...仍然非常困惑它需要如何工作....呃......

            for (int i = 0; i < 6; i++)
            {
                //if die is not held, then assign random number to image box
                try
                {
                    if (holds[i].Checked == false)
                        diceImages[i].Image = iglDice.Images[rnd.Next(6)];

                    if (holds[i].Checked == true)
                    {
                        //HERE IS WHER I NEED IT TO HOLD THE DICE IF THEY ARE CHECK AND ONLY ROLL THE REMAINING DICE. UGH I'M STILL LOST... 
                    }

                    throw new ApplicationException("No dice are held");

                }

                catch (Exception ex)
                {
                    MessageBox.Show("You must hold at least 1 scoring dice before rolling", "Format Error");
                }

            }

答案 3 :(得分:1)

我仍然对你要做的事情感到困惑,但无论如何都试图提供帮助。当您从0到5循环时,您需要跟踪是否保留了任何项目。您希望在完成循环后(即在它下面)抛出异常。例如,如果您要运行现在的代码,最终会抛出异常6次。

我认为你会发现try / catch循环应该在你的循环之外。它应该是这样的:

try
{
    bool foundHeldDie = false;

    for (int i = 0; i < 6; ++i)
    {
        // your code, which sets foundHeldDie to true if appropriate
        if (holds[i].Checked == true)
        {
            foundHeldDie = true;
            holds[i].Enabled = false;
        }  
    }

    if (!foundHeldDie)
    {
        // throw the exception
    }
}
catch (Exception ex)
{
    // handle the exception
}

要锁定骰子(您仍然缺少代码的位置),您可以禁用该复选框(将Enabled属性设置为false),这样用户就无法更改它。

答案 4 :(得分:1)

public partial class FrmBupkis1 : Form
{
    private PictureBox[] diceImages;
    private CheckBox[] holds;
    private Random rnd = new Random();


    public FrmBupkis1()
    {
        InitializeComponent();
        diceImages = new PictureBox[6];
        diceImages[0] = pbxDie0;
        diceImages[1] = pbxDie1;
        diceImages[2] = pbxDie2;
        diceImages[3] = pbxDie3;
        diceImages[4] = pbxDie4;
        diceImages[5] = pbxDie5;

        holds = new CheckBox[6];
        holds[0] = chbHold0;
        holds[1] = chbHold1;
        holds[2] = chbHold2;
        holds[3] = chbHold3;
        holds[4] = chbHold4;
        holds[5] = chbHold5;
    }

    private void rollBtn_Click(object sender, EventArgs e)
    {

        //First Check for first roll, if true skip hold checks and roll all dice
        //If false, Try the following ( 'lock' newly held dice (somehow) AND roll unheld dice )
        //If no dice are held, throw exception

            for (int i = 0; i < 6; i++)
        {
            //if die is not held, then assign random number to image box
            if (holds[i].Checked == false)
                diceImages[i].Image = iglDice.Images[rnd.Next(6)];
        }


        try
        {
            bool foundHeldDie = false;

            for (int i = 0; i < 6; ++i)
            {
                // your code, which sets foundHeldDie to true if appropriate
                if (holds[i].Checked == true)
                {
                    foundHeldDie = true;
                    holds[i].Enabled = false;
                }
            }

            if (!foundHeldDie)
            {
                // throw the exception
                throw new ApplicationException("No dice are held");

            }
        }

        catch (Exception ex)
        {
            // handle the exception
            MessageBox.Show("You must hold at least one die before rolling.", "Error");

        }
        }

    private void gameOverBtn_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 6; i++)
        {

            diceImages[i].Image = null;
            holds[i].Checked = false;
            holds[i].Enabled = true;
        }
    }


    private void quitBtn_Click(object sender, EventArgs e)
    {
        //btnQuit
        this.Close();

    }
}