<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>
Hi, everyone! I just finished my take on this game. Below you can find the html code and the JavaScript code for the game. I would love if someone had a read through and found something that was badly written or redundant.Thanks!
<Below this line, add a link to the EXACT exercise that you are stuck at.>
<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
``` HTML GameJavascript named it script.js
var canvas = document.getElementById(ācanvasā);
var context = canvas.getContext(ā2dā);
var fps = 30;
var winText = " ";
var computerChoice = " ā;
var userChoice =ā ";
var computerText;
var playerText;
var rectRock = {
x: 20,
y: 10,
width: 60,
heigth: 20
};
var rectPaper = {
x: 20,
y: 50,
width: 60,
heigth: 20
};
var rectScissors = {
x: 20,
y: 90,
width: 60,
heigth: 20
};
canvas.onselectstart = function () { return false; }
function capitalizeFirstLetter(string) {
āuse strictā;
return string.charAt(0).toUpperCase() + string.slice(1);
}
function rockBoxText() {
āuse strictā;
context.fillStyle = āblueā;
context.font = ā30px Garamondā;
context.fillText(āRockā, 100, 30);
context.fillRect(20, 10, 60, 20);
}
function paperBoxText() {
āuse strictā;
context.fillStyle = āredā;
context.font = ā30px Garamondā;
context.fillText(āPaperā, 100, 70);
context.fillRect(20, 50, 60, 20);
}
function scissorsBoxText() {
āuse strictā;
context.fillStyle = āgreenā;
context.font = ā30px Garamondā;
context.fillText(āScissorsā, 100, 110);
context.fillRect(20, 90, 60, 20);
}
//Function to check whether a point is inside a rectangle
function isInside(pos, rect) {
āuse strictā;
return pos.x > rect.x && pos.x < rect.x + rect.width && pos.y < rect.y + rect.heigth && pos.y > rect.y;
}
//Function to get the mouse position
function getMousePos(canvas, event) {
āuse strictā;
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
//Function that checks if the player won or the computer.
function compare (choice1, choice2) {
āuse strictā;
if (choice1 === choice2) {
winText =āItās a tie!ā;
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
winText = "You win!"
} else {
winText = "You lose!";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
winText = "You win!";
} else {
winText = "You lose!";
}
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
winText = "You lose!";
} else {
winText = "You win!";
}
}
};
function generateComChoice () {
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = ārockā;
} else if(computerChoice <= 0.67) {
computerChoice = āpaperā;
} else {
computerChoice = āscissorsā;
}
};
function comChoice(){
if (canvas.addEventListener) {
canvas.addEventListener ("click", function (evt) {
var mousePos = getMousePos(canvas, evt);
if (isInside(mousePos, rectRock)) {
generateComChoice();
} else if (isInside(mousePos, rectPaper)) {
generateComChoice();
} else if (isInside(mousePos, rectScissors)) {
generateComChoice();
} else {
}
}, false);
}
};
function playerChoice (){
āuse strictā;
if (canvas.addEventListener) {
canvas.addEventListener(āclickā, function (evt) {
var mousePos = getMousePos(canvas, evt);
if (isInside(mousePos, rectRock)) {
userChoice = ārockā;
} else if (isInside(mousePos, rectPaper)) {
userChoice = āpaperā;
} else if (isInside(mousePos, rectScissors)) {
userChoice = āscissorsā;
} else {
}
}, false);
}
};
function checkIfWin (){
if (userChoice === ārockā || userChoice === āpaperā || userChoice === āscissorsā) {
compare(userChoice, computerChoice);
} else {}
};
function update (){
comChoice();
playerChoice();
checkIfWin();
};
function render (){
context.clearRect(0, 0, canvas.width, canvas.height);
rockBoxText();
paperBoxText();
scissorsBoxText();
context.font = ā15px Georgiaā;
if (computerChoice === ārockā) context.fillStyle = āblueā;
else if (computerChoice === āpaperā) context.fillStyle = āredā;
else if (computerChoice === āscissorsā) context.fillStyle = āgreenā;
else context.fillStyle =āwhiteā;
computerText = context.fillText("Computer Choice: " + capitalizeFirstLetter(computerChoice.toString()), 10, 195);
if (userChoice === ārockā) context.fillStyle = āblueā;
else if (userChoice === āpaperā) context.fillStyle = āredā;
else if (userChoice === āscissorsā) context.fillStyle = āgreenā;
else context.fillStyle =āwhiteā;
playerText = context.fillText("Your choice: " + capitalizeFirstLetter(userChoice.toString()), 10, 175);
context.fillStyle = āblackā;
context.fillText(winText, 10, 150);
};
function game (){
update();
render();
};
setInterval (function() {
game();
}, 1000/fps);
<do not remove the three backticks above>