Great day to all!
I was wondering why if I already chose "rock" (in the const playerSelection) and computer chose "rock" it prints me out that the "Computer wins" or sometimes it prints out that I won the game instead of being a tie. Same goes with the other element in the code.
What would have happened that made the result of the game to also be randomized?
Here is my code:
function computerPicks() {
let compChoices = ["rock", "paper", "scissors"] // let computer pick [rock, paper, scissors]
let randomizeChoice = Math.floor(Math.random() * compChoices.length) 1 // randomize rock paper and scissors
if (randomizeChoice === 1) {
return "rock";
} if (randomizeChoice === 2) {
return "paper";
} if (randomizeChoice === 3) {
return "scissors";
}
}
console.log(computerPicks()); // view computer's choice
function playRound(playerSelection, computerSelection) {
if (playerSelection === "rock") {
if (computerSelection === "rock") {
return "It is a tie!!"
} else if (computerSelection === "paper") {
return "The computer wins!";
} else {
return "You win this game!";
}
}
else if (playerSelection === "paper") {
if (computerSelection === "paper") {
return "It is a tie!!"
} else if (computerSelection === "rock") {
return "The computer wins!";
} else {
return "You win this game!";
}
} else {
if (computerSelection === "rock") {
return "Computer wins!";
} else if (computerSelection === "paper") {
return "User wins!";
} else {
return "Draw!";
}
}
}
const playerSelection = "scissors"; // primary choice
const computerSelection = computerPicks(); // calls the randomized choices
console.log(playRound(playerSelection, computerSelection)); // print out the result
I think it is also randomizing because of how I called the randomizer or on how it is declared in the computerPicks() function.
CodePudding user response:
The question has been correctly answered, so this is just a suggestion of what changes you might make to your game that could simplify things.
Dealing with many levels of if/else
statements can be difficult to comprehend, maintain, and debug.
You could reduce a lot of the complexity by storing some of the engine logic in an object that defines which choices beat which choices (see poorly named whatBeatsWhat
).
// Explicit mapping of winning key (i.e. "rock") beating losing value (i.e. "scissors").
const whatBeatsWhat = {
rock: "scissors",
paper: "rock",
scissors: "paper",
}
// Derive computerChoices from keys of whatBeatsWhat.
const computerChoices = Object.keys(whatBeatsWhat)
function computerPicks() {
// Randomize rock paper and scissors.
let randomizeChoice = Math.floor(Math.random() * computerChoices.length)
// randomizeChoice will be one of [0,1,2] representing all possible indices of computerChoices.
return computerChoices[randomizeChoice]
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "It is a tie!!"
}
// Check if playerSelection in whatBeatsWhat maps to computerSelection
// and if so, player has beaten computer.
if (whatBeatsWhat[playerSelection] === computerSelection) {
return "You win this game!"
}
// Undoubtedly, whatBeatsWhat[computerSelection] === playerSelection
return "The computer wins!"
}
const playerSelection = "scissors"
const computerSelection = computerPicks()
const result = playRound(playerSelection, computerSelection)
console.log(`${result} (${playerSelection} vs ${computerSelection})`)
CodePudding user response:
Your program is not behaving as expected because you are using the '===' operator to compare strings. The '===' operator is a strict equality operator, meaning that it will only return true if both operands refer to the same object. In your case, '==' would be more appropriate since you are checking for basic equality.