我需要从游戏中返回一个变量

时间:2014-04-11 16:38:40

标签: javascript variables loops methods

正如我现在拥有的游戏一样,它将循环播放用户选择的次数,我需要获取播放的金额并将获胜金额除以获得胜率百分比。我不知道如何解决这个问题,所以任何帮助都表示赞赏。提前谢谢。

 function rockPaperScissors(){
            function game(wantToPlay){
                if (wantToPlay=="Yes"||wantToPlay=="yes"){
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    //gets a random number
                    var computerChoice = Math.random();

                    //assigns that random number to be either rock, paper or scissors based on the number
                    if (computerChoice < .34) {
                        computerChoice = "rock";}
                    else if(computerChoice <= .66) {
                        computerChoice = "paper";}
                    else {
                        computerChoice = "scissors";
                    }
                    console.log("computerChoice: " + computerChoice);
                    console.log("userChoice: " + userChoice);

                    //compares the computer choice to the user choice and then prints out the outcome on the page.
                    function compare(choice1,choice2){
                        console.log(choice1 + "<-----User");
                        console.log(choice2 + "<-----Computer");
                        if (choice1==choice2){
                            alert("It was a tie!");
                            game("yes");
                        }
                        if (choice1=="rock"){
                            if (choice2=="scissors"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
                                document.getElementById("loss").innerHTML="";
                            }
                            if (choice2 =="paper"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";

                            }
                            /**else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Rock crushes scissors";
                                document.getElementById("win").innerHTML="";
                            }**/
                        }
                        else if (choice1=="paper"){
                            if (choice2=="rock"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Paper smothers rock.";
                                document.getElementById("loss").innerHTML="";
                            }
                            if (choice2 =="scissors"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";

                            }
                           /** else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
                                document.getElementById("win").innerHTML="";
                            }**/
                        }
                        else if (choice1=="scissors"){
                            if (choice2=="paper"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Scissors cut paper.";
                                document.getElementById("loss").innerHTML="";
                            }
                            if (choice2 =="rock"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
                            }
                           /** else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
                                document.getElementById("win").innerHTML="";
                            }**/
                        }
                        else{
                            alert("Very funny. Read the help menu and do it right.");
                            game("yes");
                        }

                    };
                    compare(userChoice,computerChoice);
                }

                //this only runs if the user does not type yes
                else{
                    document.getElementById("messages").innerHTML="Well alrighty then.";
                    document.getElementById("loss").innerHTML="";
                    document.getElementById("win").innerHTML="";
                }
            }
            //promts the start of the game and loops x amount of times based on user input.
            var start = prompt ("Do you want to play?","Yes");
            var i = prompt("How many times do you want to play?", 5);
            for (n = 0; n < i; n ++){
            game(start);
        }}

1 个答案:

答案 0 :(得分:0)

声明变量,如(<body onload="initVars()">和创建函数initVars()

var wins=0;
var plays=0;

当玩家获胜时,只有wins++时才会plays++plays++失败。wins/plays。 当游戏结束时,你会{{1}}。