繁体   English   中英

一个javascript摇滚,纸,剪刀游戏!

[英]A javascript rock, paper, scissors game!

嘿伙计们,我正在制作一个javascript摇滚,纸张,剪刀游戏! 所以代码在下面,它不起作用警报没有显示,我真的不知道我搞砸了。

function randomNumber(x,y){                 //returns a random number from an interval given
    var z;
    if (x>y) {z=x; x=y; y=z;}               // turns the interval upside down if the numbers of the interval are entered in unconsecutive order
    else if(x==y) {return x; break;}        //if there's no interval but two same digits return the number entered
    var randomNo = (y-x)*Math.random() + x; // the random value itself in float type
    Math.round(randomNo);                   // round it to integer
}

function outcomeType(value){                    //gives the type of hand-symbol in game for each player according to the random No given
    var outcome;
    if (value==1 || value==4){outcome="rock"}   //value 1 or 4 gives rock, same way with other two...
    else if(value==2) {outcome="paper"}
    else {outcome="scissors"}
    return outcome; 
}

function result(x,y){          // compares the numbers so that a winner is decided
    if(x>y){return 1}         //player A wins
    else if(x==y){return 0;} //draw
    else {return 2}         //player B wins
}

function game(){
    var a=randomNumber(1,3); // random number for player A
    var b=randomNumber(1,3);// random number for player B

    if(a!=2 && b!=2 && a!=b){ //the case rock-scissors, rocks from 1 beecomes 4 in order to beat in result()
        if(a>b){b=4}
        else{a=4}
    }

    var winner = result(a,b); // we have a winner!!!
    if (winner==1) {alert("Player A wins with"+outcomeType(a)+"against"+outcomeType(b););} // the alert should be like: Player A wins with "scissors" against "paper"
    else if (winner==0) {alert("Draw with"+outcomeType(a););}                               //draw
    else {alert("Player B wins with"+outcomeType(b)+"against"+outcomeType(a););}            //player B winning alet

}

感谢任何帮助

我认为给你自己回答这个问题的工具更重要,而不是发布一些有效的代码。 你目前如何使用javascript?

我强烈建议使用firefox + firebug,学会使用它,你就可以在几分钟内自行修复。

它指出代码中的所有3个语法错误,然后运行,但总是返回相同的值。 在游戏功能中添加断点并快速逐步显示随机功能被破坏,并且没有返回。

一个相当严重的问题是你的“randomNumber”例程实际上没有return任何东西。 最后一行应该(可能)

return Math.round(randomNo);                   // round it to integer

你有没有在游戏开始时()和游戏结束()结束时发出警报,这样你就知道那可行吗?

另外我认为你在game()块中有一些额外的分号:我认为每个警报的参数列表中都不应该有分号。

最后,randomNumber()需要在最后一行返回。 你正在计算最终价值但却没有做任何事情!

你有很多错误。 从一开始,你从不打电话给游戏,所以你的任何一个功能都没有执行 其次,调用其中的函数的警报无效。 将outcomeType(a););}更改为outcomeType(a));}

这应该让你的警报工作,这样你就可以开始调试你的逻辑了。

标记中的错误..

固定:

 <script>
    function randomNumber(x,y){                 //returns a random number from an interval given
        var z;
        if (x>y) {z=x; x=y; y=z;}               // turns the interval upside down if the numbers of the interval are entered in unconsecutive order
        else if(x==y) {return x;}        //*REMOVED INCORRECT BREAK*if there's no interval but two same digits return the number entered
        var randomNo = (y-x)*Math.random() + x; // the random value itself in float type
        Math.round(randomNo);                   // round it to integer
return(randomNo );
    }

    function outcomeType(value){                    //gives the type of hand-symbol in game for each player according to the random No given
        var outcome;
        if (value==1 || value==4){outcome="rock"}   //value 1 or 4 gives rock, same way with other two...
        else if(value==2) {outcome="paper"}
        else {outcome="scissors"}
        return outcome;
    }

    function result(x,y){          // compares the numbers so that a winner is decided
        if(x>y){return 1;}         //player A wins
        else if(x==y){return 0;} //draw
        else {return 2}         //player B wins
    }

    function game(){
        var a=randomNumber(1,3); // random number for player A
        var b=randomNumber(1,3);// random number for player B

        if(a!=2 && b!=2 && a!=b){ //the case rock-scissors, rocks from 1 beecomes 4 in order to beat in result()
            if(a>b){b=4}
            else{a=4}
        }

        var winner = result(a,b); // we have a winner!!!
        if (winner==1) {alert("Player A wins with"+outcomeType(a)+"against"+outcomeType(b));} //*REMOVED EXTRA SEMICOLON* the alert should be like: Player A wins with "scissors" against "paper"
        else if (winner==0) {alert("Draw with"+outcomeType(a));}                               //*REMOVED EXTRA SEMICOLON*draw
        else {alert("Player B wins with"+outcomeType(b)+"against"+outcomeType(a));}            //*REMOVED EXTRA SEMICOLON*player B winning alet

    }


    game();
    </script>

我对代码进行了抨击,这就是我最终的结果:

function randomNumber(x,y) {
  return Math.floor((Math.abs(y - x) + 1) * Math.random()) + Math.min(x, y);
}

function game() {
  var outcomeType = ["rock", "paper", "scissors"];
  var a = randomNumber(0,2);
  var b = randomNumber(0,2);
  switch ((3 + a - b) % 3) {
    case 1: alert("Player A wins with " + outcomeType[a] + " against " + outcomeType[b]); break;
    case 2: alert("Player B wins with " + outcomeType[b] + " against " + outcomeType[a]); break;
    default: alert("Draw with " + outcomeType[a]); break;
  }
}

我提供了代码来展示解决游戏中某些事情的一些不同方法。 有些不太明显或不太可读,所以它绝不是理想代码的一个例子。 :)

重要说明:请注意,随机函数使用Math.floorabs(y - x) + 1来获得随机分布。 如果使用Math.random ,获得最低或最高数字的机会将是它应该的一半。

    <form name="frmRPC">

        Rock: <input type="radio" name="RPC" value="Rock" />
        </br>
        Paper: <input type="radio" name="RPC" value="Paper" />
        </br>
        Scissors: <input type="radio" name="RPC" value="Scissors" />
        </br>
        <input onclick="Play();" type="button" value="Play" name="btnPlay" />

    </form>

    <script>

    function Play()
    {
        var ComputerChoice = {"Rock":1 , "Paper":2 , "Scissors":3 };

        Object.freeze(ComputerChoice);

        var userChoice = "";
        var computerChoice = Math.floor((Math.random() * 3) + 1);

        for (i = 0; i < document.frmRPC.RPC.length; i++)
        {
            if (document.frmRPC.RPC[i].checked)
            {
                var userChoice = document.frmRPC.RPC[i].value;
                break;
            }
        }

        if (userChoice === "")
        {
            alert("Please select a choice first");
        }
        else
        {
           // alert(userChoice);
            switch(userChoice)
            {
                case 'Rock':
                    switch(computerChoice)
                    {
                        case ComputerChoice.Rock:
                            alert("You Chose: Rock - Computer chose: Rock - Tie");
                        break;

                        case ComputerChoice.Paper:
                            alert("You Chose: Rock - Computer chose: Paper - You Loose");
                        break;

                        case ComputerChoice.Scissors:
                            alert("You Chose: Rock - Computer chose: Scissors - You Win");
                        break;
                    }
                break;

                case 'Paper':

                    switch(computerChoice)
                    {
                        case ComputerChoice.Rock:
                            alert("You Chose: Paper - Computer chose: Rock - You Win");
                        break;

                        case ComputerChoice.Paper:
                            alert("You Chose: Paper - Computer chose: Paper - Tie");
                        break;

                        case ComputerChoice.Scissors:
                            alert("You Chose: Paper - Computer chose: Scissors - You Loose");
                        break;
                    }

                break;

                case 'Scissors':

                    switch(computerChoice)
                    {
                        case ComputerChoice.Rock:
                            alert("You Chose: Scissors - Computer chose: Rock - You Loose");
                        break;

                        case ComputerChoice.Paper:
                            alert("You Chose: Scissors - Computer chose: Paper - You Win");
                        break;

                        case ComputerChoice.Scissors:
                            alert("You Chose: Scissors - Computer chose: Scissors - Tie");
                        break;
                    }

                break;
            }
        }
    }

    </script>

您可以使用Math.random() 这样,计算机生成1到0之间的随机数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM