Javascript:随机数生成器始终显示相同的数字

时间:2017-05-16 22:40:36

标签: javascript

我在Javascript上遇到了随机数生成器的问题。我试图制作一款小游戏,让你更快地知道谁赢了而不必再次掷骰子。你掷骰子两次比较,谁获得更高的数字获胜。

然而,"杀手"总是胜利或者有平局。幸存者永远不会赢(可能是30次中的一次?)。这是我的代码:

    <!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>

我试图让这个数字更小的杀手,但不知何故杀手仍然获胜或者有平局。感谢您的阅读和帮助!

1 个答案:

答案 0 :(得分:0)

您在else if条款中使用分配运算符代替比较运算符,例如这里:

} else if (killer1 = surv1){

这应该是这样的:

} else if (killer1 == surv1){

(注意双等号)。在您的代码中,您使用killer1的值覆盖surv1的值,只要该值大于零,它就会{{1}分支。只有偶然else if的值等于0才会surv1分支。