最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

A javascript rock, paper, scissors game! - Stack Overflow

programmeradmin4浏览0评论

Hey guys, I'm making a javascript rock, paper, scissors game! 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

Hey guys, I'm making a javascript rock, paper, scissors game! 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

Share Improve this question edited Nov 8, 2010 at 23:55 Anthony Forloney 91.8k14 gold badges118 silver badges116 bronze badges asked Nov 8, 2010 at 23:48 kidwonkidwon 4,5045 gold badges30 silver badges46 bronze badges 3
  • 7 Your 30 lines of code generate 12 errors on JSLint. Start with that. – Šime Vidas Commented Nov 8, 2010 at 23:51
  • 1 Where is your game function called from? – Anthony Forloney Commented Nov 8, 2010 at 23:52
  • stackoverflow.com/questions/22623331/… – Mke Spa Guy Commented Mar 25, 2014 at 17:57
Add a comment  | 

8 Answers 8

Reset to default 10

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?

I strongly suggest firefox + firebug, learn to use it and you will be able to fix this yourself in minutes.

It pointed out all 3 syntax errors in the code, which then ran, but always returned the same values. 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. The last line should (probably) be

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

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>

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.

Finally, randomNumber() needs a return in the last line. 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));}

That should get your alerts working so you can start debugging your logic.

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. If you use Math.random, the chance to get the lowest or the highest number will be half as high as it should be.

    <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(). That way the computer generates a random number between 1 and 0.

发布评论

评论列表(0)

  1. 暂无评论