我在使用Javascript時出現隨機數字生成器的問題。我正在嘗試製作一款小型遊戲,它可以讓玩家更快速地知道誰贏了,而不必重新擲骰子。你擲骰子兩次,並比較,誰獲得更高的數字勝。Javascript:隨機數發生器總是顯示相同的數字
但是,「殺手」總是贏或者有平局。倖存者永遠不會贏(可能是30次中的1次?)。這是我的代碼:我試圖做出殺手數少
<!DOCTYPE html>
<html>
<head>
<title>DBD Game Random Factor Generator</title>
<script>
function CalcChase() {
killer = 0 ;
surv = 0 ;
chase = 0 ;
killer1 = Math.floor(Math.random() * 12);
killer2 = Math.floor(Math.random() * 12);
killer3 = Math.floor(Math.random() * 12);
killer4 = Math.floor(Math.random() * 12);
killer5 = Math.floor(Math.random() * 12);
surv1 = Math.floor(Math.random() * 12);
surv2 = Math.floor(Math.random() * 12);
surv3 = Math.floor(Math.random() * 12);
surv4 = Math.floor(Math.random() * 12);
surv5 = Math.floor(Math.random() * 12);
var res1, res2, res3, res4, res5 ;
if (killer1 > surv1) {
res1 = "killer";
killer++;
} else if (killer1 = surv1){
res1 = "draw";
chase++;
} else {
res1 = "survivor";
surv++;
}
if (killer2 > surv2) {
res2 = "killer";
killer++;
} else if (killer2 = surv2){
res2 = "draw";
chase++;
} else {
res2 = "survivor";
surv++;
}
if (killer3 > surv3) {
res3 = "killer";
killer++;
} else if (killer3 = surv3){
res3 = "draw";
chase++;
} else {
res3 = "survivor";
surv++;
}
if (killer4 > surv4) {
res4 = "killer";
killer++;
} else if (killer4 = surv4){
res4 = "draw";
chase++;
} else {
res4 = "survivor";
surv++;
}
if (killer5 > surv5) {
res5 = "killer";
killer++;
} else if (killer5 = surv5){
res5 = "draw";
chase++;
} else {
res5 = "survivor";
surv++;
}
document.getElementById("chase1").innerHTML = "<table><tr><td>" + killer1 + "</td><td> - </td><td>" + surv1 + "</td><td>" + res1 + "</td></tr><tr><td> " + killer2 + "</td> <td> - </td> <td>" + surv2 + "</td><td>" + res2 + "</td></tr><tr><td> " + killer3 + "</td> <td> - </td> <td>" + surv3 + "</td><td>" + res3 + "</td></tr><tr><td> " + killer4 + "</td> <td> - </td> <td>" + surv4 + "</td><td>" + res4 + "</td></tr><tr><td> " + killer5 + "</td> <td> - </td> <td>" + surv5 + "</td><td>" + res5 + "</td></tr> </table>" + "<p>" + "killer: " + killer + "<br>survivor: " + surv + "<br>chase: " + chase ;
}
</script>
</head>
<body>
<button onclick="CalcChase()">Try it</button>
<p id="chase1">Results of the game.</p>
</body>
</html>
,但不知何故,兇手仍然贏得或有平局。感謝您的閱讀和幫助!
'killer1 = surv1'需要'killer1 == surv1'! –
http://dilbert.com/strip/2001-10-25 – zerkms
請注意'Math.floor(Math.random()* 12)'不是「扔(6面)骰子兩次(和求和麪)」 ,而是扔一個12面的骰子。爲了得到與兩個骰子相同的分佈,您需要使用'Math.floor(Math.random()* 6)+ Math.floor(Math.random()* 6)'。 (並不是說你的戰鬥人員獲勝的頻率會有所不同) – Bergi