Getting odd results


#1

I've changed the programme to "Rock, Paper, Scissors, Lizard, Spock", and it is mostly working, but I get an odd result when the user and computer make the same initial choice. The couple of times I've had the problem have started the same way:

  • user and computer both pick "Spock"
  • output is correct: "Computer: Spock, User: Spock, The result is a tie!"
  • on rematch user picks lizard, computer picks scissors
  • result says "Computer: scissors, User: lizard, lizard poisons Spock, Computer wins". - HUH?!

It seems to be getting the win condition from the second run, but the message from a combination of the first and second.

I have no idea what is causing this. Help?

var scBeatP = "scissors cuts paper", scBeatL = "scissors decapitates lizard"; //Round result messages
var pBeatR = "paper covers rock", pBeatSp = "paper disproves Spock";
var rBeatSc = "rock dulls scissors", rBeatL = "rock crushes lizard";
var lBeatP = "lizard eats paper", lBeatSp = "lizard poisons Spock";
var spBeatSc = "Spock smashes scissors", spBeatR = "Spock disintegrates rock";
var uTiedC = "The result is a tie!";
var uWon = "User wins", cWon = "Computer wins", rematch = "Play another round"; //Match result messages

var uScore = 2; //0 === Computer wins, 1 === User wins, 2 === Tied

//Get input from user
var uChoice = "";
var getUserInput = function(){
while(uChoice != "rock" && uChoice != "paper" && uChoice != "scissors" && uChoice != "lizard" && uChoice != "Spock"){
uChoice = prompt("Do you choose rock, paper, scissors, lizard, or Spock?");
}
};
getUserInput();

//Computer makes choice
var cChoice = Math.random();
if (cChoice < 0.2) {
cChoice = "rock";
} else if(cChoice < 0.4) {
cChoice = "paper";
} else if(cChoice < 0.6){
cChoice = "scissors";
} else if(cChoice < 0.8){
cChoice = "lizard";
} else {
cChoice = "Spock";
}

//var cChoice = "rock";

//Compare user vs computer choice
var compare = function(choice1, choice2){
while(uChoice === cChoice){
console.log("Computer: " + cChoice);
console.log("User: " + uChoice);
console.log(uTiedC);

    var tieBreak = "";
    while(tieBreak != "rock" && tieBreak != "paper" && tieBreak != "scissors" && tieBreak != "lizard" && tieBreak != "Spock"){
        tieBreak = prompt("Do you choose rock, paper, scissors, lizard, or Spock?");
    }
    uChoice = tieBreak;

    cChoice = Math.random();
    if (cChoice < 0.2) {
        cChoice = "rock";
    } else if(cChoice < 0.4) {
        cChoice = "paper";
    } else if(cChoice < 0.6){
        cChoice = "scissors";
    } else if(cChoice < 0.8){
        cChoice = "lizard";
    } else {
        cChoice = "Spock";
    }
    
    uScore = 2;
}

console.log("Computer: " + cChoice);
console.log("User: " + uChoice);

if(choice1 === "scissors"){
    if(choice2 === "paper"){
        console.log(scBeatP);
        return 1;
   } else if(choice2 === "rock"){
        console.log(rBeatSc);
        return 0;
    } else if(choice2 === "lizard"){
        console.log(scBeatL);
        return 1;
    } else {
        console.log(spBeatSc);
        return 0;
     }
} else if(choice1 === "paper"){
    if(choice2 === "scissors"){
        console.log(scBeatP);
        return 0;
    } else if(choice2 === "rock"){
        console.log(pBeatR);
        return 1;
    } else if(choice2 === "lizard"){
        console.log(lBeatP);
        return 0;
   } else {
        console.log(pBeatSp);
        return 1;
    }
} else if(choice1 === "rock"){
    if(choice2 === "scissors"){
        console.log(rBeatSc);
        return 1;
   } else if(choice2 === "paper"){
        console.log(pBeatR);
        return 0;
    } else if(choice2 === "lizard"){
        console.log(rBeatL);
        return 1;
    } else {
        console.log(spBeatR);
        return 0;
   }
}else if(choice1 === "lizard"){
    if(choice2 === "scissors"){
        console.log(scBeatL);
        return 0;
   } else if(choice2 === "paper"){
        console.log(lBeatP);
        return 1;
   } else if(choice2 === "rock"){
        console.log(rBeatL);
        return 0;
   } else {
        console.log(lBeatSp);
        return 1;
    }
} else {
    if(choice2 === "scissors"){
        console.log(spBeatSc);
        return 1;
    } else if(choice2 === "rock"){
        console.log(spBeatR);
        return 1;
   } else if(choice2 === "paper"){
        console.log(pBeatSp);
        return 0;
    } else {
        console.log(lBeatSp);
        return 0;
    }
}
console.log(rematch);

};

uScore = compare(uChoice, cChoice);

if(uScore === 2){
console.log(rematch);
} else if(uScore === 1){
console.log(uWon);
} else {
console.log(cWon);
}


#2

@fuggnuggins,

1 To extend the userChoice-prompting with a validity-check, you would
embed the prompting into a function-body...
So you would start by
var getUserChoice = function() { your prompting here };

2 If you are extending the choice-possibillity, you have to extend the
so-called truth-table.

Your new Decisions-Table: fill-in ?? wins...

              | ch-2  |  ch-2  | ch-2     | ch-2   |
              | rock  |  paper | scissors | rope   |
  ------------|-------|--------|----------|--------|
              |       | paper  |  rock    | ????   |
 choice1  rock|   X   | wins   |  wins    |  wins  | (1st else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              | paper |        |scissors  |  ? ?   |
 choice1 paper| wins  |    X   |  wins    |  wins  | (2nd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  rock |scissors|          |  ?  ?  | 
 ch-1 scissors|  wins | wins   |   X      |  wins  | (3rd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  ? ?  | ? ? ?  |  ? ? ?   |        | 
 ch-1 rope    |  wins | wins   |   wins   |    X   | (4th else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|

3 What you want is a loop-construct...
create the getUserChoice function using return-statement
create the getComputerChoice function using return-statement
creat the compare function using return-statement

As you are using return-statements you call the compare-function
var theResult = compare(userChoice,computerChoice);
and thus having the =result= of the game in the theResult variable

===========================================

var playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";

   while (playAgain) {

       //execute the pre-declared getUserChoice-function
       var userChoice = getUserChoice();
       console.log("User choice is " + userChoice);

        //execute the pre-declared getComputerChoice-function
        var computerChoice = getComputerChoice();
        console.log("Computer choice is " + computerChoice);

        //execute the compare-function and capture the-result
        theResult = compare(userChoice,computerChoice);
        console.log("The result is " + theResult);

    if (theResult === "It is a tie!") {
       playAgain = true;
    } else {
      playAgain = false;
    }

    //end of -while- loop is reached, and if VALUE of playAgain === true
    // the loop will start over
    } //closing the while-loop
 }; //closing the function-body of playTheGame

// call the function playTheGame
playTheGame();