I’m having some unusual bugs while trying to run my RPS game. Sometimes, it will just not give the score point to the winner, even though everything else seems to work as intended… Each function is linked as an “onclick” event on three different buttons. Here’s the code:
// JavaScript source code
let playerChoice = document.getElementById('PLAplay');
let cpuChoice = document.getElementById('CPUplay');
const playerPoint = document.getElementById('playerScorepoint')
const cpuPoint = document.getElementById('cpuScorepoint')
let plScore = 1;
let cpuScore = 1;
const chooseRock = () => {
playerChoice.src = "icons/PLArock.png";
playerChoice.width = 180;
playerChoice.height = 180;
return true;
};
const choosePaper = () => {
playerChoice.src = "icons/PLApaper.png";
playerChoice.width = 180;
playerChoice.height = 180;
return true;
};
const chooseScissors = () => {
playerChoice.src = "icons/PLAscissors.png";
playerChoice.width = 180;
playerChoice.height = 180;
return true;
};
const cpuBehaviour = (minValue = 1, maxValue = 12) => {
minValue = Math.ceil(minValue);
maxValue = Math.floor(maxValue);
let choiceResult = Math.floor(Math.random() * (maxValue - minValue + 1) + minValue);
if (choiceResult >= 1 && choiceResult <= 4) {
cpuChoice.src = "icons/CPUrock.png";
cpuChoice.width = 180;
cpuChoice.height = 180;
return "Rock";
}
else if (choiceResult >= 5 && choiceResult <= 8) {
cpuChoice.src = "icons/CPUpaper.png";
cpuChoice.width = 180;
cpuChoice.height = 180;
return "Paper";
}
else if (choiceResult >= 9 && choiceResult <= 12) {
cpuChoice.src = "icons/CPUScissors.png";
cpuChoice.width = 180;
cpuChoice.height = 180;
return "Scissors";
}
};
const checkWinRock = () => {
if (chooseRock() == true && cpuBehaviour() === "Paper") {
cpuPoint.innerHTML = cpuScore;
cpuScore++;
}
else if (chooseRock() == true && cpuBehaviour() === "Rock") {
cpuPoint.innerHTML = cpuPoint.innerHTML;
playerPoint.innerHTML = playerPoint.innerHTML;
}
else if (chooseRock() == true && cpuBehaviour() === "Scissors") {
playerPoint.innerHTML = plScore;
plScore++;
}
};
const checkWinPaper = () => {
if (choosePaper() == true && cpuBehaviour() === "Scissors") {
cpuPoint.innerHTML = cpuScore;
cpuScore++;
}
else if (choosePaper() == true && cpuBehaviour() === "Paper") {
cpuPoint.innerHTML = cpuPoint.innerHTML;
playerPoint.innerHTML = playerPoint.innerHTML;
}
else if (choosePaper() == true && cpuBehaviour() === "Rock") {
playerPoint.innerHTML = plScore;
plScore++;
}
};
const checkWinScissors = () => {
if (chooseScissors() == true && cpuBehaviour() === "Rock") {
cpuPoint.innerHTML = cpuScore;
cpuScore++;
}
else if (chooseScissors() == true && cpuBehaviour() == "Scissors") {
cpuPoint.innerHTML = cpuPoint.innerHTML;
playerPoint.innerHTML = playerPoint.innerHTML
}
else if (chooseScissors() == true && cpuBehaviour() == "Paper") {
playerPoint.innerHTML = plScore;
plScore++;
}
};