我在Visual Studio Community 2015中建立了一个骰子滚动模拟器,并且遇到了一个奇怪的故障。我可以通过一个随机数获得底边值,甚至得到骰子的相反顶边值 我的问题是,有时当获得骰子正面的值时,它给我的数字与顶部或底部相同。 我没有编写太长时间,但我会假设我输入的嵌套if语句强制前骰子侧值再次“取出”,如果该值与顶部或底部值相同。 / p>
在代码中试图确定骰子正面值的部分,似乎忽略了嵌套的“if”语句。 我已经运行了程序,大概每15个卷它会抛出一个我试图过滤掉的值。顶边值为1或6的滚动不会引起问题,只有当我需要它才能重新滚动数字2到5时。
这是我的代码。我承认,它有点草率但是随着时间的推移我想把它修剪下来并简化它。
{
"id" : "29976",
"name" : "20121416306",
"type" : "Contract",
"key" : "29976",
"_parents" : [{
"id" : "83979",
"name" : "2626-41C",
"type" : "Invoice",
"status" : "Completed",
"key" : "29976",
"amount" : "205853.71000000002",
"invoiceType" : "ECMS"
}, {
"id" : "86985",
"name" : "2626-42A",
"type" : "Invoice",
"status" : "Completed",
"key" : "29976",
"amount" : "291395.12",
"invoiceType" : "ECMS"
}, {
"id" : "86988",
"name" : "2626-42C",
"type" : "Invoice",
"status" : "Completed",
"key" : "29976",
"amount" : "220331.0",
"invoiceType" : "ECMS"
}, {
"id" : "90455",
"name" : "2626-43A",
"type" : "Invoice",
"status" : "Completed",
"key" : "29976",
"amount" : "277177.64",
"invoiceType" : "ECMS"
}, {
"id" : "90478",
"name" : "2626-43L",
"type" : "Invoice",
"status" : "Pending",
"key" : "29976",
"amount" : "743964.34",
"invoiceType" : "ECMS"
}, {
"id" : "20189",
"name" : "TO13ECTPSE00012",
"type" : "TaskOrder",
"key" : "29976",
"_parents" : [{
"id" : "38737",
"name" : "2626-26A",
"type" : "Invoice",
"status" : "Completed",
"key" : "20189",
"amount" : "243298.23",
"invoiceType" : "ECMS"
}
]
}, {
"id" : "21541",
"name" : "TO13TDSBIC00122",
"type" : "TaskOrder",
"key" : "29976",
"_parents" : [{
"id" : "86989",
"name" : "2626-42S",
"type" : "Invoice",
"status" : "Completed",
"key" : "21541",
"amount" : "344.78",
"invoiceType" : "ECMS"
}
]
}, {
"id" : "20906",
"name" : "TO13ECTPSE00031",
"type" : "TaskOrder",
"key" : "29976",
"_parents" : [{
"id" : "90477",
"name" : "2626-43F",
"type" : "Invoice",
"status" : "Pending",
"key" : "20906",
"amount" : "4729.41",
"invoiceType" : "ECMS"
}, {
"id" : "69000",
"name" : "2626-36F",
"type" : "Invoice",
"status" : "Completed",
"key" : "20906",
"amount" : "7869.54",
"invoiceType" : "ECMS"
}, {
"id" : "56285",
"name" : "2626-32F",
"type" : "Invoice",
"status" : "Completed",
"key" : "20906",
"amount" : "7423.15",
"invoiceType" : "ECMS"
}
]
}, {
"id" : "21461",
"name" : "TO13TDSBIC00105",
"type" : "TaskOrder",
"key" : "29976",
"_parents" : [{
"id" : "4",
"name" : "20140002641",
"type" : "Invoice",
"status" : "Submitted",
"key" : "21461",
"amount" : "55823035.44",
"invoiceType" : "VC"
}
]
}, {
"id" : "22022",
"name" : "TO13TDSBIC00228",
"type" : "TaskOrder",
"key" : "29976",
"_parents" : [{
"id" : "86992",
"name" : "2626-42X",
"type" : "Invoice",
"status" : "Completed",
"key" : "22022",
"amount" : "59876.9",
"invoiceType" : "ECMS"
}, {
"id" : "83988",
"name" : "2626-41X",
"type" : "Invoice",
"status" : "Completed",
"key" : "22022",
"amount" : "392.52",
"invoiceType" : "ECMS"
}
]
}
]
}
答案 0 :(得分:0)
您的设计不必要地复杂化。
模具有24种可能的方向。您已将这些存储在数据结构中。
您只需要调用rnd()
,然后选择数据结构的相应元素,将TopSide()
设置为您选择的数组中的第一个元素,等等上。
答案 1 :(得分:0)
不确定我是否完全理解它,但是你不是让它变得比实际上要复杂得多吗?您应该通过Random
调用获取正面值。所有其他面孔将自动获得锁定,不是吗?因此,您无需再次使用Random
来获取其他方面的值。您只需要创建一个Dictionary<int, int[]>
,其中包含6个元素(FRONT face的每个值一个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];