简体   繁体   中英

Rock paper scissors tie case on JavaScript

I am currently learning from CodeAcademy JS course and they told me to try to implement a retry case if the choice of the user and the choice of the computer are the same. Firstly, allow me to apologize for my bad indentation, as I am still learning how to properly ident my code. Take a look at the if(choice1 === choice2) .Under it I have written my way of performing a retry case, but it failed.I would be very glad to recieve help on how should I create a retry case if there is a tie between the choices? Thanks in advance!

var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
    confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
    userChoice = prompt("Do you choose rock, paper or scissors?");
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
console.log("You Chose:  "+userChoice);
console.log("The Computer Chose:  "+computerChoice);
var compare = function (choice1,choice2){
    if (choice1 === choice2) {
        userChoice = prompt("Choose again");
        computerChoice = Math.random();
        if (computerChoice < 0.34) {
            computerChoice = "rock";
        } else if(computerChoice <= 0.67) {
            computerChoice = "paper";
        } else {
            computerChoice = "scissors";
        }
        compare(userChoice,computerChoice);
    }

    if (choice1 === "rock"){
        if  (choice2 === "paper"){
            return "Paper wins";
        }
        if (choice2 === "scissors") {
            return "Rock wins";
        }
    }

    if (choice1 === "paper"){
        if (choice2 === "scissors"){
            return "Scissors wins!";
        }
        if (choice2 === "rock"){
            return "Paper wins!";
        }
    }

    if (choice1 === "scissors"){
        if (choice2 === "rock"){
            return "Rock wins!";
        }
        if (choice2 === "paper"){
            return "Scissors wins!";
        }
    }
};
compare(userChoice,computerChoice);

Here you go:

function rockpaperscissors() {
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
    confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
    userChoice = prompt("Do you choose rock, paper or scissors?");
  }
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  }
  else if(computerChoice <= 0.67) {
    computerChoice = "paper";
  }
  else {
    computerChoice = "scissors";
  }
  console.log("You Chose:  "+userChoice);
  console.log("The Computer Chose:  "+computerChoice);
  var choice1 = userChoice;
  var choice2 = computerChoice;
  if (choice1 === choice2){
    confirm('Sorry, but there was a tie. You and the computer are equals. Let\'s try again anyways.');
    return rockpaperscissors();
  }

  if (choice1 === "rock"){
    if (choice2 === "paper"){
      return "Paper wins";
    }
    if (choice2 === "scissors") {
      return "Rock wins";
    }
  }
  if (choice1 === "paper"){
    if (choice2 === "scissors"){
      return "Scissors wins!";
    }
    if (choice2 === "rock"){
      return "Paper wins!";
    }
  }
  if (choice1 === "scissors"){
    if (choice2 === "rock"){
      return "Rock wins!";
    }
    if (choice2 === "paper"){
      return "Scissors wins!";
    }
  }
}
confirm(rockpaperscissors());

Now then, to explain what I did:

  • Everything now lives within the rockpaperscissors function. This way you can call it from within itself without duplicating code.
  • Reindented everything with two spaces. Seriously, indentation is important.
  • If there is a tie, it'll confirm you telling you that there's a tie, followed by restarting itself.
  • Once a not-tie has been reached, it will confirm you the result.

Additionally, because I felt like it, a very refined version that uses lots of complicated JS:

function rockpaperscissors() {
  var user = '';
  do {
    user = prompt((user!==''?'Invalid answer. ':'')+'Choose rock, paper, or scissors.');
  } while (['rock', 'paper', 'scissors'].indexOf(user) == -1);
  var computer = ['rock', 'paper', 'scissors'][Math.floor(Math.random()*3)];

  switch (user+'|'+computer) {
    case "rock|scissors":
    case "paper|rock":
    case "scissors|paper":
      return "User Wins!";
    case "rock|rock":
    case "paper|paper":
    case "scissors|scissors":
      return rockpaperscissors();
    default:
      return "Computer Wins!";
  }

}
confirm(rockpaperscissors());

I'm not too clear on how your program is running but what you could do is break up you code into different functions. For example, have a function just to handle moves, another one that handles game logic (the score, looking for fouls, etc...)

If you had a function that would return the computers move, you would just use your same logic to say if (choice === computerMove) However, I would not compare computerMove with whatever the user types in with the identity comparison because you never know if the user will misspell rock or something. You could have this 'game logic' function return maybe a value that would tell if there is a winner, if both players made the same move (foul), or which turn it is.

If you're really hurting for a code snippet, let me know and I'll create one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM