Rock Paper Scissors Game Javascript | Rock Paper Scissors Game Html Css







Rock Paper Scissors Game Javascript | Rock Paper Scissors Game Html Css 

Welcome🎉 to Code With Random blog. In this blog, we learn how we create Rock Paper Scissors Game. We use HTML, Css, and javascript for this  Rock Paper Scissors Game. I hope you enjoy our blog so let’s start with a basic HTML structure for Rock Paper Scissors Game.

HTML code

 <!DOCTYPE html>  
<html>
<head>
<meta charset="utf-8>" />
<title>Rock Papaer Scissors</title>
<link rel="stylesheet" href="./styles.css" type="text/css" />
</head>
<body>
<header><h1>Rock Paper Scissors</h1></header>
<div class="score-board">
<div id="user-label" class="badge">user</div>
<div id="computer-label" class="badge">comp</div>
<span id="user-score">0</span> : <span id="computer-score">0</span>
</div>
<div class="result"><p>Paper covers rock. You win!</p></div>
<div class="choices">
<div id="rock" class="choice">
<img
width="48"
height="48"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAKfSURBVGhD7ZjJy81RGIA/GaPMItPOglhJFqzIEDZIypBhoZQkfYXwB1BCFhZ2UqSQsqCUJHMyZWnKkFhQyDw8z/nu6XeTm+m+9/Pdfk89fb/3/W7nnPeec+8957SUlJSUlDQj/bB722PHoBt2antMLMG7+A0/42kci5n/rrjpeBU/4Rs8jJvRAvRj1fML9H8W+AU/4BEcgu3KfPyKDvId+s7nQesG7IpD8WIll61+/R3sgtILq2e2IeSlsxUdyEA8Vck9weoBzULzFj4PO6Ovv4Hm9+ODyvN7PIa+AeEMQDt9maKCyWj+VooKauVXo/nsa3TZ+WyReabCGIl29jBFBePQfK1CzqWoYA6a11UmYBjeQ3Mu31BqFTIKzV9LUcEkNP9jIbnAx1i9FDeh+Z0pCqRWIQ5mPU5NUYG/J5fRAVaTCzmfooKFaP5QigKpVcifMhiv4NoUFcxG2z+RokDqVUgtGlbIcGyKQvpgUxQyCDt8IVPwPtqRfyMIL2QivkU7eYpLMYLQQnqjg7eDA+gGL4rQQlrRxs+im75IQgu5hDY+LUWxhBbiIcltuGeMaMIK8fNhw69SFE9YIZ4LnA1txHk7dGk9Qhsfk6JYQgvxYsHG16UoltBCFqGN38ToC4LQQnriM7SDuSYCCS1E1qAdXMfIWcln+eMpCsBvrHxtEzkrK9A+9qUoiOUYPSs70D68mQzDfZa3g1Gz4m9WnvUJJiKxADvyYOVpsZ54EWHbtzH62zFxBu3wKPYwUQe8HvJy23ZnmmgEI/A52ukF/JdffDeizoT3vra3HRvKaMzr2X3YSdyCi3HBb7gSd6NL1DZ0G0afd35KX9yD+d38W90xzMB2pz8uw114EN2b/cq9uBHHY0M+2CUlJSUldaSl5Tv4zOmPUWh7YwAAAABJRU5ErkJggg=="
/>
</div>
<div id="paper" class="choice">
<img
width="48"
height="48"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAALqSURBVGhD7ZlL6A1RHMevt5BHHnllgZL3TikkQpHHwsZjQVjYyiMrZYEsiIV3NmSDhUd/VixsbAhFeYfyWFD8Q17x+c6Zc5s7Hf1dM2fm3JpPfZpzbt3bfO+c38w5Z2oVFa3JGByPnaNeCzIOb+Pv2Cc4DVuKHngfL+AoHILH8Q0OwOAZFB9n4nfsG/UMXfE1rsVtqJCHcCoGQSfcjB9QQ+glnsEXmOYG/kINs33Yht9wHpbORvyE63ASbsEf+Lcgj7F31DPsxUemWS4PUCef5DC6guxEDa0kE1FXcmDUKxENldmmWWc0LjLNDtHNQEF0LBWdxAzTrDMFd5tmA5twjmnWSQbpFVsKriC63S41zQbmoh6QSWyQq/gTVV/nsfCh5grSDV1DZTgmC13YIHdQt215F89hobiCzMIrptnAEVxtmnVskORv6Pu6MoUOM1eQrNhwrqvqDVeQZopd05lT8dESTJBmil2z4vXx0RJMkGaKXYyNj5ZggjRT7CroV/HREkyQrAQTpCr2mGCCVMUek3uQLqiTXIOLsT+mcQXJSq5BpuND1A9qra01eDtuRS1vLUEHmY9f8BiqcEVP1JVRmINowwQd5BJqM2Fw1GtEJ/0ZtXkggg6iwruJWhf00wcptLRVmF0YdBChPap7qJ2P5B3FouH3FYMPIkbiU7yM3fVBioWofanggwg9rN7iaXRtRC/DvPd0vQQRk1F3seVRzz/egogDqJrRQ9I3XoMMRW2Nboh6fvEaRGxH1Utyl90H3oPo6f4M90c9f3gPIhagdgTT+715MgEVxE6PvKEXNJp6D4t6+bMEdZdMTk69oGWppjDS9dTPykm8aJr+GYHP8Rr20Qc5oTfA2i7VFKgwVIx623QLNa6zoj9Hb7TORr2C0VRfrwE0gdQbqf+5OnpRuhK1gNMV9jFc/5lV+A4/4nU8gUc7ULWgF6LvUQu2HaiNi9LRP7kCtehynbjLPajvuPYDKioqcqNW+wMkWMAxXNmXsgAAAABJRU5ErkJggg=="
/>
</div>
<div id="scissors" class="choice">
<img
width="48"
height="48"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAATySURBVGhD7ZlprF9DGIevLWIJaoul9ogiog0iYg9iadQHS4JaW1uINqpI7QlfJITYI4hUIpEoidpCQkiIFkWJtWlQGjsVey3Pc+6dmnvunHvmnP8/+uX8kif3P3PmvGdmzjvvvHPuQKdOnTp16tSp0/+m1WEfuBRuHfo7FvqiVWEXOBk0fBGcCJtBr7Lje4N2n4Kf4J8S1h0ErbQhnAQPwldQNi5/w3NgR3K1GuwFM+EJWAZlm2/DLXAOPATWvwFZWgUmwBXwMiyH+AEfgIO6EW6AOfADeM22Z0NKdnwPmAGPw48Q27Xj74BudCxsDLG838H6jLWsqNLBcB8shfgBP4MzdgHsAClp+Br4C+zQEaAb7g7T4DH4HmK78i7cDsfDplCnL8H7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/wnu2LUo/SNzW2CDaxoqX8SqIdY/yIiJKQx9y6SNVIJoOmInbC0Nnmk40yPLqXaMfw+zr4CSeF0SzsC05A3+Sb8Iufhv2o5mJtMyDtPArlzSuFzzkT+i7dwWw0PMjOLAEPO6lZTeEbcReOz+baMUIdDh7MxO9n5dys7zoQPK2Fj9Jt8AuJOZERbFdYqTLT9ZzscTTMZB3+38QEsFOnTp06jaKBgX8BYNbZ+2P3L60AAAAASUVORK5CYII="
/>
</div>
<p id="action-message">Make your move.</p>
</div>
<script src="./app.js" type="text/javascript"></script>
</body>
</html>

There is all the HTML code for the  Rock Paper Scissors Game. Now, you can see output without CSS, then we write css & javascript for the Rock Paper Scissors Game.

output

Rock Paper Scissors Game Javascript | Rock Paper Scissors Game Html Css

CSS Code

 @import url("https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700");  
@import url('https://fonts.googleapis.com/css?family=Source+Code+Pro:300,500');
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
* {
margin: 0;
padding: 0;
}
body {
background: #24272e;
color: #fff;
font-family: "Roboto", sans-serif;
}
code {
font-family: 'Source Code Pro', monospace;
font-size: 12px;
}
a {
font-family: 'Source Code Pro', monospace;
color: #ffcc99;
}
a:hover {
color: #ffba76;
}
header {
background: #fff;
padding: 20px;
}
header h1 {
color: #444;
text-align: center;
font-weight: 100;
}
.score-board {
border: 3px solid #fff;
width: 300px;
margin: 40px auto;
font-size: 40px;
border-radius: 4px;
text-align: center;
padding: 15px 20px;
position: relative;
}
.badge {
background: #e2584d;
text-transform: uppercase;
font-size: 14px;
padding: 10px 6px;
}
#user-label {
position: absolute;
top: 50%;
left: -5%;
transform: translate(-10%, -50%);
}
#computer-label {
position: absolute;
top: 50%;
right: -5%;
transform: translate(10%, -50%);
}
.result {
font-weight: 300;
text-align: center;
font-size: 32px;
line-height: 40px;
}
.result {
line-height: 50px;
}
.choices {
margin: 0 auto;
text-align: center;
padding: 40px 0;
}
.choice {
display: inline-block;
border: 3px solid #fff;
border-radius: 50%;
padding: 10px;
margin: auto 10px;
transition: all 0.3s ease;
}
.choice img {
filter: invert(100%);
}
#action-message {
text-align: center;
font-weight: 500;
text-transform: uppercase;
letter-spacing: 2px;
margin: 40px 0;
}
.winningStyles {
border: 3px solid #4dcc7d;
background-color: #043507;
box-shadow: 0 0 20px #043507;
}
.losingStyles {
border: 3px solid #c52e2e;
background-color: #2e0303;
box-shadow: 0 0 20px #2e0303;
}
.drawStyles {
border: 3px solid #444;
background-color: #222;
box-shadow: 0 0 20px #222;
}
sup {
margin: 0;
padding: 0;
}

Here is our updated output CSS.

output

Rock Paper Scissors Game Javascript | Rock Paper Scissors Game Html Css

Javascript code 

 // cache the dom (storing for future use)  
// & reset everything to 0 value
let userScore = 0;
let computerScore = 0;
const userScore_span = document.getElementById('user-score');
const computerScore_span = document.getElementById('computer-score');
const scoreBoard_div = document.querySelector('.score-board');
const result_div = document.querySelector('.result');
const rock_div = document.getElementById('rock');
const paper_div = document.getElementById('paper');
const scissors_div = document.getElementById('scissors');
// set up the core function for the computer that will use math.random to loop through an array and return that value
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
const randomNumber = Math.floor(Math.random() * 3);
return choices[randomNumber];
}
// similar to convertcase but just takes lowercase and replaces with titlecase
function convertCase(anythingIwant) {
if (anythingIwant === 'paper') return 'Paper';
if (anythingIwant === 'scissors') return 'Scissors';
return 'Rock';
}
// Winning Condition - this handles what happens when the user clicks one of the choices where the value is them passed through as a parameter
function win(user, computer) {
userScore++;
// console.log('user score is ' + userScore + ' ' + user);
userScore_span.innerHTML = userScore;
const userName = ' (user)'.fontsize(3).sup();
const compName = ' (comp)'.fontsize(3).sup();
result_div.innerHTML = `<p>${convertCase(user)}${userName} beats ${convertCase(computer)}${compName}. You win!</p>`;
const roundStatus = document.getElementById(user);
roundStatus.classList.add('winningStyles');
setTimeout(() => roundStatus.classList.remove('winningStyles'), 300);
}
// Losing Condition - this handles what happens when the user clicks one of the choices where the value is them passed through as a parameter
function loses(user, computer) {
computerScore++;
// console.log('computer score is ' + computerScore + ' ' + computer);
computerScore_span.innerHTML = computerScore;
const userName = ' (user)'.fontsize(3).sup();
const compName = ' (comp)'.fontsize(3).sup();
result_div.innerHTML = `<p>${convertCase(computer)}${compName} beats ${convertCase(user)}${userName}. You lose!</p>`;
const roundStatus = document.getElementById(user);
roundStatus.classList.add('losingStyles');
setTimeout(() => roundStatus.classList.remove('losingStyles'), 300);
}
// Draw Condition - this handles what happens when the user clicks one of the choices where the value is them passed through as a parameter
function draw(user, computer) {
const userName = ' (user)'.fontsize(3).sup();
const compName = ' (comp)'.fontsize(3).sup();
result_div.innerHTML = `<p>It was a draw! You both chose ${convertCase(user)}</p>`;
// "It was a draw! You both chose " + user + " " + computer; // old js
const roundStatus = document.getElementById(user);
roundStatus.classList.add('drawStyles');
setTimeout(() => roundStatus.classList.remove('drawStyles'), 300);
}
// The core game functions that set up and determine the games actual logic aka paper beats rock etc
function game(userChoice) {
const computerChoice = getComputerChoice();
// console.log('Game function: user choice is = ' + userChoice);
// console.log('Game function: computer choice is = ' + computerChoice);
switch (userChoice + computerChoice) {
case 'paperrock':
case 'rockscissors':
case 'scissorspaper':
win(userChoice, computerChoice);
// console.log("user wins");
break;
case 'rockpaper':
case 'scissorsrock':
case 'paperscissors':
loses(userChoice, computerChoice);
// console.log("computer wins");
break;
case 'rockrock':
case 'scissorsscissors':
case 'paperpaper':
draw(userChoice, computerChoice);
// console.log("draw");
break;
}
}
// ES5 style of writing this function
// function main() {
// rock_div.addEventListener('click', function() {
// game('rock');
// });
// paper_div.addEventListener('click', function() {
// game('paper');
// });
// scissors_div.addEventListener('click', function() {
// game('scissors');
// });
// }
// ES6 style of writing this function
// This function creates and adds an eventlistener to the rock, paper scissors html element and the passes the value of that element to the game function
function main() {
rock_div.addEventListener('click', () => game('rock'));
paper_div.addEventListener('click', () => game('paper'));
scissors_div.addEventListener('click', () => game('scissors'));
}
main();

Final output

Rock Paper Scissors Game Javascript | Rock Paper Scissors Game Html Css

Now we have completed our javascript section,  Here is our updated output with javascriptHope you like the Rock Paper Scissors Game. you can see output video and project screenshots. See our other blogs and gain knowledge in front-end development. Thank you 🙏💕!

Share on:

Leave a Comment