-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
142 lines (113 loc) · 4.24 KB
/
script.js
File metadata and controls
142 lines (113 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const generateRandomColor = () => {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
};
let targetColor;
let options = [];
let score = 0;
let totalGuesses = 0;
let wrongGuesses = 0;
let chancesLeft = 15;
document.addEventListener('DOMContentLoaded', setupGame);
function setupGame() {
if (chancesLeft <= 0) {
alert('Game Over! You have used all your chances.');
return;
}
targetColor = generateRandomColor();
const redComponent = getRedComponent(targetColor);
const greenComponent = getGreenComponent(targetColor);
const blueComponent = getBlueComponent(targetColor);
document.getElementById('color-to-guess').innerHTML = `
<div class="rgb-component">R: ${redComponent}</div>
<div class="rgb-component">G: ${greenComponent}</div>
<div class="rgb-component">B: ${blueComponent}</div>
`;
options = [];
options.push(targetColor);
for (let i = 1; i < 4; i++) {
let randomColor;
do {
randomColor = generateRandomColor();
} while (options.includes(randomColor));
options.push(randomColor);
}
shuffleArray(options);
displayOptions();
updateScoreboard();
}
function getRedComponent(color) {
return parseInt(color.slice(1, 3), 16);
}
function getGreenComponent(color) {
return parseInt(color.slice(3, 5), 16);
}
function getBlueComponent(color) {
return parseInt(color.slice(5, 7), 16);
}
function displayOptions() {
const optionsContainer = document.querySelector('.options');
optionsContainer.innerHTML = ''; // Clear previous options
options.forEach((optionColor, index) => {
const optionElement = document.createElement('div');
optionElement.classList.add('option');
optionElement.style.backgroundColor = optionColor;
optionElement.setAttribute('onclick', 'checkGuess(this)');
optionsContainer.appendChild(optionElement);
});
}
function displayScorecard() {
const accuracy = ((score / totalGuesses) * 100).toFixed(2);
const message = `Score: ${score}\nTotal Guesses: ${totalGuesses}\nWrong Guesses: ${wrongGuesses}\nAccuracy: ${accuracy}%\n\nWell Done!`;
alert(message);
}
function checkGuess(selectedOption) {
totalGuesses++;
const selectedColor = selectedOption.style.backgroundColor;
const targetColorRgb = `rgb(${getRedComponent(targetColor)}, ${getGreenComponent(targetColor)}, ${getBlueComponent(targetColor)})`;
// Remove any existing border from all options
document.querySelectorAll('.option').forEach(option => {
option.style.border = '';
});
if (selectedColor === targetColorRgb) {
// User selected the correct option
score++;
// Highlight the correct option with a green border
selectedOption.style.border = '2px solid green';
// Remove the green border after 2 seconds
setTimeout(() => {
selectedOption.style.border = ''; // Set border to an empty string to remove it
setupGame();
}, 0.5*1000);
} else {
// User selected the wrong option
wrongGuesses++;
// Highlight the wrong option with a red border
selectedOption.style.border = '2px solid red';
// Remove the red border after 2 seconds
setTimeout(() => {
selectedOption.style.border = ''; // Set border to an empty string to remove it
updateScoreboard();
}, 0.5*1000);
}
chancesLeft--;
if (chancesLeft <= 0) {
displayScorecard();
}
}
function updateScoreboard() {
document.getElementById('score').textContent = score;
document.getElementById('total-guesses').textContent = totalGuesses;
document.getElementById('wrong-guesses').textContent = wrongGuesses;
document.getElementById('chances-left').textContent = chancesLeft;
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}