2015-12-11 95 views
0

我在Visual Studio Community 2015中建立了一個骰子滾動模擬器,並且遇到了奇怪的故障。我可以通過隨機數得到底部值,甚至可以得到骰子的相反頂端值。
我的問題是,有時當獲得骰子正面的值時,它會給出與頂部或底部相同的數字。 我沒有被編碼太久,但我會假設我已經輸入的嵌套if語句強制將前面的骰子邊值再次「提取」,如果該值與頂部或底部的值相同。骰子模擬器hickup

在代碼中試圖確定骰子正面值的部分,它好像忽略了嵌套的「if」語句。 我已經運行該程序,並且每調用一次它就會拋出一個值,我嘗試過濾掉。上面值爲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); 

    } 

回答

0

您的設計是不必要的複雜。

模具有24種可能的方向。您已經將這些存儲在數據結構中。

所有您需要做的就是對rnd()進行一次調用,然後選擇數據結構的相應元素,將TopSide()設置爲您選擇的數組中的第一個元素,依此類推。

0

不知道我是否完全理解它,但是不是讓它變得比實際更復雜?您應該通過撥打Random獲得Front face的價值。所有其他面孔將自動獲得鎖定,不是?所以你不需要再次使用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];