Reputation: 1
I'm coding a rock paper scissors game for a chatbot. The issue I'm running into is that the switch (playerChoice)
is not recognizing the string defined in the playerChoice
variable.
When I run the switch, the default case always outputs. It would seem that it's not even picking up the variable in the switch()
Here is my code, with comments:
var user = "user"
var message = "/rps rock" //this can be paper or scissors, based on the user's input.
//Here we find out what the player chose from the command '/rps [choice]'
var playerSaid = message.split(" ")
var playerChoice = playerSaid[1].toString()
//This determines the bot's choice of 1-3
var botChoice = (Math.floor(Math.random() * 3) + 1)
//This switch *should* find the user's choice
//The if statements inside each case should get the bot's choice and tell the verdict appropriately
switch (playerChoice) {
case "rock":
if (botChoice = 1) {
var rpsVerdict = "It\'s a tie!"
} else if (botChoice = 2) {
var rpsVerdict = "Botticus wins!"
} else if (botChoice = 3) {
var rpsVerdict = user + " wins!"
};
break;
case "paper":
if (botChoice = 1) {
var rpsVerdict = "It\'s a tie!"
} else if (botChoice = 2) {
var rpsVerdict = "Botticus wins!"
} else if (botChoice = 3) {
var rpsVerdict = user + " wins!"
};
break;
case "scissors":
if (botChoice = 1) {
var rpsVerdict = "It\'s a tie!"
} else if (botChoice = 2) {
var rpsVerdict = "Botticus wins!"
} else if (botChoice = 3) {
var rpsVerdict = user + " wins!"
};
break;
default:
var rpsVerdict = "default"
break
}
//Here we output a simple sentence telling who won.
console.log('You chose ' + playerChoice + '. I chose ' + botChoice + '. ' + rpsVerdict)
(I realize the botChoice outputs a number, rather than a string)
Upvotes: 0
Views: 78
Reputation: 1075009
The problem is with the comparison in your if
statements:
if (botChoice = 1) {
// -----------^
=
is the assignment operator. To compare, you want ==
or ===
(doesn't matter which in this case).
With the assignment, here's what you're really doing:
botChoice = 1
if (1) {
...because with the assignment operator, it assigns the value to the target (botChoice
), and the result of the operation is the value that was assigned. So you always enter the first if
(because 1
is truthy), no matter what botChoice
actually was from Math.random
.
You don't need the switch
, though, you can determine the winner more simply and with less repetition using a lookup.
// Set up data. `choices` is for using Math.random to get
// a choice for the bot. `winLookup` is for looking up the
// player's choice and seeing what it beats.
var choices = ["rock", "paper", "scissors"];
var winLookup = {
rock: "scissors",
paper: "rock",
scissors: "paper"
};
// Bot chooses
var botChoice = choices[Math.floor(Math.random() * 3)];
// Outcome
var rpsVerdict;
if (playerChoice == botChoice) {
rpsVerdict = "It's a tie!";
} else if (winLookup[playerChoice] == botChoice) {
rpsVerdict = user + " wins!";
} else {
rpsVerdict = "Botticus wins!";
}
Live example:
document.getElementById("play").onclick = function() {
var choices = ["rock", "paper", "scissors"];
var winLookup = {
rock: "scissors",
paper: "rock",
scissors: "paper"
};
var user = "user";
var message = "/rps rock";
var playerSaid = message.split(" ");
var playerChoice = document.getElementById("playerChoice").value;
//var playerChoice = playerSaid[1];
//// No .toString() --------------^
// botChoice => rock, paper, or scissors
var botChoice = choices[Math.floor(Math.random() * 3)];
// outcome
var rpsVerdict;
if (playerChoice == botChoice) {
rpsVerdict = "It's a tie!";
} else if (winLookup[playerChoice] == botChoice) {
rpsVerdict = user + " wins!";
} else {
rpsVerdict = "Botticus wins!";
}
snippet.log(
"You chose " + playerChoice + ". I chose " + botChoice +
". " + rpsVerdict
);
};
<label>
Your choice:
<select size=3 id="playerChoice">
<option>rock</option>
<option>paper</option>
<option>scissors</option>
</select>
</label>
<input type="button" id="play" value="Play">
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Upvotes: 2