2

我完成了我的掷骰子程序,并一直在这里和那里进行调整,我的程序目前允许用户随意掷骰子,但是,我尝试使用不同的代码来让玩家只被允许某些在他们收到他们已经输掉所有钱的消息之前的滚动数,例如我的游戏应该预先确定一个随机数让我们说 9,在 9 次滚动后他们收到消息,我重置所有内容并结束游戏。我是新手 :(


来自评论的代码

private void button1_Click(object sender, EventArgs e) 
{ 
    rollagainLabel.Visible = true; 
    Random rand = new Random(); 
    roll1 = rand.Next(6) + 1; 
    int value = 100; 
    int sum = (roll1 * value); 
    runningTotal += sum; 
    totalmoneyLabel.Text = runningTotal.ToString("c"); 
    int maxRolls = rand.Next(5); 
    for (int i = 0; i < maxRolls; i++) 
        if (roll1 == 1) 
        {
            diceBox1.Image = GAME.Properties.Resources._1Die; 
        }
} 
4

3 回答 3

4

只需在程序开始时计算数字并将其存储在变量中,然后计算玩家滚动的次数并检查它是否达到先前计算的数字。

Random rand = new Random();
int maxRolls = rand.Next(10); // 10, or whatever you want the max possible limit to be

for(int i = 0; i < maxRolls; i++)
{
    roll(); // obviously this is not actual code to be used, but it gives you the structure. each roll goes inside this loop.
}
于 2012-10-30T03:58:55.710 回答
0

我认为您想要做的是在 Page_Load 事件中生成您的随机数。之后,将其保存到会话中,以便您可以在 Button_Click 事件中使用它进行比较。

    Random rand = new Random();
    Session["maxRolls"] = rand.Next(10);

之后,您可以通过这种方式检索值

    int maxRolls = (int)Session["maxRolls"];

编辑

这是将所有内容联系在一起的示例代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        int minAllowableRolls = 1;
        int maxAllowableRolls = 10;
        Random rand = new Random();
        Session["maxRolls"] = rand.Next(maxAllowableRolls - minAllowableRolls) + minAllowableRolls;
        Session["rollCount"] = 0;
        Session["runningTotal"] = 0;
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    int maxRolls = (int)Session["maxRolls"];
    int rollCount = (int)Session["rollCount"];
    int runningTotal = (int)Session["runningTotal"];

    rollCount++;
    if (rollCount < maxRolls)
    {
        Random rand = new Random();
        runningTotal += rand.Next(6) + 1;
        Label1.Text = runningTotal.ToString();
    }
    else
    {
        // Game has ended
    }
    Session["rollCount"] = rollCount;
    Session["runningTotal"] = runningTotal;
}
于 2012-10-30T04:43:25.013 回答
0

这应该为您解决问题,我正在检查 maxRolls 是否等于零,然后创建一个新的随机数,然后我正在检查 rollCount 是否等于 maxRolls 并重置所有内容。

public partial class Form1 : Form
{
    int runningTotal;
    int roll1;
    int maxRolls;
    int rollCount;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (maxRolls == 0)
        {
            Random getMax = new Random();
            rollagainLabel.Visible = false;
            maxRolls = getMax.Next(10) ;
        }
        else 
            if(rollCount >= maxRolls)
            {
                maxRolls = 0;
                rollagainLabel.Visible = true;
                rollCount = 0;
                runningTotal = 0;
                totalmoneyLabel.Text = "$0.0";
                return; 
            }

        Random rand = new Random();
        roll1 = rand.Next(6) + 1;
        int value = 100;
        int sum = (roll1 * value);
        runningTotal += sum;
        totalmoneyLabel.Text = runningTotal.ToString("c");
        rollCount += 1;

        if (roll1 == 1)
        {
                //diceBox1.Image = GAME.Properties.Resources._1Die;
        }

    }
}
于 2012-10-30T06:24:21.167 回答