CSS - This document details the development and implementation of a digital Rock-Paper-Scissors
CSS - This document details the development and implementation of a digital Rock-Paper-Scissors
PAPER
SCISSORS
INDEX
Sr No Topic Page No.
1. Acknowledgement 1
2. Introduction 2
5. Game Mechanics 5
6. Concepts Used 6
7. HTML Code 7
8. JavaScript Code 9
9. Output 11
10. Conclusion 13
11. References 14
ACKNOWLEDGEMENT
1
INTRODUCTION
2
GAME RULES AND LOGIC
Game Rules:
1. The player and the computer both choose between rock, paper, and
scissors.
2. Rock beats scissors, scissors beat paper, and paper beats rock.
3. If both the player and the computer choose the same option, the round is a
draw.
4. The first player to score 5 points wins the game.
Game Logic:
1. The game randomly generates the computer’s choice using JavaScript.
2. The user’s choice is taken from their interaction with the buttons (rock,
paper, or scissors).
3. Based on the comparison of the choices, the game determines the winner
of each round and updates the score.
4. Once a score of 5 is reached by either player, the game ends, and a
"Game Over" popup appears displaying the final score.
3
OBJECTIVE OF THE GAME
1. Win Conditions:
The game checks if either the user or the computer reaches a score of 5.
If the user wins a round, they gain a point. If the computer wins, it gains a
point. A draw results in no points being awarded.
The first to reach 5 points wins, and the game ends, resetting the score to
zero for the next playthrough.
2. Scoring System:
The score is displayed in real time on the scoreboard.
Each time the user wins, the userScore increments by 1 and is updated on
the scoreboard.
Similarly, the computerScore increments by 1 whenever the computer wins.
Scores are stored using cookies, which ensures that users can resume their
game if they refresh the page.
4
GAME MECHANICS
5
JAVASCRIPT CONCEPTS
1. DOM Manipulation
USED
The Document Object Model (DOM) is extensively used in the project to
dynamically update the user interface based on game events (e.g., score
updates, game results). Key elements like the scoreboard, result display, and
popups are all modified using JavaScript’s DOM manipulation methods such
as getElementById() and innerHTML.
2. Event Handling
Event listeners are used to handle the player's interactions with the game
buttons. When a player clicks a button, it triggers the playGame() function,
which processes the player’s choice and runs the game logic.
3. Conditional Statements
The game’s logic relies heavily on conditional statements (if, else if, else) to
compare the player's choice with the computer's, decide the result, and
update the scores accordingly.
4. Cookie Management
Cookies are used to store and retrieve the user's and computer's scores.
Functions like setCookie() and getCookie() handle cookie creation, retrieval,
and expiration.
6
HTML CODE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rock Paper Scissors</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-
awesome/6.0.0-beta3/css/all.min.css"
/>
<link
href="https://fonts.googleapis.com/css2?
family=Orbitron:wght@400;700&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Rock Paper Scissors</h1>
<div class="scoreboard">
User: <span id="user-score">0</span> | Computer:
<span id="computer-score">0</span>
</div>
<div class="choices">
<button class="button" onclick="playGame('rock')">
<i class="fas fa-hand-rock"></i>
</button>
<button class="button" onclick="playGame('paper')">
<i class="fas fa-hand-paper"></i>
</button>
<button class="button" onclick="playGame('scissors')">
<i class="fas fa-hand-scissors"></i>
</button>
</div>
<div id="result"></div>
7
HTML CODE
<!-- Custom Alert for Game Over -->
<div id="gameOverModal" class="modal hidden">
<div class="modal-content">
<h2>Game Over!</h2>
<p id="finalScore"></p>
<button onclick="closeGameOverModal()">Close</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
8
JAVASCRIPT CODE
let userScore = 0;
let computerScore = 0;
document.addEventListener("DOMContentLoaded", function () {
// Load scores from cookies when the page loads
userScore = getCookie("userScore") ? parseInt(getCookie("userScore")) : 0;
computerScore = getCookie("computerScore")
? parseInt(getCookie("computerScore"))
: 0;
document.getElementById("user-score").textContent = userScore;
document.getElementById("computer-score").textContent = computerScore;
document.getElementById("popup").classList.remove("hidden");
});
function playGame(playerChoice) {
const choices = ["rock", "paper", "scissors"];
const computerChoice = choices[Math.floor(Math.random() * 3)];
let result = "";
document.getElementById("user-score").textContent = userScore;
document.getElementById("computer-score").textContent = computerScore;
document.getElementById("result").innerHTML = `
<div class="icon"><i class="fas fa-hand-${playerChoice}"></i></div>
<div class="icon"><i class="fas fa-hand-${computerChoice}"></i></div>
<p>${result}</p>`;
checkGameEnd();
}
9
JAVASCRIPT CODE
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = `${name}=${value};expires=${date.toUTCString()};path=/`;
}
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(";").shift();
}
function checkGameEnd() {
if (userScore === 5 || computerScore === 5) {
document.getElementById(
"finalScore"
).textContent = `Final score - User: ${userScore}, Computer: ${computerScore}`;
document.getElementById("gameOverModal").classList.remove("hidden");
userScore = 0;
computerScore = 0;
document.getElementById("user-score").textContent = userScore;
document.getElementById("computer-score").textContent = computerScore;
setCookie("userScore", userScore, 365);
setCookie("computerScore", computerScore, 365);
}
}
function closePopup() {
document.getElementById("popup").classList.add("hidden");
}
function closeGameOverModal() {
document.getElementById("gameOverModal").classList.add("hidden");
}
// Show popup on page load
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("popup").classList.remove("hidden");
});
10
OUTPUT
11
OUTPUT
12
CONCLUSION
The Rock Paper Scissors game is a simple web project demonstrating key web
development concepts. It uses JavaScript for dynamic user interactions and
DOM manipulation to update scores in real time. The game also incorporates
cookies to store and persist scores across sessions, ensuring user data isn't
lost when the page refreshes.
The project provides a basic simulation of the classic game while introducing
fundamental concepts like randomness and conditionals. Future enhancements
could include adding difficulty levels, a multiplayer option, or improved
graphics to make the experience more engaging.
13
REFERENCES
canva.com
www.geeksforgeeks.com
chat.openai.com
14