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

时间:2010-11-08 23:48:27

标签: javascript

嘿伙计们, 我正在制作一个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

}

感谢任何帮助

8 个答案:

答案 0 :(得分:11)

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

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

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

答案 1 :(得分:5)

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

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

答案 2 :(得分:2)

你有没有在游戏开头()和游戏结束()结束时发出警报,以便你知道这样做是否正常?

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

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

答案 3 :(得分:2)

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

这应该让您的警报正常工作,以便您可以开始调试您的逻辑。

答案 4 :(得分:2)

标记错误..

固定:

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

答案 5 :(得分:2)

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

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,获得最低或最高数字的机会将是应有的一半。

答案 6 :(得分:0)

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

答案 7 :(得分:0)

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