为什么我的随机性不检查随机数生成工作?

时间:2019-12-04 01:03:16

标签: javascript html

一些背景信息,这是一个Rock Paper Scissor Spock和Lizard游戏,有2名玩家,人类对ai。 到目前为止,一切正常,并且有一个表格可以让您查看谁获胜,但我想要的是程序的真正“随机性”。例如,我想查看100次播放的结果并将其与理想的解决方案进行比较(每个变量平均分配20/100(在这种情况下为5))。

重要提示!!请记住,我编写的代码应在控制台中运行,没有可见的表格可供检查。

这是我的.js文件代码,底部是应该进行随机性检查的地方

var Rock = document.getElementById('Rock').addEventListener('click', run);
var Paper = document.getElementById('Paper').addEventListener('click', run);
var Scissors = document.getElementById('Scissors').addEventListener('click', run);
var Lizard = document.getElementById('Lizard').addEventListener('click', run);
var Spock = document.getElementById('Spock').addEventListener('click', run);

var computerChoice;
var playerChoice;

var player = document.getElementById('player');
var computer = document.getElementById('computer');
var outcome = document.getElementById('outcome');

function run() {
    playerChoice = this.innerText;
    computerGamble();
    compare();
}
var playerChoice = 0
var computerChoice = 0

localStorage.setitem('win', '1')
localStorage.setitem('loss','2')

function computerGamble() {
    var dice = Math.random();
    if (dice <= 0.2) {
        computerChoice = "Rock";
    } else if (dice <= 0.4) {
        computerChoice = "Paper";
    } else if (dice <= 0.6) {
        computerChoice = "Scissors";
    } else if (dice <= 0.8) {
      computerChoice = "Lizard";
    } else{
      computerChoice = "Spock";
    }
}


 function compare() {
    player.innerHTML = "You have chosen " + playerChoice;
    computer.innerHTML = "Computer chooses " + computerChoice;

    if (playerChoice == computerChoice) {
        outcome.innerHTML = "Stalemate";

    } else if (playerChoice === "Rock" && computerChoice === "Paper") {
        outcome.innerHTML = "Paper has trapped the rock, you lose!";
     counter.loss += 1;
    } else if (playerChoice === "Rock" && computerChoice === "Scissors") {
        outcome.innerHTML = "Rock has broken the scissors, you win!";
   counter.win += 1;

    } else if (playerChoice === "Paper" && computerChoice === "Rock") {
        outcome.innerHTML = "Rock has been trapped by paper, you win!";
    } else if (playerChoice === "Paper" && computerChoice === "Scissors") {
        outcome.innerHTML = "Scissors snap the paper in half, you lose!";


    } else if (playerChoice === "Scissors" && computerChoice === "Rock") {
        outcome.innerHTML = "Rock has broken the scissors, you lose!";
    } else if (playerChoice === "Scissors" && computerChoice === "Paper") {
        outcome.innerHTML = "Scissor snaps the paper in half, you win!";

  } else if (playerChoice === "Rock" && computerChoice === "Spock") {
    outcome.innerHTML = "Spock vaporizes rock, you lose!";
  } else if (playerChoice === "Rock" && computerChoice === "Lizard") {
    outcome.innerHTML = "Lizard gets knocked unconcious by rock, victory!";


  } else if (playerChoice === "Paper" && computerChoice === "Spock") {
    outcome.innerHTML = "Spock has been disproved by paper, you win!";
  } else if (playerChoice === "Paper" && computerChoice === "Lizard") {
    outcome.innerHTML = "Lizard ate the paper, defeat!";


  } else if (playerChoice === "Scissors" && computerChoice === "Spock") {
    outcome.innerHTML = "Spock smashes scissors, you lose!";
  } else if (playerChoice === "Scissors" && computerChoice === "Lizard") {
    outcome.innerHTML = "Victory!";

  } else if (playerChoice === "Lizard" && computerChoice === "Scissors") {
    outcome.innerHTML = "Defeat!";
  } else if (playerChoice === "Lizard" && computerChoice === "Spock") {
    outcome.innerHTML = "Victory!";


  } else if (playerChoice === "Lizard" && computerChoice === "Paper") {
    outcome.innerHTML = "Victory!";
  } else if (playerChoice === "Lizard" && computerChoice === "Rock") {
    outcome.innerHTML = "Defeat!";


  } else if (playerChoice === "Spock" && computerChoice === "Paper") {
    outcome.innerHTML = "Defeat!";
  } else if (playerChoice === "Spock" && computerChoice === "Rock") {
    outcome.innerHTML = "Victory!";

  } else if (playerChoice === "Spock" && computerChoice === "Lizard") {
    outcome.innerHTML = "Defeat!";
  } else if (playerChoice === "Spock" && computerChoice === "Scissors") {
    outcome.innerHTML = "Victory!";

    }
 }
 function random(){
 total = 0;
 expected = 0;
 margin = 0;

 total = loop;
 loop = 0;
 expected = total * 0.2;
 margin = expected * 0.05;

     //player1 randomness start
     if(p1r == expected){
         console.log("Player1: rock is random");
         p1r = 0;
     }else{
         if(p1r == p1r - margin){
             console.log("Player1: rock is random");
             p1r = 0;
         }else{
             if(p1r == p1r + margin){
                 console.log("Player1: rock is random");
                 p1r = 0;
             }else{
                 console.log("player1: rock is not random");
                 p1r = 0;
             }
         }
     }

     if(p1p == expected){
         console.log("Player1: paper is random");
         p1p = 0;
     }else{
         if(p1p == p1p - margin){
             console.log("Player1: paper is random");
             p1p = 0;
         }else{
             if(p1p == p1p + margin){
                 console.log("Player1: paper is random");
                 p1p = 0;
             }else{
                 console.log("player1: paper is not random");
                 p1p = 0;
             }
         }
     }

     if(p1s == expected){
         console.log("Player1: scissor is random");
         p1s = 0;
     }else{
         if(p1s == p1s - margin){
             console.log("Player1: scissor is random");
             p1s = 0;
         }else{
             if(p1s == p1s + margin){
                 console.log("Player1: scissor is random");
                 p1s = 0;
             }else{
                 console.log("player1: scissor is not random");
                 p1s = 0;
             }
         }
     }

     if(p1l == expected){
         console.log("Player1: lizard is random");
         p1l = 0;
     }else{
         if(p1l == p1l - margin){
             console.log("Player1: lizard is random");
             p1l = 0;
         }else{
             if(p1l == p1l + margin){
                 console.log("Player1: lizard is random");
                 p1l = 0;
             }else{
                 console.log("player1: lizard is not random");
                 p1l = 0;
             }
         }
     }

     if(p1sp == expected){
         console.log("Player1: spock is random");
         p1sp = 0;
     }else{
         if(p1sp == p1sp - margin){
             console.log("Player1: spock is random");
             p1sp = 0;
         }else{
             if(p1sp == p1sp + margin){
                 console.log("Player1: spock is random");
                 p1sp = 0;
             }else{
                 console.log("player1: spock is not random");
                 p1sp = 0;
             }
         }
     }
     //player1 ramdomness end

     //player2 randomness start
      if(p2r == expected){
         console.log("Player2: rock is random");
         p2r = 0;
     }else{
         if(p2r == p2r - margin){
             console.log("Player2: rock is random");
             p2r = 0;
         }else{
             if(p2r == p2r + margin){
                 console.log("Player2: rock is random");
                 p2r = 0;
             }else{
                 console.log("Player2: rock is not random");
                 p2r = 0;
             }
         }
     }

     if(p2p == expected){
         console.log("Player2: paper is random");
         p2p = 0;
     }else{
         if(p2p == p2p - margin){
             console.log("Player2: paper is random");
             p2p = 0;
         }else{
             if(p2p == p2p + margin){
                 console.log("Player2: paper is random");
                 p2p = 0;
             }else{
                 console.log("Player2: paper is not random");
                 p2p = 0;
             }
         }
     }

     if(p2s == expected){
         console.log("Player2: scissor is random");
         p2s = 0;
     }else{
         if(p2s == p2s - margin){
             console.log("Player2: scissor is random");
             p2s = 0;
         }else{
             if(p2s == p2s + margin){
                 console.log("Player2: scissor is random");
                 p2s = 0;
             }else{
                 console.log("Player2: scissor is not random");
                 p2s = 0;
             }
         }
     }

     if(p2l == expected){
         console.log("Player2: lizard is random");
         p2l = 0;
     }else{
         if(p2l == p2l - margin){
             console.log("Player2: lizard is random");
             p2l = 0;
         }else{
             if(p2l == p2l + margin){
                 console.log("Player2: lizard is random");
                 p2l = 0;
             }else{
                 console.log("Player2: lizard is not random");
                 p2l = 0;
             }
         }
     }

     if(p2sp == expected){
         console.log("Player2: spock is random");
         p2sp = 0;
     }else{
         if(p2sp == p2sp - margin){
             console.log("Player2: spock is random");
             p2sp = 0;
         }else{
             if(p2sp == p2sp + margin){
                 console.log("Player2: spock is random");
                 p2sp = 0;
             }else{
                 console.log("Player2: spock is not random");
                 p2sp = 0;
             }
         }
     }
     //player2 ramdomness end

这是我的HTML文件代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>RPSLS </title>
        <script src="https://randojs.com/1.0.0.js"></script>
        <script>
            var p1r = 0;
            var p2r
            function onload(){
                document.getElementById('Rock').addEventListener('click', run);
                document.getElementById('Paper').addEventListener('click', run);
                document.getElementById('Scissors').addEventListener('click', run);
                document.getElementById('Lizard').addEventListener('click', run);
                document.getElementById('Spock').addEventListener('click', run);
            }

            function run() {
                var playerChoice = this.innerText;
                var computerChoice = rando(["Rock", "Paper", "Scissors", "Lizard", "Spock"]).value;

                document.getElementById('player').innerHTML = "You have chosen " + playerChoice;
                document.getElementById('computer').innerHTML = "Computer chooses " + computerChoice;

                if(playerChoice == computerChoice) {
                    //tie
                    document.getElementById('outcome').innerHTML = "Stalemate";
                    addToRecord(playerChoice, computerChoice, "tied");
                }
                else if(playerChoice == "Rock" && ["Spock", "Paper"].indexOf(computerChoice) > -1 ||
                    playerChoice == "Paper" && ["Scissors", "Lizard"].indexOf(computerChoice) > -1 ||
                    playerChoice == "Scissors" && ["Rock, Spock"].indexOf(computerChoice) > -1 ||
                    playerChoice == "Lizard" && ["Rock", "Scissors"].indexOf(computerChoice) > -1 ||
                    playerChoice == "Spock" && ["Paper", "Lizard"].indexOf(computerChoice) > -1){
                    //player loses
                    document.getElementById('outcome').innerHTML = getActionPhrase([playerChoice, computerChoice]) + ", you lose!";
                    addToRecord(playerChoice, computerChoice, "lost");
                }
                else{
                    //player wins
                    document.getElementById('outcome').innerHTML = getActionPhrase([playerChoice, computerChoice]) + ", you win!";
                    addToRecord(playerChoice, computerChoice, "won");
                }
            }

            function getActionPhrase(choiceArray){
                if(choiceArray.indexOf("Rock") > -1 && choiceArray.indexOf("Paper") > -1) return "Paper has trapped the rock";
                if(choiceArray.indexOf("Rock") > -1 && choiceArray.indexOf("Scissors") > -1) return "Rock has broken the scissors";
                if(choiceArray.indexOf("Rock") > -1 && choiceArray.indexOf("Lizard") > -1) return "Lizard gets knocked unconcious by rock";
                if(choiceArray.indexOf("Rock") > -1 && choiceArray.indexOf("Spock") > -1) return "Spock vaporizes rock";
                if(choiceArray.indexOf("Paper") > -1 && choiceArray.indexOf("Scissors") > -1) return "Scissors snap the paper in half";
                if(choiceArray.indexOf("Paper") > -1 && choiceArray.indexOf("Lizard") > -1) return "Lizard ate the paper";
                if(choiceArray.indexOf("Paper") > -1 && choiceArray.indexOf("Spock") > -1) return "Spock has been disproved by paper";
                if(choiceArray.indexOf("Scissors") > -1 && choiceArray.indexOf("Lizard") > -1) return "Scissors have decapitated the lizard";
                if(choiceArray.indexOf("Scissors") > -1 && choiceArray.indexOf("Spock") > -1) return "Spock smashes the scissors";
                if(choiceArray.indexOf("Lizard") > -1 && choiceArray.indexOf("Spock") > -1) return "Lizard poisons Spock";
            }

            function addToRecord(playerChoice, computerChoice, result){
                document.getElementById('record').innerHTML += "<tr><td>" + playerChoice + "</td><td>" + computerChoice + "</td><td>" + result + "</td></tr>";
                var recordSpan = document.getElementById('record').getElementsByTagName('span')[["won", "lost", "tied"].indexOf(result)];
                recordSpan.innerHTML = Number(recordSpan.innerHTML) + 1;
            }
    </head>
    <body onload="onload();">
        <h1> RPSLS (RPSLS PROJECT) </h1>
        <h2>Rock, scissors, paper, lizard, spock, shake..!</h2>
        <iframe width="560" height="315" src="https://www.youtube.com/embed/x5Q6-wMx-K8" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

        <div class="button-box">
            <button id="Rock">Rock</button>
            <button id="Paper">Paper</button>
            <button id="Scissors">Scissors</button>
            <button id="Lizard">Lizard</button>
            <button id="Spock">Spock</button>
        </div>
        <h5 id="player"></h5>
        <h5 id="computer"></h5>
        <h3 id="outcome"></h3>
        <table id="record">
            <tr>
                <td><span>0</span> = Your wins</td>
                <td><span>0</span> = Your losses</td>
                <td><span>0</span> = Your ties</td>
            </tr>
        </table>
    </body>
</html>
</div>
<!DOCTYPE html>
<html>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

这是一个更新的HTML文件,该文件将在控制台中登录每次游戏一轮的计算机/玩家选择是否在预期的幅度之内。我注释掉了显示在实际页面上的HTML表,因为我认为这是您想要的,而不是您想要的。如果您仍然想要它,请随时取消注释。非常清楚,您仅需要两个文件就是这个文件和您的CSS文件。将该名称命名为任意名称,但请确保文件扩展名为“ .html”。然后将您的CSS文件命名为“ theme.css”,并将其放置在该HTML文件所在的文件夹中。然后您应该会很高兴。

Name

此文件要求您实际动手玩每局并在控制台中获得结果。如果您不想进行100次操作,则可以选择在打开的HTML文件的控制台中运行此操作,以模拟100次选择:

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>RPSLS </title>
            <script src="https://randojs.com/1.0.0.js"></script>
            <link href="theme.css" rel="stylesheet">
            <script>
                function onload(){
                    document.getElementById('Rock').addEventListener('click', run);
                    document.getElementById('Paper').addEventListener('click', run);
                    document.getElementById('Scissors').addEventListener('click', run);
                    document.getElementById('Lizard').addEventListener('click', run);
                    document.getElementById('Spock').addEventListener('click', run);
                }

                function run() {
                    var playerChoice = this.innerText;
                    var computerChoice = rando(["Rock", "Paper", "Scissors", "Lizard", "Spock"]).value;

                    document.getElementById('player').innerHTML = "You have chosen " + playerChoice;
                    document.getElementById('computer').innerHTML = "Computer chooses " + computerChoice;

                    if(playerChoice == computerChoice) {
                        //tie
                        document.getElementById('outcome').innerHTML = "Stalemate";
                        addToRecord(playerChoice, computerChoice, "tied");
                    }
                    else if(playerChoice == "Rock" && ["Spock", "Paper"].indexOf(computerChoice) > -1 || 
                        playerChoice == "Paper" && ["Scissors", "Lizard"].indexOf(computerChoice) > -1 || 
                        playerChoice == "Scissors" && ["Rock, Spock"].indexOf(computerChoice) > -1 || 
                        playerChoice == "Lizard" && ["Rock", "Scissors"].indexOf(computerChoice) > -1 || 
                        playerChoice == "Spock" && ["Paper", "Lizard"].indexOf(computerChoice) > -1){
                        //player loses
                        document.getElementById('outcome').innerHTML = getActionPhrase([playerChoice, computerChoice]) + ", you lose!";
                        addToRecord(playerChoice, computerChoice, "lost");
                    }
                    else{
                        //player wins
                        document.getElementById('outcome').innerHTML = getActionPhrase([playerChoice, computerChoice]) + ", you win!";
                        addToRecord(playerChoice, computerChoice, "won");
                    }
                }

                function getActionPhrase(choiceArray){
                    if(choiceArray.indexOf("Rock") > -1 && choiceArray.indexOf("Paper") > -1) return "Paper has trapped the rock";
                    if(choiceArray.indexOf("Rock") > -1 && choiceArray.indexOf("Scissors") > -1) return "Rock has broken the scissors";
                    if(choiceArray.indexOf("Rock") > -1 && choiceArray.indexOf("Lizard") > -1) return "Lizard gets knocked unconcious by rock";
                    if(choiceArray.indexOf("Rock") > -1 && choiceArray.indexOf("Spock") > -1) return "Spock vaporizes rock";
                    if(choiceArray.indexOf("Paper") > -1 && choiceArray.indexOf("Scissors") > -1) return "Scissors snap the paper in half";
                    if(choiceArray.indexOf("Paper") > -1 && choiceArray.indexOf("Lizard") > -1) return "Lizard ate the paper";
                    if(choiceArray.indexOf("Paper") > -1 && choiceArray.indexOf("Spock") > -1) return "Spock has been disproved by paper";
                    if(choiceArray.indexOf("Scissors") > -1 && choiceArray.indexOf("Lizard") > -1) return "Scissors have decapitated the lizard";
                    if(choiceArray.indexOf("Scissors") > -1 && choiceArray.indexOf("Spock") > -1) return "Spock smashes the scissors";
                    if(choiceArray.indexOf("Lizard") > -1 && choiceArray.indexOf("Spock") > -1) return "Lizard poisons Spock";
                }

                var roundsPlayed = 0;
                var computerPlayed = {
                    Rock: 0,
                    Paper: 0, 
                    Scissors: 0, 
                    Lizard: 0, 
                    Spock: 0
                };
                var playerPlayed = {
                    Rock: 0,
                    Paper: 0, 
                    Scissors: 0, 
                    Lizard: 0, 
                    Spock: 0
                };
                function addToRecord(playerChoice, computerChoice, result){
                    var expected = .2;
                    var margin = 0.05;
                    console.log("\n\nROUND " + (roundsPlayed + 1) + ":");
                    if(++computerPlayed[computerChoice] / ++roundsPlayed >= expected - margin && computerPlayed[computerChoice] / roundsPlayed <= expected + margin){
                        console.log("Computer played " + computerChoice + " within our expected margin.");
                    }
                    else{
                        console.log("Computer played " + computerChoice + " OUTSIDE OF our expected margin.");
                    }

                    if(++playerPlayed[playerChoice] / roundsPlayed >= expected - margin && playerPlayed[playerChoice] / roundsPlayed <= expected + margin){
                        console.log("Player played " + playerChoice + " within our expected margin.");
                    }
                    else{
                        console.log("Player played " + playerChoice + " OUTSIDE OF our expected margin.");
                    }

                    document.getElementById('record').innerHTML += "<tr><td>" + playerChoice + "</td><td>" + computerChoice + "</td><td>" + result + "</td></tr>";
                    var recordSpan = document.getElementById('record').getElementsByTagName('span')[["won", "lost", "tied"].indexOf(result)];
                    recordSpan.innerHTML = Number(recordSpan.innerHTML) + 1;
                }
            </script>
            <style>
                #record{
                    margin:auto;
                    border-collapse:collapse;
                }

                #record td{
                    padding:5px 10px;
                }

                #record tbody:first-of-type td{
                    border-bottom:1px solid #000;
                }
            </style>
        </head>
        <body onload="onload();">
            <h1> For Mrs. Romero (RPSLS PROJECT) </h1>
            <h2>Rock, scissors, paper, lizard, spock, shake..!</h2>
            <div class="button-box">
                <button id="Rock">Rock</button>
                <button id="Paper">Paper</button>
                <button id="Scissors">Scissors</button>
                <button id="Lizard">Lizard</button>
                <button id="Spock">Spock</button>
            </div>
            <h5 id="player"></h5>
            <h5 id="computer"></h5>
            <h3 id="outcome"></h3>
            <table id="record">
                <tr>
                    <td><span>0</span> wins.</td>
                    <td><span>0</span> losses.</td>
                    <td><span>0</span> ties.</td>
                </tr>
            </table>
        </body>
    </html>

让我知道这是否有帮助。

答案 1 :(得分:0)

在您的示例中,我看不到任何随机数生成,因此包含了一个简单的函数,该函数将生成一个介于int到int之间的随机数。

有一个示例可以生成100个随机数,并检查它们是否在预期的目标百分比之内:

// generate random number - min and max are inclusive
function getRandom(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// 5 options
const options = 5;
const results = [];
for (let i = 0; i < options; i++) {
  results[i] = 0;
}

// generate 100 random numbers and record the result
for (let i = 0; i < 100; i++) {
  let random = getRandom(1, options);
  results[random - 1]++;
}

// output the results
console.log(results);
console.log(results.reduce((accumulator, currentValue) => accumulator + currentValue));