Rock, Paper, Scissors, Lizard, Spock in JavaScript

前端 未结 6 1737
感情败类
感情败类 2020-11-28 15:36

I\'m kind of new to JavaScript. I just started learning it, and I decided to make a \'Rock, Paper, Scissors, Lizard, Spock\' game. Here is the code:

var use         


        
6条回答
  •  -上瘾入骨i
    2020-11-28 16:11

    I would do something like the following (please note that the syntax may be off slightly):

    var compare = function (choice1, choice2)
    {
        switch (choice1.tolower())
        {
            case "rock"
                RockPicked(choice2);
                break;
            case "scissors"
                ScissorsPicked(choice2);
                break;
            ....
            ....
            case default
                alert ("Selection was invalid")
                break;
        }
    
    }
    
    // if the user picked rock then we compare the computers choice and decide winner
    var RockPicked = function(choice2)
    {
        if (choice2 === "scissors") 
        {
            alert("Rock wins!");
        } 
        else if (choice2 === "paper") 
        {
            alert("Paper wins!");
        } 
        else if (choice2 === "lizard") 
        {
            alert("Rock wins!");
        } 
        else 
        {
            alert("Spock wins!");
        }
    }
    

提交回复
热议问题