Rock, Paper, Scissors in JavaScript

前端 未结 13 1697
野的像风
野的像风 2020-12-03 00:13

I\'m working on making my first game (Rock Paper Sissors) and I ran into an issue where when the userChoice is scissors and the com

13条回答
  •  情歌与酒
    2020-12-03 00:39

    var compare = function (choice1, choice2)
    {
        if (choice1 === choice2)
        {
            return "The result is a tie!";
        }
        else
        {
            if(choice1 === "rock")
            {
                if(choice2 === "paper")
                {
                   return "Paper beats rock. Computer Wins.";
                }
                else
                {
                    return "Rock beats scissors. You win.";
    
                }
            }
            else
            {
                if(choice1 === "paper")
                    {
                         if(choice2 === "rock")
                            {
                                 return "Paper beats rock. You Win.";
                            }
                else
                    {
                    return "Scissors beat paper. Computer Wins.";               }
    
                    }
        if(choice1 === "scissors")
                    {
                         if(choice2 === "rock")
                            {
                                 return "Rock beats scissors. Computer Wins.";
                            }
                else
                    {
                    return "Scissors beat paper. You Win.";               }
    
                    }
            }
        }
    
    
    
    };
    var r = function(user)
    {
        while(user < 0 | user >3)
        {user = prompt("Please don't act oversmart. Press '1' for rock, '2' for paper, and '3' for scissors.");
        }
    
        if(user === "1")
        user = "rock";
    
        else
        {
            if(user === "2")
            {user = "paper";}
            else
            {user = "scissors";}
        };
        console.log("You chose: " + user);
    
        computerChoice = Math.random()
        if(computerChoice <= 0.33)
        {
            computerChoice = "rock";
        }
        else
        {
            if(computerChoice > 0.33 && computerChoice <=0.66)
            {computerChoice = "paper";}
            else
            {computerChoice = "scissors";}
        }
    
        console.log("The computer chose: "+computerChoice)
        console.log(compare(user, computerChoice));
        if(user===computerChoice)
        {
            userChoice = user;
            return "1";}
    
    };
    
    
    var userChoice = prompt("Press '1' for rock, '2' for paper, and '3' for scissors")
    var computerChoice;
    
    var a = r(userChoice);
    if(a === "1")
    {//console.log("1");
    while(userChoice === computerChoice)
    {
        var a = prompt("Since there was a tie, please choose again. Press 1 for rock, 2 for paper and 3 for scissors.")
        var b = r(a);
        if(b !== "1")
        {break;}
    }
    }
    

提交回复
热议问题