swg.css
· 1.8 KiB · CSS
Raw
body {
font-family: "Arial", sans-serif;
text-align: center;
padding: 50px;
background-color: #2c3e50;
color: #ecf0f1;
margin: 0;
}
#app {
max-width: 600px;
margin: 0 auto;
background-color: #34495e;
padding: 30px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
header {
margin-bottom: 30px;
}
h1 {
color: #3498db;
font-size: 32px;
margin-bottom: 20px;
}
p {
color: #ecf0f1;
font-size: 18px;
margin-bottom: 30px;
}
main {
margin-bottom: 30px;
}
#word-container {
margin-bottom: 30px;
font-size: 36px;
font-weight: bold;
color: #2ecc71;
}
#word-input {
font-size: 18px;
padding: 15px;
margin-bottom: 30px;
border: 2px solid #3498db;
border-radius: 8px;
color: #3498db;
background-color: #fff;
}
#game-info {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
margin-bottom: 30px;
}
#score,
#timer,
#remaining-clicks {
font-size: 18px;
margin-bottom: 20px;
color: #ecf0f1;
flex-basis: 100%;
}
#score-value,
#time-left,
#remaining-clicks-value {
font-weight: bold;
color: #e74c3c;
}
label {
display: block;
margin-bottom: 15px;
color: #3498db;
font-size: 18px;
}
#difficulty-select {
font-size: 18px;
padding: 15px;
margin-bottom: 30px;
border: 2px solid #3498db;
border-radius: 8px;
color: #3498db;
background-color: #fff;
}
button {
font-size: 18px;
padding: 15px;
cursor: pointer;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 8px;
margin-right: 15px;
transition: background-color 0.3s ease;
}
button:disabled {
background-color: #bdc3c7;
cursor: not-allowed;
}
button:hover {
background-color: #2980b9;
}
#another-word-button {
margin-bottom: 30px;
}
#remaining-clicks-value {
margin-left: 5px;
}
| 1 | body { |
| 2 | font-family: "Arial", sans-serif; |
| 3 | text-align: center; |
| 4 | padding: 50px; |
| 5 | background-color: #2c3e50; |
| 6 | color: #ecf0f1; |
| 7 | margin: 0; |
| 8 | } |
| 9 | |
| 10 | #app { |
| 11 | max-width: 600px; |
| 12 | margin: 0 auto; |
| 13 | background-color: #34495e; |
| 14 | padding: 30px; |
| 15 | border-radius: 10px; |
| 16 | box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); |
| 17 | } |
| 18 | |
| 19 | header { |
| 20 | margin-bottom: 30px; |
| 21 | } |
| 22 | |
| 23 | h1 { |
| 24 | color: #3498db; |
| 25 | font-size: 32px; |
| 26 | margin-bottom: 20px; |
| 27 | } |
| 28 | |
| 29 | p { |
| 30 | color: #ecf0f1; |
| 31 | font-size: 18px; |
| 32 | margin-bottom: 30px; |
| 33 | } |
| 34 | |
| 35 | main { |
| 36 | margin-bottom: 30px; |
| 37 | } |
| 38 | |
| 39 | #word-container { |
| 40 | margin-bottom: 30px; |
| 41 | font-size: 36px; |
| 42 | font-weight: bold; |
| 43 | color: #2ecc71; |
| 44 | } |
| 45 | |
| 46 | #word-input { |
| 47 | font-size: 18px; |
| 48 | padding: 15px; |
| 49 | margin-bottom: 30px; |
| 50 | border: 2px solid #3498db; |
| 51 | border-radius: 8px; |
| 52 | color: #3498db; |
| 53 | background-color: #fff; |
| 54 | } |
| 55 | |
| 56 | #game-info { |
| 57 | display: flex; |
| 58 | flex-wrap: wrap; |
| 59 | justify-content: space-between; |
| 60 | margin-bottom: 30px; |
| 61 | } |
| 62 | |
| 63 | #score, |
| 64 | #timer, |
| 65 | #remaining-clicks { |
| 66 | font-size: 18px; |
| 67 | margin-bottom: 20px; |
| 68 | color: #ecf0f1; |
| 69 | flex-basis: 100%; |
| 70 | } |
| 71 | |
| 72 | #score-value, |
| 73 | #time-left, |
| 74 | #remaining-clicks-value { |
| 75 | font-weight: bold; |
| 76 | color: #e74c3c; |
| 77 | } |
| 78 | |
| 79 | label { |
| 80 | display: block; |
| 81 | margin-bottom: 15px; |
| 82 | color: #3498db; |
| 83 | font-size: 18px; |
| 84 | } |
| 85 | |
| 86 | #difficulty-select { |
| 87 | font-size: 18px; |
| 88 | padding: 15px; |
| 89 | margin-bottom: 30px; |
| 90 | border: 2px solid #3498db; |
| 91 | border-radius: 8px; |
| 92 | color: #3498db; |
| 93 | background-color: #fff; |
| 94 | } |
| 95 | |
| 96 | button { |
| 97 | font-size: 18px; |
| 98 | padding: 15px; |
| 99 | cursor: pointer; |
| 100 | background-color: #3498db; |
| 101 | color: #fff; |
| 102 | border: none; |
| 103 | border-radius: 8px; |
| 104 | margin-right: 15px; |
| 105 | transition: background-color 0.3s ease; |
| 106 | } |
| 107 | |
| 108 | button:disabled { |
| 109 | background-color: #bdc3c7; |
| 110 | cursor: not-allowed; |
| 111 | } |
| 112 | |
| 113 | button:hover { |
| 114 | background-color: #2980b9; |
| 115 | } |
| 116 | |
| 117 | #another-word-button { |
| 118 | margin-bottom: 30px; |
| 119 | } |
| 120 | |
| 121 | #remaining-clicks-value { |
| 122 | margin-left: 5px; |
| 123 | } |
| 124 |
swg.html
· 1.6 KiB · HTML
Raw
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>QuickSort Word Scramble</title>
</head>
<body>
<div id="app">
<header>
<h1>Simple word game</h1>
<p>Rearrange the words to correct form , before time runs out</p>
</header>
<main>
<div id="word-container"></div>
<input
type="text"
id="word-input"
placeholder="Enter rearranged word"
/>
</main>
<!-- INFO -->
<div id="game-info">
<div id="score">Score: <span id="score-value">0</span></div>
<div id="timer">Time Left: <span id="time-left">60</span> seconds</div>
<div id="remaining-clicks">
Another word: <span id="remaining-clicks-value">2</span>
</div>
</div>
<!-- DIFICULTIES -->
<div>
<label for="difficulty-select">Select Difficulty:</label>
<select id="difficulty-select" onchange="changeDifficulty()">
<option value="easy">Easy</option>
<option value="medium">Medium</option>
<option value="hard">Hard</option>
</select>
</div>
<!-- BUTTONS -->
<button onclick="startGame()">Start New Game</button>
<button id="another-word-button" onclick="newWord('another-word-button')">
Another word
</button>
<button onclick="sortWord()">Submit Answer</button>
</div>
<script src="script.js"></script>
</body>
</html>
| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="UTF-8" /> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | <link rel="stylesheet" href="style.css" /> |
| 7 | <title>QuickSort Word Scramble</title> |
| 8 | </head> |
| 9 | <body> |
| 10 | <div id="app"> |
| 11 | <header> |
| 12 | <h1>Simple word game</h1> |
| 13 | <p>Rearrange the words to correct form , before time runs out</p> |
| 14 | </header> |
| 15 | <main> |
| 16 | <div id="word-container"></div> |
| 17 | <input |
| 18 | type="text" |
| 19 | id="word-input" |
| 20 | placeholder="Enter rearranged word" |
| 21 | /> |
| 22 | </main> |
| 23 | <!-- INFO --> |
| 24 | <div id="game-info"> |
| 25 | <div id="score">Score: <span id="score-value">0</span></div> |
| 26 | <div id="timer">Time Left: <span id="time-left">60</span> seconds</div> |
| 27 | <div id="remaining-clicks"> |
| 28 | Another word: <span id="remaining-clicks-value">2</span> |
| 29 | </div> |
| 30 | </div> |
| 31 | <!-- DIFICULTIES --> |
| 32 | <div> |
| 33 | <label for="difficulty-select">Select Difficulty:</label> |
| 34 | <select id="difficulty-select" onchange="changeDifficulty()"> |
| 35 | <option value="easy">Easy</option> |
| 36 | <option value="medium">Medium</option> |
| 37 | <option value="hard">Hard</option> |
| 38 | </select> |
| 39 | </div> |
| 40 | <!-- BUTTONS --> |
| 41 | <button onclick="startGame()">Start New Game</button> |
| 42 | <button id="another-word-button" onclick="newWord('another-word-button')"> |
| 43 | Another word |
| 44 | </button> |
| 45 | <button onclick="sortWord()">Submit Answer</button> |
| 46 | </div> |
| 47 | |
| 48 | <script src="script.js"></script> |
| 49 | </body> |
| 50 | </html> |
| 51 |
swg.js
· 7.8 KiB · JavaScript
Raw
let words = [];
let currentPair = {};
let score = 0;
let timer;
let isTimerRunning = false;
let initialRemainingClicks = 2; // Store the initial remaining clicks
let selectedDifficulty = "easy";
let timeLimit = 60;
/*---------------------------------------------- Submit Answer sortWord(); ----------------------------------------------------------*/
function sortWord() {
const userInput = document
.getElementById("word-input")
.value.trim()
.toLowerCase();
const isCorrect = userInput === currentPair.word;
if (isCorrect) {
score += 10;
} else {
score -= 10;
}
document.getElementById("score-value").textContent = score;
if (currentPair.remainingClicks > 0) {
setTimeout(function () {
newWord("submit-answer");
}, 1000);
}
}
/*---------------------------------------------- Generate Words ----------- ----------------------------------------------------------*/
function generateWordAndScrambled() {
let newPair;
do {
newPair = words[Math.floor(Math.random() * words.length)];
} while (newPair.word === currentPair.word);
return { word: newPair.word, scrambled: newPair.scrambled };
}
function setDefaultGameUI() {
// set up the UI for a new game
// add timer UI functionality here
document.getElementById("time-left").textContent = getTimeLimit(selectedDifficulty);
document.getElementById("score-value").textContent = score;
document.getElementById("remaining-clicks-value").textContent =
currentPair.remainingClicks;
document.getElementById("another-word-button").disabled = false;
displayWord(currentPair.scrambled);
}
/*---------------------------------------------- AnotherWord newWord(); ----------------------------------------------------------*/
function newWord(source) {
if (currentPair.remainingClicks > 0) {
const { word, scrambled } = generateWordAndScrambled();
currentPair = { ...currentPair, word, scrambled };
if (source === "another-word-button") {
currentPair.remainingClicks--;
}
updateUI();
}
}
function updateUI() {
document.getElementById("remaining-clicks-value").textContent =
currentPair.remainingClicks; // update remaining clicks that the user can see
if (currentPair.remainingClicks === 0) {
document.getElementById("another-word-button").disabled = true; // if out of clicks disable the button
}
document.getElementById("word-input").value = "";
displayWord(currentPair.scrambled); // update displayed word
}
/*---------------------------------------------- More Generated Words ----------------------------------------------------------------*/
function generateWords(difficulty) {
return correctedWords(difficulty).map(({ word }) => ({
word,
scrambled: shuffleWord(word),
}));
}
/*---------------------------------------------- The List of Words ------------------------------------------------------------------*/
function correctedWords(difficulty) {
const wordLists = {
easy: [
{ word: "cat" },
{ word: "dog" },
{ word: "bat" },
{ word: "sun" },
{ word: "sky" },
{ word: "cup" },
{ word: "pen" },
{ word: "hat" },
{ word: "run" },
{ word: "box" },
{ word: "red" },
{ word: "lip" },
{ word: "top" },
{ word: "hop" },
{ word: "fly" },
{ word: "cry" },
{ word: "joy" },
{ word: "zip" },
{ word: "mix" },
{ word: "fun" },
],
medium: [
{ word: "apple" },
{ word: "happy" },
{ word: "table" },
{ word: "house" },
{ word: "mouse" },
{ word: "plant" },
{ word: "water" },
{ word: "night" },
{ word: "music" },
{ word: "dance" },
{ word: "grape" },
{ word: "smile" },
{ word: "bread" },
{ word: "dream" },
{ word: "cloud" },
{ word: "light" },
{ word: "tiger" },
{ word: "robot" },
{ word: "piano" },
{ word: "flame" },
{ word: "guitar" },
{ word: "planet" },
{ word: "forest" },
],
hard: [
{ word: "algorithm" },
{ word: "programming" },
{ word: "developer" },
{ word: "javascript" },
],
};
const selectedWordList = wordLists[difficulty] || wordLists.easy;
// Now, we shuffle and return the words based on the selected difficulty
return selectedWordList.map(({ word }) => ({
word,
scrambled: shuffleWord(word),
}));
}
/*---------------------------------------- Event Listeners DIFFICULTIES-----------------------------------------*/
function changeDifficulty() {
selectedDifficulty = document.getElementById("difficulty-select").value;
startNewRound(selectedDifficulty);
}
document.getElementById("start-button").addEventListener("click", function () {
if (!isTimerRunning) {
const selectedDifficulty =
document.getElementById("difficulty-select").value;
startGame(selectedDifficulty);
isTimerRunning = true;
}
});
document
.getElementById("another-word-button")
.addEventListener("click", function () {
if (currentPair.remainingClicks > 0) {
const { word, scrambled } = generateWordAndScrambled();
currentPair = {
word,
scrambled,
remainingClicks: currentPair.remainingClicks - 1,
};
updateUI();
} else {
startNewRound();
}
});
document
.getElementById("submit-answer-button")
.addEventListener("click", function () {
sortWord();
});
/* ------------------------------------------ Shuffle and Display words --------------------------------------------------------------*/
function shuffleWord(word) {
return word
.split("")
.sort(() => Math.random() - 0.5)
.join("");
}
function displayWord(word) {
const wordContainer = document.getElementById("word-container");
wordContainer.innerHTML = "";
for (let letter of word) {
const letterSpan = document.createElement("span");
letterSpan.textContent = letter;
wordContainer.appendChild(letterSpan);
}
}
/*----------------------------------------- startGame() , timer , reset score , new round ---------------------------------------------*/
function startGame(difficulty) {
words = generateWords(difficulty);
const { word, scrambled } = generateWordAndScrambled();
currentPair = { word, scrambled, remainingClicks: initialRemainingClicks };
timeLimit = getTimeLimit(selectedDifficulty);
// Reset the timer
clearInterval(timer);
countdownTimer(timeLimit);
setDefaultGameUI();
}
function resetScore() {
score = 0;
document.getElementById("score-value").textContent = score;
}
function startNewRound(difficulty) {
currentPair = {};
words = generateWords(difficulty);
const { word, scrambled } = generateWordAndScrambled();
currentPair = {
word,
scrambled,
remainingClicks: getRemainingClicks(difficulty),
};
setDefaultGameUI();
}
/*--------------------------------------- DIFFICULTY SETTINGS ------------------------------------------------------------------------*/
function getTimeLimit(difficulty) {
switch (difficulty) {
case "easy":
return 60;
case "medium":
return 45;
case "hard":
return 30;
default:
return 60;
}
}
function getRemainingClicks(difficulty) {
switch (difficulty) {
case "easy":
case "medium":
return 2;
case "hard":
return 3;
default:
return 2;
}
}
/*--------------------------------------------------- TIMER -------------------------------------------------------------------------*/
function countdownTimer(timeLimit) {
timeleft = 60;
timeLeft = timeLimit;
updateTimerDisplay();
timer = setInterval(() => {
timeLeft--;
if (timeLeft < 0) {
timeLeft = 0;
}
updateTimerDisplay(timeLeft);
if (timeLeft === 0) {
clearInterval(timer);
isTimerRunning = false;
}
}, 1000);
}
function updateTimerDisplay(timeLeft) {
const timeDisplay = document.getElementById("time-left");
timeDisplay.textContent = timeLeft;
}
| 1 | let words = []; |
| 2 | let currentPair = {}; |
| 3 | let score = 0; |
| 4 | let timer; |
| 5 | let isTimerRunning = false; |
| 6 | let initialRemainingClicks = 2; // Store the initial remaining clicks |
| 7 | let selectedDifficulty = "easy"; |
| 8 | let timeLimit = 60; |
| 9 | |
| 10 | /*---------------------------------------------- Submit Answer sortWord(); ----------------------------------------------------------*/ |
| 11 | |
| 12 | function sortWord() { |
| 13 | const userInput = document |
| 14 | .getElementById("word-input") |
| 15 | .value.trim() |
| 16 | .toLowerCase(); |
| 17 | |
| 18 | const isCorrect = userInput === currentPair.word; |
| 19 | |
| 20 | if (isCorrect) { |
| 21 | score += 10; |
| 22 | } else { |
| 23 | score -= 10; |
| 24 | } |
| 25 | |
| 26 | document.getElementById("score-value").textContent = score; |
| 27 | |
| 28 | if (currentPair.remainingClicks > 0) { |
| 29 | setTimeout(function () { |
| 30 | newWord("submit-answer"); |
| 31 | }, 1000); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /*---------------------------------------------- Generate Words ----------- ----------------------------------------------------------*/ |
| 36 | |
| 37 | function generateWordAndScrambled() { |
| 38 | let newPair; |
| 39 | do { |
| 40 | newPair = words[Math.floor(Math.random() * words.length)]; |
| 41 | } while (newPair.word === currentPair.word); |
| 42 | |
| 43 | return { word: newPair.word, scrambled: newPair.scrambled }; |
| 44 | } |
| 45 | |
| 46 | function setDefaultGameUI() { |
| 47 | // set up the UI for a new game |
| 48 | // add timer UI functionality here |
| 49 | document.getElementById("time-left").textContent = getTimeLimit(selectedDifficulty); |
| 50 | document.getElementById("score-value").textContent = score; |
| 51 | document.getElementById("remaining-clicks-value").textContent = |
| 52 | currentPair.remainingClicks; |
| 53 | document.getElementById("another-word-button").disabled = false; |
| 54 | displayWord(currentPair.scrambled); |
| 55 | } |
| 56 | |
| 57 | /*---------------------------------------------- AnotherWord newWord(); ----------------------------------------------------------*/ |
| 58 | |
| 59 | function newWord(source) { |
| 60 | if (currentPair.remainingClicks > 0) { |
| 61 | const { word, scrambled } = generateWordAndScrambled(); |
| 62 | currentPair = { ...currentPair, word, scrambled }; |
| 63 | if (source === "another-word-button") { |
| 64 | currentPair.remainingClicks--; |
| 65 | } |
| 66 | updateUI(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function updateUI() { |
| 71 | document.getElementById("remaining-clicks-value").textContent = |
| 72 | currentPair.remainingClicks; // update remaining clicks that the user can see |
| 73 | if (currentPair.remainingClicks === 0) { |
| 74 | document.getElementById("another-word-button").disabled = true; // if out of clicks disable the button |
| 75 | } |
| 76 | document.getElementById("word-input").value = ""; |
| 77 | displayWord(currentPair.scrambled); // update displayed word |
| 78 | } |
| 79 | |
| 80 | /*---------------------------------------------- More Generated Words ----------------------------------------------------------------*/ |
| 81 | |
| 82 | function generateWords(difficulty) { |
| 83 | return correctedWords(difficulty).map(({ word }) => ({ |
| 84 | word, |
| 85 | scrambled: shuffleWord(word), |
| 86 | })); |
| 87 | } |
| 88 | |
| 89 | /*---------------------------------------------- The List of Words ------------------------------------------------------------------*/ |
| 90 | function correctedWords(difficulty) { |
| 91 | const wordLists = { |
| 92 | easy: [ |
| 93 | { word: "cat" }, |
| 94 | { word: "dog" }, |
| 95 | { word: "bat" }, |
| 96 | { word: "sun" }, |
| 97 | { word: "sky" }, |
| 98 | { word: "cup" }, |
| 99 | { word: "pen" }, |
| 100 | { word: "hat" }, |
| 101 | { word: "run" }, |
| 102 | { word: "box" }, |
| 103 | { word: "red" }, |
| 104 | { word: "lip" }, |
| 105 | { word: "top" }, |
| 106 | { word: "hop" }, |
| 107 | { word: "fly" }, |
| 108 | { word: "cry" }, |
| 109 | { word: "joy" }, |
| 110 | { word: "zip" }, |
| 111 | { word: "mix" }, |
| 112 | { word: "fun" }, |
| 113 | ], |
| 114 | medium: [ |
| 115 | { word: "apple" }, |
| 116 | { word: "happy" }, |
| 117 | { word: "table" }, |
| 118 | { word: "house" }, |
| 119 | { word: "mouse" }, |
| 120 | { word: "plant" }, |
| 121 | { word: "water" }, |
| 122 | { word: "night" }, |
| 123 | { word: "music" }, |
| 124 | { word: "dance" }, |
| 125 | { word: "grape" }, |
| 126 | { word: "smile" }, |
| 127 | { word: "bread" }, |
| 128 | { word: "dream" }, |
| 129 | { word: "cloud" }, |
| 130 | { word: "light" }, |
| 131 | { word: "tiger" }, |
| 132 | { word: "robot" }, |
| 133 | { word: "piano" }, |
| 134 | { word: "flame" }, |
| 135 | { word: "guitar" }, |
| 136 | { word: "planet" }, |
| 137 | { word: "forest" }, |
| 138 | ], |
| 139 | hard: [ |
| 140 | { word: "algorithm" }, |
| 141 | { word: "programming" }, |
| 142 | { word: "developer" }, |
| 143 | { word: "javascript" }, |
| 144 | ], |
| 145 | }; |
| 146 | const selectedWordList = wordLists[difficulty] || wordLists.easy; |
| 147 | |
| 148 | // Now, we shuffle and return the words based on the selected difficulty |
| 149 | return selectedWordList.map(({ word }) => ({ |
| 150 | word, |
| 151 | scrambled: shuffleWord(word), |
| 152 | })); |
| 153 | } |
| 154 | |
| 155 | /*---------------------------------------- Event Listeners DIFFICULTIES-----------------------------------------*/ |
| 156 | |
| 157 | function changeDifficulty() { |
| 158 | selectedDifficulty = document.getElementById("difficulty-select").value; |
| 159 | startNewRound(selectedDifficulty); |
| 160 | } |
| 161 | |
| 162 | document.getElementById("start-button").addEventListener("click", function () { |
| 163 | if (!isTimerRunning) { |
| 164 | const selectedDifficulty = |
| 165 | document.getElementById("difficulty-select").value; |
| 166 | startGame(selectedDifficulty); |
| 167 | isTimerRunning = true; |
| 168 | } |
| 169 | }); |
| 170 | |
| 171 | document |
| 172 | .getElementById("another-word-button") |
| 173 | .addEventListener("click", function () { |
| 174 | if (currentPair.remainingClicks > 0) { |
| 175 | const { word, scrambled } = generateWordAndScrambled(); |
| 176 | currentPair = { |
| 177 | word, |
| 178 | scrambled, |
| 179 | remainingClicks: currentPair.remainingClicks - 1, |
| 180 | }; |
| 181 | updateUI(); |
| 182 | } else { |
| 183 | startNewRound(); |
| 184 | } |
| 185 | }); |
| 186 | |
| 187 | document |
| 188 | .getElementById("submit-answer-button") |
| 189 | .addEventListener("click", function () { |
| 190 | sortWord(); |
| 191 | }); |
| 192 | |
| 193 | /* ------------------------------------------ Shuffle and Display words --------------------------------------------------------------*/ |
| 194 | |
| 195 | function shuffleWord(word) { |
| 196 | return word |
| 197 | .split("") |
| 198 | .sort(() => Math.random() - 0.5) |
| 199 | .join(""); |
| 200 | } |
| 201 | |
| 202 | function displayWord(word) { |
| 203 | const wordContainer = document.getElementById("word-container"); |
| 204 | wordContainer.innerHTML = ""; |
| 205 | for (let letter of word) { |
| 206 | const letterSpan = document.createElement("span"); |
| 207 | letterSpan.textContent = letter; |
| 208 | wordContainer.appendChild(letterSpan); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /*----------------------------------------- startGame() , timer , reset score , new round ---------------------------------------------*/ |
| 213 | |
| 214 | function startGame(difficulty) { |
| 215 | words = generateWords(difficulty); |
| 216 | const { word, scrambled } = generateWordAndScrambled(); |
| 217 | currentPair = { word, scrambled, remainingClicks: initialRemainingClicks }; |
| 218 | |
| 219 | timeLimit = getTimeLimit(selectedDifficulty); |
| 220 | |
| 221 | // Reset the timer |
| 222 | clearInterval(timer); |
| 223 | countdownTimer(timeLimit); |
| 224 | |
| 225 | setDefaultGameUI(); |
| 226 | } |
| 227 | |
| 228 | function resetScore() { |
| 229 | score = 0; |
| 230 | document.getElementById("score-value").textContent = score; |
| 231 | } |
| 232 | |
| 233 | function startNewRound(difficulty) { |
| 234 | currentPair = {}; |
| 235 | words = generateWords(difficulty); |
| 236 | const { word, scrambled } = generateWordAndScrambled(); |
| 237 | currentPair = { |
| 238 | word, |
| 239 | scrambled, |
| 240 | remainingClicks: getRemainingClicks(difficulty), |
| 241 | }; |
| 242 | setDefaultGameUI(); |
| 243 | } |
| 244 | |
| 245 | /*--------------------------------------- DIFFICULTY SETTINGS ------------------------------------------------------------------------*/ |
| 246 | |
| 247 | function getTimeLimit(difficulty) { |
| 248 | switch (difficulty) { |
| 249 | case "easy": |
| 250 | return 60; |
| 251 | case "medium": |
| 252 | return 45; |
| 253 | case "hard": |
| 254 | return 30; |
| 255 | default: |
| 256 | return 60; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | function getRemainingClicks(difficulty) { |
| 261 | switch (difficulty) { |
| 262 | case "easy": |
| 263 | case "medium": |
| 264 | return 2; |
| 265 | case "hard": |
| 266 | return 3; |
| 267 | default: |
| 268 | return 2; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /*--------------------------------------------------- TIMER -------------------------------------------------------------------------*/ |
| 273 | |
| 274 | function countdownTimer(timeLimit) { |
| 275 | timeleft = 60; |
| 276 | timeLeft = timeLimit; |
| 277 | updateTimerDisplay(); |
| 278 | |
| 279 | timer = setInterval(() => { |
| 280 | timeLeft--; |
| 281 | |
| 282 | if (timeLeft < 0) { |
| 283 | timeLeft = 0; |
| 284 | } |
| 285 | |
| 286 | updateTimerDisplay(timeLeft); |
| 287 | |
| 288 | if (timeLeft === 0) { |
| 289 | clearInterval(timer); |
| 290 | isTimerRunning = false; |
| 291 | } |
| 292 | }, 1000); |
| 293 | } |
| 294 | |
| 295 | function updateTimerDisplay(timeLeft) { |
| 296 | const timeDisplay = document.getElementById("time-left"); |
| 297 | timeDisplay.textContent = timeLeft; |
| 298 | } |
| 299 |