Skip to content

Commit

Permalink
Merge pull request #2376 from 1911aditi/master
Browse files Browse the repository at this point in the history
added an extension for color memory challenge.
  • Loading branch information
Sulagna-Dutta-Roy authored Jul 14, 2024
2 parents 69847c6 + 86782fa commit 207d563
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Color Memory Challenge/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Color Memory Challenge</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="game-container">
<h1>Color Memory Challenge</h1>
<div id="color-boxes"></div>
<button id="start-button">Start Game</button>
<div id="message"></div>
</div>
<script src="script.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions Color Memory Challenge/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"manifest_version": 3,
"name": "Color Memory Challenge",
"version": "1.0",
"description": "Test your memory with colors!",
"action": {
"default_popup": "index.html"
},
"permissions": []
}
82 changes: 82 additions & 0 deletions Color Memory Challenge/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const colorBoxes = document.getElementById('color-boxes');
const startButton = document.getElementById('start-button');
const message = document.getElementById('message');

let colors = [];
let sequence = [];
let userInput = [];
let round = 0;

// Start the game
startButton.addEventListener('click', startGame);

function startGame() {
round = 0;
sequence = [];
message.textContent = '';
generateSequence();
}

// Generate a random color sequence
function generateSequence() {
colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'cyan'];
sequence.push(colors[Math.floor(Math.random() * colors.length)]);
displaySequence();
}

// Display the sequence to the user
function displaySequence() {
colorBoxes.innerHTML = '';
sequence.forEach((color, index) => {
setTimeout(() => {
createColorBox(color);
if (index === sequence.length - 1) {
setTimeout(() => {
getUserInput();
}, 1000);
}
}, index * 1000);
});
}

// Create a color box
function createColorBox(color) {
const box = document.createElement('div');
box.className = 'box';
box.style.backgroundColor = color;
colorBoxes.appendChild(box);
}

// Get user input
function getUserInput() {
colorBoxes.innerHTML = '';
colors.forEach(color => {
const box = document.createElement('div');
box.className = 'box';
box.style.backgroundColor = color;
box.addEventListener('click', () => handleUserClick(color));
colorBoxes.appendChild(box);
});
}

// Handle user clicks
function handleUserClick(color) {
userInput.push(color);
checkUserInput();
}

// Check user input
function checkUserInput() {
const currentRound = userInput.length - 1;
if (userInput[currentRound] !== sequence[currentRound]) {
message.textContent = 'Wrong! Game Over.';
return;
}
if (userInput.length === sequence.length) {
userInput = [];
round++;
setTimeout(() => {
generateSequence();
}, 1000);
}
}
43 changes: 43 additions & 0 deletions Color Memory Challenge/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}

#game-container {
text-align: center;
background: #fff;
border: 1px solid #ddd;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}

#color-boxes {
display: grid;
grid-template-columns: repeat(4, 1fr);
margin-bottom: 20px;
}

.box {
width: 60px;
height: 60px;
margin: 5px;
border: 1px solid #ccc;
border-radius: 5px;
}

button {
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
}

#message {
margin-top: 20px;
font-size: 18px;
}

0 comments on commit 207d563

Please sign in to comment.