I am new to Javascript, and I made a simple Rock Paper scissor game with some basic UI. We can play the game for how much longer we want and can check the score of both computer and our. It's a simple game that I made with basic knowledge that I had.
let compScore = 0 , playerScore = 0;
document.querySelector("#start").onclick = () => {
document.querySelector("#startup").style.display = "none";
document.querySelector("#choose").style.display = "flex";
}
document.querySelector("#check-score").onclick = () => {
document.querySelector("#startup").style.display = "none";
document.querySelector("#score-check-menu").style.display = "flex";
document.querySelector("#comp-score").innerHTML = compScore;
document.querySelector("#player-score").innerHTML = playerScore;
}
document.querySelector("#main-menue").onclick = () => {
document.querySelector("#startup").style.display = "flex";
document.querySelector("#score-check-menu").style.display = "none";
}
const options = document.querySelectorAll(".opt");
options.forEach((option) => {
option.onclick = function () {
playerOption = this.value;
console.log(playerOption);
option.classList.add("active");
options.forEach((option) => {
option.disabled = true;
})
document.querySelector("#output").style.display = "flex";
document.querySelector("#pl-choosen").innerHTML = playerOption;
let compOption = compOpt();
document.querySelector("#comp-choosen").innerHTML = compOption;
let winner = roundWinner(playerOption, compOption)
document.querySelector("#winner").innerHTML = winner;
if (winner == "Player"){
playerScore += 1;
}
else if (winner == "Computer"){
compScore += 1;
}
}
})
document.querySelector("#restart").onclick = () => {
document.querySelector("#startup").style.display = "flex";
document.querySelector("#choose").style.display = "none";
document.querySelector("#output").style.display = "none";
options.forEach((option) => {
option.disabled = false;
option.classList.remove("active");
})
}
function compOpt() {
let compOpt
do{
if (Math.floor(Math.random()*3+1) == 1){
compOpt = "Rock"
}
else if (Math.floor(Math.random()*3+1) == 2){
compOpt = "Paper"
}
else if (Math.floor(Math.random()*3+1) == 3){
compOpt = "Scissor"
}
} while (!(compOpt == "Rock" || compOpt == "Paper" || compOpt == "Scissor"))
return compOpt;
}
function roundWinner(playerOption, compOption) {
if (playerOption == "Rock" && compOption == "Scissor"){
return "Player";
}
else if (playerOption == "Scissor" && compOption == "Paper"){
return "Player";
}
else if (playerOption == "Paper" && compOption == "Rock"){
return "Player";
}
else if (playerOption == compOption){
return "No One";
}
else {
return "Computer";
}
}
*{
padding: 0;
margin: 0;
}
body{
background: black;
color: white;
text-align: center;
}
h1{
font-size: 48px;
font-weight: bold;
}
h2{
font-size: 24px;
font-weight: bold;
}
.btn{
padding: 10px 30px;
background: black;
color: white;
border: 2px solid white;
width: max-content;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: 0.3s;
}
.btn.active{
background: white;
color: black;
}
.btn:hover{
background: white;
color: black;
}
#container{
height: 100vh;
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 32px;
}
#startup{
display: flex;
flex-direction: column;
gap: 32px;
justify-content: center;
align-items: center;
}
#score-check-menu{
display: none;
flex-direction: column;
gap: 32px;
}
#choose{
display: none;
flex-direction: column;
align-items: center;
gap: 32px;
}
.options{
display: flex;
align-items: center;
gap: 32px;
}
#output{
display: none;
flex-direction: column;
align-items: center;
gap: 32px;
}
#player-score, #pl-choosen{
color: rgb(255, 78, 78);
}
#comp-score, #comp-choosen{
color: aqua;
}
#winner{
color: rgb(0, 255, 0);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="container">
<h1>Rock, Paper, Scissor Game</h1>
<div id="startup">
<h2>You will play against the Computer</h2>
<h2>To start click the Start button and to view score click the Check Score button</h2>
<div class="options">
<button class="btn" id="start">Start</button>
<button class="btn" id="check-score">Check Score</button>
</div>
</div>
<div id="score-check-menu">
<h2>Computer Score: <span id="comp-score"></span></h2>
<h2>Player Score: <span id="player-score"></span></h2>
<button class="btn" id="main-menue">Back To Main Menue</button>
</div>
<div id="choose">
<h2>Choose the option you want to play</h2>
<div class="options">
<button class="btn opt" value="Rock">Rock</button>
<button class="btn opt" value="Paper">Paper</button>
<button class="btn opt" value="Scissor">Scissor</button>
</div>
</div>
<div id="output">
<h2>You choosed <span id="pl-choosen"></span> and Computer choosed <span id="comp-choosen"></span></h2>
<h2><span id="winner"></span> has won the match</h2>
<h2>Would you like to play again? If yes then press the following button</h2>
<button class="btn" id="restart">Play Again</button>
</div>
</div>
</body>
</html>