如何在提示再次播放之前显示分数? (游戏)

时间:2016-11-12 04:46:52

标签: javascript



userCounter=0
computerCounter=0
function play() {
    var userChoice = prompt("Do you choose rock, paper or scissors?");
    userChoice=userChoice.toLowerCase();
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
        computerChoice = "rock";
        } else if(computerChoice <= 0.67) {
            computerChoice = "paper";
            } else {
                computerChoice = "scissors";
                } console.log("Computer: " + computerChoice);
    while (userChoice!== 'rock' && userChoice!=='paper' && userChoice!=='scissors') {
        userChoice=prompt("Do you choose rock, paper or scissors?"); 
        }
    console.log("Player: "+userChoice)
    if (userChoice===computerChoice) {console.log("Tie!");
    if (confirm("Play again?")===true) {play()}
    return};
    if (userChoice==='rock') {
        if (computerChoice==='paper') {
            console.log("Computer wins!")
            computerCounter=computerCounter+1
            }
        else {
            console.log("You win!")
            userCounter=userCounter+1
            }
    };
    if (userChoice==='paper') {
        if (computerChoice==='scissors') {
            console.log("Computer wins!")
            computerCounter=computerCounter+1}
        else {
            console.log("You win!")
            userCounter=userCounter+1}
    };
    if (userChoice==='scissors') {
        if (computerChoice==='rock') {
            console.log("Computer wins!")
            computerCounter=compputerCounter+1
        }
        else {
            console.log("You win!")
            userCounter=userCounter+1
        }
    }
    ;
    if (confirm("Play again?")===true) {play()}
    };

play();
function score() {
    console.log("Computer: "+computerCounter)
    console.log("Player: "+userCounter)
    }
score()
&#13;
&#13;
&#13;

所以到目前为止一切都很好,但是我试图在提示之前或者同时再次播放时显示得分?&#34;这样玩家可以在决定他们之前看到得分想再玩一次。目前,你可以一遍又一遍地玩,直到你最终决定停止,然后才会显示最终得分。有任何想法吗?感觉就像一个简单的解决方案,但我在精神上块。 谢谢!

1 个答案:

答案 0 :(得分:0)

在score功能中使用alert而不是console.log。这样你就会在提示之前弹出一个弹出窗口。宾果!