Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 30, 2023 02:38
Show Gist options
  • Save code-boxx/e2cce972b7820fa81f5e82dce394e124 to your computer and use it in GitHub Desktop.
Save code-boxx/e2cce972b7820fa81f5e82dce394e124 to your computer and use it in GitHub Desktop.
Javascript Rock Paper Scissors

JAVASCRIPT ROCK PAPER SCISSORS

https://code-boxx.com/javascript-rock-paper-scissors-game/

IMAGES

game-rock game-paper game-scissors

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (A) GAME WRAPPER + LAYOUT */
#rps-game * {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
#rps-game, #rps-play {
display: flex;
flex-wrap: wrap;
}
#rps-game { max-width: 600px; }
/* (B) MOVES */
#rps-you, #rps-com {
width: 50%;
padding: 15px;
text-align: center;
}
#rps-you-move, #rps-com-move {
max-width: 100%;
width: auto;
height: 200px;
margin-bottom: 10px;
}
#rps-you { background: #ffdada; }
#rps-com { background: #dcffdc; }
/* (C) SHARED PLAY & SCORE */
#rps-play, #rps-score {
width: 100%;
padding: 10px;
background: #444b9a;
}
/* (D) SELECT ROCK/PAPER/SCISSORS */
#rps-you-sel { flex-grow: 1; }
#rps-you-sel, #rps-you-go { padding: 10px; }
/* (E) SCORE */
#rps-score { color: #fff; }
<!DOCTYPE html>
<html>
<head>
<title>Javascript Rock Paper Scissors Demo</title>
<link href="rps.css" rel="stylesheet">
<script src="rps.js"></script>
</head>
<body>
<div id="rps-game">
<!-- (A) YOUR MOVE -->
<div id="rps-you">
<h2>YOUR MOVE</h2>
<img id="rps-you-move">
</div>
<!-- (B) COMPUTER'S MOVE -->
<div id="rps-com">
<h2>COMPUTER'S MOVE</h2>
<img id="rps-com-move">
</div>
<!-- (C) SELECT ROCK/PAPER/SCISSORS -->
<div id="rps-play">
<select id="rps-you-sel" disabled>
<option value="rock">Rock</option>
<option value="paper">Paper</option>
<option value="scissors">Scissors</option>
</select>
<input id="rps-you-go" type="button" value="Go!" disabled>
</div>
<!-- (D) SCOREBOARD -->
<div id="rps-score">
Win - <span id="rps-win">0</span> |
Lose - <span id="rps-lose">0</span> |
Draw - <span id="rps-draw">0</span>
</div>
</div>
</body>
</html>
// ROCK PAPER SCISSORS IMAGE SOURCE - WIKIPEDIA
// https://commons.wikimedia.org/wiki/File:Pierre_ciseaux_feuille_l%C3%A9zard_spock_aligned.svg
var rps = {
// (A) PRELOAD IMAGES
load : () => {
let loaded = 0;
for (let i of ["game-rock.png", "game-paper.png", "game-scissors.png"]) {
let img = new Image();
img.onload = () => {
loaded++;
if (loaded == 3) { rps.init(); }
};
img.src = i;
}
},
// (B) INIT GAME
eYou : null, // your move html image
eCom : null, // computer move html image
eSel : null, // html rock, scissors, paper selector
eGo : null, // html go button
eWin : null, wins : 0, // wins counter
eLose: null, loses : 0, // loses counter
eDraw : null, draws : 0, // draws counter
init : () => {
// (B1) GET HTML ELEMENTS
rps.eYou = document.getElementById("rps-you-move");
rps.eCom = document.getElementById("rps-com-move");
rps.eSel = document.getElementById("rps-you-sel");
rps.eGo = document.getElementById("rps-you-go");
rps.eWin = document.getElementById("rps-win");
rps.eLose = document.getElementById("rps-lose");
rps.eDraw = document.getElementById("rps-draw");
// (B2) UPDATE IMAGE WHEN CHANGING ROCK/PAPER/SCISSORS
rps.eSel.onchange = () => rps.eYou.src = `game-${rps.eSel.value}.png`;
rps.eYou.src = `game-${rps.eSel.value}.png`;
// (B3) WHEN USER HITS "GO!"
rps.eGo.onclick = rps.game;
// (B4) UNLOCK CONTROLS
rps.eSel.disabled = false;
rps.eGo.disabled = false;
},
// (C) GAME "ENGINE"
game : () => {
// (C1) RANDOM COMPUTER MOVE
var comMove = Math.random();
if (comMove <= 0.33) { comMove = "rock"; }
else if (comMove <= 0.67) { comMove = "paper"; }
else { comMove = "scissors"; }
// (C2) UPDATE COMPUTER MOVE GRAPHIC
rps.eCom.src = `game-${comMove}.png`;
// (C3) WIN, LOSE, OR DRAW?
var youMove = rps.eSel.value;
// (C3-1) DRAW
if (youMove == comMove) {
rps.draws++; rps.eDraw.innerHTML = rps.draws;
alert("DRAW");
} else {
// (C3-2) CHECK
let win = true;
switch (youMove) {
case "rock":
if (comMove=="paper") { win = false; }
break;
case "paper":
if (comMove=="scissors") { win = false; }
break;
case "scissors":
if (comMove=="rock") { win = false; }
break;
}
// (C3-3) UPDATE SCOREBOARD
if (win) {
rps.wins++; rps.eWin.innerHTML = rps.wins;
alert("YOU WIN");
} else {
rps.loses++; rps.eLose.innerHTML = rps.loses;
alert("YOU LOSE");
}
}
}
};
window.addEventListener("load", rps.load);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment