【问题标题】:Dice Simulator hickup骰子模拟器打嗝
【发布时间】:2015-12-11 07:21:53
【问题描述】:

我正在 Visual Studio Community 2015 中构建一个掷骰子模拟器,但遇到了一个奇怪的故障。我可以通过一个随机数获得底面值,甚至可以获得骰子的相反顶面值。
我的问题是,有时在获得骰子正面的值时,它会给我与顶部或底部相同的数字。 我没有编码太久,但我会假设我输入的嵌套 if 语句应该强制前面的骰子边值再次“获取”,如果值与顶部或底部值相同。

在尝试确定骰子正面值的代码部分中,它似乎忽略了嵌套的“if”语句。 我已经运行了该程序,大约每 15 次滚动它会抛出一个我试图过滤掉的值。顶部值为 1 或 6 的滚动不会引起问题,只有当我需要它在数字 2 到 5 上重新滚动时才会出现问题。

这是我的代码。我承认,这有点草率,但随着时间的推移,我想对其进行精简并使其流线型。

       //Dice Facings
        //This is a list of all of the possible dice facings on a 6-sided dice (also know as a D6)
        //Array Structure: Top/Right/Back/Front/Left/Bottom
        int[,] DiceCombos = { 
            {1,2,3,4,5,6}, {1,4,2,5,3,6}, {1,5,4,3,2,6}, {1,3,5,2,4,6}, 
            {2,6,3,4,1,5}, {2,3,1,6,4,5}, {2,1,4,3,6,5}, {2,4,6,1,3,5},
            {3,2,6,1,5,4}, {3,1,2,5,6,4}, {3,5,1,6,2,4}, {3,6,5,2,1,4},
            {4,2,1,6,5,3}, {4,1,5,2,6,3}, {4,5,6,1,2,3}, {4,6,2,5,1,3},
            {5,1,3,4,6,2}, {5,3,6,1,4,2}, {5,6,4,3,1,2}, {5,4,1,6,3,2},
            {6,5,4,3,2,1}, {6,4,2,5,3,1}, {6,2,3,4,5,1}, {6,3,5,2,4,1}
        };

        //To be able to use this information, once the dice are rolled.
        //Need a way to determine how the dice landed (facing wise). To do this we run a random number from 1-6 against the Front value of the dice.
        //Once a matching number is found for the FSide (Front Side) value, 
        //the program will look up the facing in a built in database (tiny one as you can tell here).
        //Say the value of Rolled = 4 (BoSide) and through random number generation, the value of FSide = 2.
        //Then the program would know that LSide (Left Side) = 1 and RSide (Right Side) = 6
        //

        // Get the Top Facing side based on the value of BoSide

        // Get a Random number for the Front Facing side.

        //Example of how to show facing based on strength of the roll 
        // Soft roll
        // NOFaces==3 ; number of times to show a top number (3rd value = final)
        // Generate random number between 1-6: Pass to BoSide and get TSide value. Show TSide value.
        // Repeat two more times, the final time being the value the dice ends up on.

        //Variables to think about when the dice are rolling.
        //When rolling the dice to the right the numbers roll from the front and from the left
        //When rolling the dice to the left the numbers roll from the front and from the right
        //Giving the dice a backspin causes the numbers to roll back a tick or two before rolling forward
        //      Soft roll: First facing goes from the back to the front.
        //      Regular roll: First 2 facings go backwards.
        //      Hard roll: Up to 5 facings go backwards.

        //On each "tick" of a roll check these things...
        //Faces; where are the numbers located on the dice.
        //Direction; which numbers will be checked for this time.
        //Corners

    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Basic check for Bottom Facing side of dice
        Random rnd = new Random();
        int BoSide = new int();
        int TopSide = new int();
        BoSide = rnd.Next(1, 6 + 1);

        // Get the value for TopSide
        if (BoSide == 1) { TopSide = 6;}  
        if (BoSide == 2) { TopSide = 5;}
        if (BoSide == 3) { TopSide = 4;}
        if (BoSide == 4) { TopSide = 3;}
        if (BoSide == 5) { TopSide = 2;}
        if (BoSide == 6) { TopSide = 1;}
        else;

        //Get the value for FSide
        int FSide = new int();
        if (TopSide == 1)
        {
            FSide = rnd.Next(2, 5 + 1);
        }
        if (TopSide == 2)
        {
            FSide = rnd.Next(1, 6 + 1);
            if (FSide == 2)
            {
                FSide = rnd.Next(1, 6 + 1);
            }
            if (FSide == 5)
            {
                FSide = rnd.Next(1, 6 + 1);
            }
        }
        if (TopSide == 3)
        {
            FSide = rnd.Next(1, 6 + 1);
            if (FSide == 3)
            {
                FSide = rnd.Next(1, 6 + 1);
            }
            if (FSide == 4)
            {
                FSide = rnd.Next(1, 6 + 1);
            }
        }
        if (TopSide == 4)
        {
            FSide = rnd.Next(1, 6 + 1);
            if (FSide == 3)
            {
                FSide = rnd.Next(1, 6 + 1);
            }
            if (FSide == 4)
            {
                FSide = rnd.Next(1, 6 + 1);
            }
        }
        if (TopSide == 5)
        {
            FSide = rnd.Next(1, 6 + 1);
            if (FSide == 5)
            {
                FSide = rnd.Next(1, 6 + 1);
            }
            if (FSide == 2)
            {
                FSide = rnd.Next(1, 6 + 1);
            }
        }
        if (TopSide == 6)
        {
            FSide = rnd.Next(2, 5 + 1);
        }

        // Get the Left, Back and Right Side Numbers
        int RSide = new int();
        int LSide = new int();
        int BSide = new int();
        if (TopSide == 1)
        {
            if (FSide == 2)
            {
                RSide = 3; BSide = 5; LSide = 4;
            }
            if (FSide == 3)
            {
                RSide = 5; BSide = 4; LSide = 2;
            }
            if (FSide == 4)
            {
                RSide = 2; BSide = 3; LSide = 5;
            }
            if (FSide == 5)
            {
                RSide = 4; BSide = 2; LSide = 3;
            }
        }
        if (TopSide == 2)
        {
            if (FSide == 1)
            {
                RSide = 4; BSide = 6; LSide = 3;
            }
            if (FSide == 3)
            {
                RSide = 1; BSide = 4; LSide = 6;
            }
            if (FSide == 4)
            {
                RSide = 6; BSide = 3; LSide = 1;
            }
            if (FSide == 6)
            {
                RSide = 3; BSide = 1; LSide = 4;
            }
        }
        if (TopSide == 3)
        {
            if (FSide == 1)
            {
                RSide = 2; BSide = 6; LSide = 5;
            }
            if (FSide == 2)
            {
                RSide = 6; BSide = 5; LSide = 1;
            }
            if (FSide == 5)
            {
                RSide = 1; BSide = 2; LSide = 6;
            }
            if (FSide == 6)
            {
                RSide = 5; BSide = 1; LSide = 2;
            }
        }
        if (TopSide == 4)
        {
            if (FSide == 1)
            {
                RSide = 5; BSide = 6; LSide = 2;
            }
            if (FSide == 2)
            {
                RSide = 1; BSide = 5; LSide = 6;
            }
            if (FSide == 5)
            {
                RSide = 6; BSide = 2; LSide = 1;
            }
            if (FSide == 6)
            {
                RSide = 5; BSide = 1; LSide = 2;
            }
        }
        if (TopSide == 5)
        {
            if (FSide == 1)
            {
                RSide = 3; BSide = 6; LSide = 4;
            }
            if (FSide == 3)
            {
                RSide = 6; BSide = 4; LSide = 1;
            }
            if (FSide == 4)
            {
                RSide = 1; BSide = 3; LSide = 6;
            }
            if (FSide == 6)
            {
                RSide = 4; BSide = 1; LSide = 3;
            }
        }
        if (TopSide == 6)
        {
            if (FSide == 2)
            {
                RSide = 3; BSide = 5; LSide = 4;
            }
            if (FSide == 3)
            {
                RSide = 5; BSide = 4; LSide = 2;
            }
            if (FSide == 4)
            {
                RSide = 2; BSide = 3; LSide = 5;
            }
            if (FSide == 5)
            {
                RSide = 4; BSide = 2; LSide = 3;
            }
        }
        // Show Values
        label5.Text = Convert.ToString(BoSide);
        label17.Text = Convert.ToString(BoSide);
        label7.Text = Convert.ToString(TopSide);
        label26.Text = Convert.ToString(TopSide);
        label14.Text = Convert.ToString(FSide);
        label28.Text = Convert.ToString(FSide);
        label15.Text = Convert.ToString(LSide);
        label13.Text = Convert.ToString(BSide);
        label12.Text = Convert.ToString(RSide);

    }

【问题讨论】:

    标签: c# visual-studio dice


    【解决方案1】:

    你的设计太复杂了。

    模具有 24 种可能的方向。您已经将这些存储在数据结构中。

    您只需调用rnd(),然后选择数据结构的相应元素,将TopSide() 设置为您选择的数组中的第一个元素,依此类推。

    【讨论】:

      【解决方案2】:

      不确定我是否完全理解它,但你是不是让它比它应该的复杂得多?您应该通过Random 调用获得正面的值。所有其他面孔都会自动锁定,不是吗?所以你不需要再次使用Random 来获取对方的值。您只需要创建一个Dictionary<int, int[]>,其中将包含 6 个元素(FRONT 面的每个值一个 KEY,而 VALUE 成员将具有其他 5 个面的值)。然后,您只需通过 Dictionary 键定位其他值的值。

      //create a Dictionary
      var Dic = new Dictionary<int, int[]>();
      
      //Add all 6 possible configs
      Dic.Add(1, {1,2,3,4,5,6});
      Dic.Add(2, {2,6,3,4,1,5});
      Dic.Add(3, {3,2,6,1,5,4});
      Dic.Add(4, {4,2,1,6,5,3});    
      Dic.Add(5, {5,1,3,4,6,2});
      Dic.Add(6, {6,5,4,3,2,1});
      
      //do a random
      Random r = new Random();
      var Front = r.Next(1, 6 + 1);
      
      //Get your Dice's config
      var DiceConfig = Dic[Front];
      

      【讨论】:

        猜你喜欢
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        • 2016-04-18
        • 2013-12-18
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        • 2012-09-28
        相关资源
        最近更新 更多