0

我有一个掷骰子游戏。我希望骰子值从6 to 12下降频率降低,骰子值从2 to 5下降频率下降。

骰子值应如下表所示

  • 如果 UserCount 是,600or1200or1800...那么骰子值应该是6
  • 如果 UserCount 是,700or1400or2100...那么骰子值应该是7
  • 如果 UserCount 是,800or1600or2400...那么骰子值应该是8
  • 如果 UserCount 是,900or1800or2700...那么骰子值应该是9
  • 如果 UserCount 是,1000or2000or3000...那么骰子值应该是10
  • 如果 UserCount 是,1100or2200or3300...那么骰子值应该是11
  • 如果 UserCount 是,1200or2400or3600...那么骰子值应该是12

所以就像如果情况是 600or1200 => 6 和 1200 => 12 那么骰子值应该是 6 如果是 1600or2400 => 8 和 1200or2400 =>12 那么骰子值应该是 8

对于其余的用户计数,骰子值应该2 to 5随机下降。

我一次掷 2 个骰子,每个骰子有 6 面。当我掷骰子时,如果它落在 2 到 5 之间,那么我将向用户显示相同的值。但如果它大于 5,那么我将检查用户数,并且必须修复问题中指定的值

我将用户计数保持在table. 这是一个windows application. 请帮助。

我想出了下面的代码

public static String Roll(Int32 numberOfDice, Int32 numberOfSides, Int32 diceRolled)
        {

            if (numberOfDice <= 0)
            {
                throw new ApplicationException("Number of die must be greater than zero.");
            }

            if (numberOfSides <= 0)
            {
                throw new ApplicationException("Number of sides must be greater than zero.");
            }

            Random rnd = new Random((Int32)DateTime.Now.Ticks);
            StringBuilder result = new StringBuilder();
            Int32 total = 0;

            for (Int32 i = 1; i < numberOfDice; i++)
            {

                Int32 roll = rnd.Next(2, numberOfSides);
                total += roll;

                if (total > 5)
                {
                    if (diceRolled % 6 == 0)
                    {
                        total = 6;
                    }
                    else if (diceRolled % 7 == 0)
                    {
                        total = 7;
                    }
                    else if (diceRolled % 8 == 0)
                    {
                        total = 8;
                    }
                    else if (diceRolled % 9 == 0)
                    {
                        total = 9;
                    }
                    else if (diceRolled % 10 == 0)
                    {
                        total = 10;
                    }
                    else if (diceRolled % 11 == 0)
                    {
                        total = 11;
                    }
                    else if (diceRolled % 12 == 0)
                    {
                        total = 12;
                    }
                    else
                    {
                        Dice.Roll(2, 12, diceRolled);
                    }
                }

            }

            Console.WriteLine("Total{0}", Convert.ToString(total));

            return Convert.ToString(total);
        }

请提出任何改进建议。

4

1 回答 1

1

我认为应该这样做:

    public static int RollDice(int userCount)
    {
        //Reduce the user count ex: 600 => 6
        var reducedValue = userCount/100;

        //Process numbers 6-12
        for (int i = 6; i <= 12; i++)
        {
            //Create a trials list 
            var trials = new[]
                         {
                             reducedValue/i,
                             reducedValue/(i*2),
                             reducedValue/(i*3),
                         };

            //If any of the trials for this iteration passed
            //i.e. divided evenly, then return our index
            if (trials.Any(t => t == 1))
            {
                return i;
            } 
        }

        //Ok, just return a random number from 2 to 5
        var r = new Random();
        return  r.Next(2, 5);
    }
于 2013-05-23T05:18:32.933 回答