简体   繁体   English

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

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

Hey guys, I'm making a javascript rock, paper, scissors game! 嘿伙计们,我正在制作一个javascript摇滚,纸张,剪刀游戏! So the code is below and it doesn't work the alerts isn't showing and I really don't know where I mess things up. 所以代码在下面,它不起作用警报没有显示,我真的不知道我搞砸了。

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

}

Appreciate any help 感谢任何帮助

I think it's more important to give you the tools to answer this yourself, rather than post some working code. 我认为给你自己回答这个问题的工具更重要,而不是发布一些有效的代码。 How are you working with javascript at the moment? 你目前如何使用javascript?

I strongly suggest firefox + firebug, learn to use it and you will be able to fix this yourself in minutes. 我强烈建议使用firefox + firebug,学会使用它,你就可以在几分钟内自行修复。

It pointed out all 3 syntax errors in the code, which then ran, but always returned the same values. 它指出代码中的所有3个语法错误,然后运行,但总是返回相同的值。 Adding a breakpoint to the game function and step through it quickly showed the random function was broken, and didn't return. 在游戏功能中添加断点并快速逐步显示随机功能被破坏,并且没有返回。

Well one fairly serious problem is that your "randomNumber" routine fails to actually return anything. 一个相当严重的问题是你的“randomNumber”例程实际上没有return任何东西。 The last line should (probably) be 最后一行应该(可能)

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

Have you put an alert at the beginning of game() and the end of game() so that you know that that is working okay? 你有没有在游戏开始时()和游戏结束()结束时发出警报,这样你就知道那可行吗?

Also I think you have some extra semicolons in the game() blocks: I don't think there should be a semicolon inside the parameter list of each alert. 另外我认为你在game()块中有一些额外的分号:我认为每个警报的参数列表中都不应该有分号。

Finally, randomNumber() needs a return in the last line. 最后,randomNumber()需要在最后一行返回。 You're computing the final value but not doing anything with it! 你正在计算最终价值但却没有做任何事情!

You've got a lot of errors. 你有很多错误。 Starting off, you never call game so none of your functions ever execute. 从一开始,你从不打电话给游戏,所以你的任何一个功能都没有执行 Second your alerts calling functions inside them are invalid. 其次,调用其中的函数的警报无效。 Change outcomeType(a););} to outcomeType(a));} 将outcomeType(a););}更改为outcomeType(a));}

That should get your alerts working so you can start debugging your logic. 这应该让你的警报工作,这样你就可以开始调试你的逻辑了。

errors in your markup.. 标记中的错误..

fixed: 固定:

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

I had a stab at the code, and this is what I ended up with: 我对代码进行了抨击,这就是我最终的结果:

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;
  }
}

I provide the code to show some different ways of solving some things in the game. 我提供了代码来展示解决游戏中某些事情的一些不同方法。 Some are less obvious or less readable, so it's by no way an example of ideal code. 有些不太明显或不太可读,所以它绝不是理想代码的一个例子。 :) :)

Important: Note that the random function uses Math.floor and abs(y - x) + 1 to get the random distribution right. 重要说明:请注意,随机函数使用Math.floorabs(y - x) + 1来获得随机分布。 If you use Math.random , the chance to get the lowest or the highest number will be half as high as it should be. 如果使用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>

You can use Math.random() . 您可以使用Math.random() That way the computer generates a random number between 1 and 0. 这样,计算机生成1到0之间的随机数。

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

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