Summary
The issue with the Rock, Paper, Scissors game is due to the incorrect implementation of the getHumanChoice and getComputerChoice functions. The getHumanChoice function is using prompt which is not suitable for a console game, and the getComputerChoice function is generating a new random choice every time it is called. The main problem is that the playRound function is calling getHumanChoice and getComputerChoice multiple times, resulting in different choices being made each time.
Root Cause
The root cause of the issue is:
- The
getHumanChoicefunction is usingpromptwhich is not suitable for a console game. - The
getComputerChoicefunction is generating a new random choice every time it is called. - The
playRoundfunction is callinggetHumanChoiceandgetComputerChoicemultiple times.
Why This Happens in Real Systems
This issue can happen in real systems when:
- Functions are not properly tested and their behavior is not well understood.
- Global variables are used and their values are changed unexpectedly.
- Functions have side effects and their behavior is not predictable.
Real-World Impact
The impact of this issue is:
- Incorrect game results due to the incorrect implementation of the game logic.
- Poor user experience due to the unexpected behavior of the game.
- Difficulty in debugging due to the complex and unpredictable behavior of the game.
Example or Code
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
let humanScore = 0;
let computerScore = 0;
function getComputerChoice() {
const options = ["Rock", "Paper", "Scissors"];
const randomChoice = Math.floor(Math.random() * 3);
return options[randomChoice];
}
function getHumanChoice() {
return new Promise(resolve => {
readline.question("What is your choice? ", resolve);
});
}
async function playRound() {
const humanChoice = await getHumanChoice();
const computerChoice = getComputerChoice();
console.log(`Human choice: ${humanChoice}`);
console.log(`Computer choice: ${computerChoice}`);
if (humanChoice === computerChoice) {
console.log("It's a draw");
} else if (humanChoice === "Rock" && computerChoice === "Scissors") {
humanScore++;
console.log("You win! Rock crushes Scissors");
} else if (humanChoice === "Scissors" && computerChoice === "Rock") {
computerScore++;
console.log("You lose! Rock crushes Scissors");
} else if (humanChoice === "Paper" && computerChoice === "Rock") {
humanScore++;
console.log("You win! Paper wraps Rock");
} else if (humanChoice === "Rock" && computerChoice === "Paper") {
computerScore++;
console.log("You lose! Paper wraps Rock");
} else if (humanChoice === "Paper" && computerChoice === "Scissors") {
computerScore++;
console.log("You lose! Scissors cuts Paper");
} else if (humanChoice === "Scissors" && computerChoice === "Paper") {
humanScore++;
console.log("You win! Scissors cuts Paper");
}
}
playRound();
How Senior Engineers Fix It
Senior engineers fix this issue by:
- Properly testing the functions and their behavior.
- Using local variables instead of global variables.
- Avoiding side effects in functions and making their behavior predictable.
- Using async/await to handle user input and game logic.
Why Juniors Miss It
Juniors miss this issue because:
- Lack of experience with game development and console games.
- Insufficient testing of the functions and their behavior.
- Poor understanding of the game logic and the requirements of the game.
- Inadequate use of debugging tools to identify and fix the issue.