c# - 计算 - RNG游戏

时间:2015-09-01 13:43:50

标签: c#

我正在制作足球比赛。为了获得触地得分,你必须获得100码。我有public static int yardsLeft = 100;。它通过一个循环,每次通过获得如此多的码。一旦yardsLeft = 0,它就会退出。我在试图弄清楚如何在最后(达阵)获得码数方面遇到困难,实际上是100码。

我有这个:

if (Program.yardsLeft >= 80)
{
    if (Program.chance >= 1 && Program.chance <= 5)
        Program.gained = Program.r.Next(80, (100 + 1));

    else if (Program.chance >= 6 && Program.chance <= 12)
        Program.gained = Program.r.Next(60, (79 + 1));

    else if (Program.chance >= 13 && Program.chance <= 24)
        Program.gained = Program.r.Next(20, (59 + 1));

    else if (Program.chance >= 25 && Program.chance <= 100)
        Program.gained = Program.r.Next(1, (19 + 1));
}
else if (Program.yardsLeft <= 79 && Program.yardsLeft >= 60)
{
    if (Program.chance >= 1 && Program.chance <= 10)
        Program.gained = Program.r.Next(60, 80);

    else if (Program.chance >= 11 && Program.chance <= 30)
        Program.gained = Program.r.Next(20, 60);

    else if (Program.chance >= 31 && Program.chance <= 100)
        Program.gained = Program.r.Next(1, 20);
}
else if (Program.yardsLeft <= 59 && Program.yardsLeft >= 40)
{
    if (Program.chance >= 1 && Program.chance <= 10)
        Program.gained = Program.r.Next(40, 60);

    else if (Program.chance >= 11 && Program.chance <= 30)
        Program.gained = Program.r.Next(20, 40);

    else if (Program.chance >= 31 && Program.chance <= 100)
        Program.gained = Program.r.Next(1, 20);
}
else if (Program.yardsLeft <= 39 && Program.yardsLeft >= 20)
{
    if (Program.chance >= 1 && Program.chance <= 10)
        Program.gained = Program.r.Next(30, 40);

    else if (Program.chance >= 11 && Program.chance <= 30)
        Program.gained = Program.r.Next(10, 30);

    else if (Program.chance >= 31 && Program.chance <= 100)
        Program.gained = Program.r.Next(1, 10);
}
else if (Program.yardsLeft <= 19)
{
    if (Program.chance >= 1 && Program.chance <= 10)
        Program.gained = Program.r.Next(15, 20);

    if (Program.chance >= 11 && Program.chance <= 30)
        Program.gained = Program.r.Next(10, 15);

    if (Program.chance >= 31 && Program.chance <= 100)
        Program.gained = Program.r.Next(1, 10);
}

首先它检查yardsLeft是否超过80,如果是,它有机会获得这么多码(最多100码)但是如果第一次通过,它不会超过20码(继续下一次检查else if (Program.yardsLeft <= 79 && Program.yardsLeft >= 60)它会回到else if (Program.yardsLeft <= 79 && Program.yardsLeft >= 60)并再次添加一些导致最多100的内容,结束时的意思,结果将通过100

实施例

if (yardsLeft is 80 or more)
    {
        // generate number somewhere through 60-80 (not ever exceed 100)
        // generate number somehwere between 20-60 (not ever exceeding 100
        // generator number somewehere between 1-20 (not ever exceeding 100)
    }
    else if (yardsLeft is between 60-20)
    {
        // generate number somewhere through 40-60 (not ever exceed 100)
        // generate number somehwere between 20-40 (not ever exceeding 100
        // generator number somewehere between 1-20 (not ever exceeding 100)
    }

最终结果CAN NOT超过100.这就是我想说的。每次循环时,我希望它生成一些数字,最后不会超过100。

2 个答案:

答案 0 :(得分:1)

一个简单的解决方案就是在剩下的代码之后放置这个块:

Program.gained = Math.Min(Program.gained, Program.yardsLeft);

这意味着如果获得的码数大于获得0所需的码数,则将其设置为剩余码数。因此,当您稍后(大概)按Program.yardsLeft递减Program.gained时,它不会低于0。

基础知识:Math.Min返回您给出的最小参数。此语句根据早期代码中给出的值将Program.gained设置为新值。这会将Program.gained的值“钳制”为低于yardsLeft。这是一个更简单的用法:

int yardsYouCanRun = 112;
int yardsYouTraveled = Math.Min(yardsYouCanRun, 100);
// yardsYouTraveled is Math.Min(112, 100), which is 100.

int yardsICanRun = 15;
int yardsITraveled = Math.Min(yardsICanRun, 100);
// yardsITraveled is Math.Min(15, 100), which is 15.

这表示您正在跟踪可从零开始运行的码数,但希望将其限制为100,因为这是您可以运行的最大距离。两者中的最小值是距离跑。你可以看到,你可以跑完整个长度,但我只能跑15码,所以你得到一个100码的触地得分,但我最终只能在15码线。这会将yardsXCanRun的值“钳制”在100码以下。

这与前面的代码行相同,但不是你或我可以运行100码,而是只有Program.yardsLeft才能运行。

答案 1 :(得分:0)

我认为你想要的是:

    if (Program.yardsLeft >= 80)
    {
        if (Program.chance >= 1 && Program.chance <= 5)
            Program.gained = Program.r.Next(80, (100 + 1));

        else if (Program.chance >= 6 && Program.chance <= 12)
            Program.gained = Program.r.Next(60, (79 + 1));

        else if (Program.chance >= 13 && Program.chance <= 24)
            Program.gained = Program.r.Next(20, (59 + 1));

        else if (Program.chance >= 25 && Program.chance <= 100)
            Program.gained = Program.r.Next(1, (19 + 1));
    }
    else if (Program.yardsLeft <= 79 && Program.yardsLeft >= 60)
    {
        if (Program.chance >= 1 && Program.chance <= 10)
            Program.gained = Program.r.Next(60, 80);

        else if (Program.chance >= 11 && Program.chance <= 30)
            Program.gained = Program.r.Next(20, 60);

        else if (Program.chance >= 31 && Program.chance <= 100)
            Program.gained = Program.r.Next(1, 20);
    }
    else if (Program.yardsLeft <= 59 && Program.yardsLeft >= 40)
    {
        if (Program.chance >= 1 && Program.chance <= 10)
            Program.gained = Program.r.Next(40, 60);

        else if (Program.chance >= 11 && Program.chance <= 30)
            Program.gained = Program.r.Next(20, 40);

        else if (Program.chance >= 31 && Program.chance <= 100)
            Program.gained = Program.r.Next(1, 20);
    }
    else if (Program.yardsLeft <= 39 && Program.yardsLeft >= 20)
    {
        if (Program.chance >= 1 && Program.chance <= 10)
            Program.gained = Program.r.Next(30, 40);

        else if (Program.chance >= 11 && Program.chance <= 30)
            Program.gained = Program.r.Next(10, 30);

        else if (Program.chance >= 31 && Program.chance <= 100)
            Program.gained = Program.r.Next(1, 10);
    }
    else if (Program.yardsLeft <= 19 && Program.yardsLeft !>= 100)
    {
        if (Program.chance >= 1 && Program.chance <= 10)
        Program.gained = Program.r.Next(15, 20);

    if (Program.chance >= 11 && Program.chance <= 30)
        Program.gained = Program.r.Next(10, 15);

    if (Program.chance >= 31 && Program.chance <= 100)
        Program.gained = Program.r.Next(1, 10);
    }
    else
   {return;}//return the value if over 100

只是你没有提出一个论据来检查在最后的其他地方剩下的码数是否超过100

另一种做你在这里的方法就是使用.yardsLeft制作随机动态,总是只允许你达到100而且永远不会有更多。

if (Program.chance >= 31 && Program.chance <= 100)
        Program.gained = Program.r.Next(1, program.yardsLeft);