2

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

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
kidwon
  • 4,448
  • 5
  • 28
  • 45

8 Answers8

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.

Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
  • 2
    +1 for providing a fishing rod instead of a fish, especially in this case. – Marcel Korpel Nov 09 '10 at 00:04
  • Well, your "strong suggestion" suggests that Firebug is an superior debugging tool (compared to other tools). However, Chrome's built-in Developer tools are also an excellent choice. – Šime Vidas Nov 09 '10 at 00:08
  • @Sime, indeed, chrome dev tools, opera dragonfly and to a lesser extent ie dev toolbar, are all appropriate. – Paul Creasey Nov 09 '10 at 00:13
4

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
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Actually, it should use `Math.floor` and `(y-x+1)`, otherwise the random distribution gets wrong. – Guffa Nov 09 '10 at 00:26
  • OK well whatever; the most important part to get rolling was the `return` :-) – Pointy Nov 09 '10 at 00:43
  • That's a real WTF - Worse Than Failure. Not working is bad, but thinking that it works just fine while it doesn't is worse. – Guffa Nov 09 '10 at 20:00
2

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!

winwaed
  • 7,645
  • 6
  • 36
  • 81
2

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.

superfro
  • 3,327
  • 1
  • 18
  • 14
2

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>
FatherStorm
  • 7,133
  • 1
  • 21
  • 27
  • While you're at it, you could add the missing semi-colons (6 occurances), and also replace regular comparison (==) with strict comparison (===). – Šime Vidas Nov 08 '10 at 23:59
  • @Marcel Korpel - It's up to the OP whether he learns or not. FatherStorm fixed some errors and there's nothing wrong with posting the fixes. – kirk.burleson Nov 09 '10 at 00:16
  • @kirk: All right, I overlooked the added comments, but they aren't that obvious (greyed out, partly hidden). – Marcel Korpel Nov 09 '10 at 00:27
2

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Great piece of code I love it! Just can't figure how do cases recognize the modulus operator results? – kidwon Nov 09 '10 at 12:42
  • @kidwon: Thanks. If you examine the possible outcomes of `a-b`, you see that the values repeats cyclicly with a cycle of 3 so that -2 and 1 gives the same result. The modulus simply overlaps the cycles. :) – Guffa Nov 09 '10 at 20:15
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>
Riek
  • 1
0

You can use Math.random(). That way the computer generates a random number between 1 and 0.

Andy Clifton
  • 4,926
  • 3
  • 35
  • 47