Repository navigation
Expand file tree
/
Copy pathwordscramble.js
More file actions
236 lines (209 loc) Β· 8.89 KB
/
Copy pathwordscramble.js
File metadata and controls
236 lines (209 loc) Β· 8.89 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Word Scramble β Fantasy themed word guessing game with multiplayer
const WORDS = [
{ word: 'dragon', category: 'Creature', hint: 'A fire-breathing beast of legend' },
{ word: 'wizard', category: 'Character', hint: 'Master of arcane arts' },
{ word: 'castle', category: 'Place', hint: 'A fortified royal stronghold' },
{ word: 'potion', category: 'Item', hint: 'A magical brew in a vial' },
{ word: 'dungeon', category: 'Place', hint: 'Underground prison beneath a castle' },
{ word: 'knight', category: 'Character', hint: 'Armored warrior on horseback' },
{ word: 'goblin', category: 'Creature', hint: 'Small green mischievous creature' },
{ word: 'treasure', category: 'Item', hint: 'Gold and gems hidden by a dragon' },
{ word: 'enchant', category: 'Magic', hint: 'To place a magical spell upon' },
{ word: 'shield', category: 'Item', hint: 'Defensive armor for the arm' },
{ word: 'sword', category: 'Item', hint: 'A sharp-bladed melee weapon' },
{ word: 'forest', category: 'Place', hint: 'Dense woodland full of mystery' },
{ word: 'archer', category: 'Character', hint: 'Fighter who uses a bow' },
{ word: 'goblet', category: 'Item', hint: 'A decorative drinking cup' },
{ word: 'mystic', category: 'Magic', hint: 'Of hidden or supernatural quality' },
{ word: 'portal', category: 'Magic', hint: 'A magical gateway to another realm' },
{ word: 'realm', category: 'Place', hint: 'A kingdom or domain' },
{ word: 'rogue', category: 'Character', hint: 'A cunning thief in the shadows' },
{ word: 'shadow', category: 'Magic', hint: 'Darkness cast by blocking light' },
{ word: 'temple', category: 'Place', hint: 'Sacred building for worship' },
{ word: 'tower', category: 'Place', hint: 'Tall structure in a castle' },
{ word: 'warlock', category: 'Character', hint: 'A sorcerer of dark magic' },
{ word: 'fairy', category: 'Creature', hint: 'Tiny winged magical being' },
{ word: 'gnome', category: 'Creature', hint: 'Small earth-dwelling creature' },
{ word: 'troll', category: 'Creature', hint: 'Large creature living under bridges' },
{ word: 'unicorn', category: 'Creature', hint: 'A magical horse with a horn' },
{ word: 'phoenix', category: 'Creature', hint: 'Mythical bird reborn from ashes' },
{ word: 'grail', category: 'Item', hint: 'A sacred legendary cup' },
{ word: 'rune', category: 'Magic', hint: 'An ancient magical symbol' },
{ word: 'totem', category: 'Magic', hint: 'A carved spiritual symbol' },
];
const WORDS_PER_ROUND = 10;
const WORD_TIME = 30;
// Multiplayer
const playerCount = parseInt(localStorage.getItem('miniGamePlayers') || '1', 10);
let currentPlayer = 0;
let playerScores = Array(playerCount).fill(0);
// Game state
let wordQueue = [];
let currentWordIdx = 0;
let currentEntry = null;
let scrambled = '';
let timeLeft = WORD_TIME;
let timerInterval = null;
let hintUsed = false;
let gameActive = false;
let bestScore = parseInt(localStorage.getItem('wordscrambleBest') || '0', 10);
function scrambleWord(word) {
const arr = word.split('');
let shuffled;
do {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
shuffled = arr.join('');
} while (shuffled === word);
return shuffled;
}
function initGame() {
wordQueue = shuffle([...WORDS]).slice(0, WORDS_PER_ROUND);
currentWordIdx = 0;
currentPlayer = 0;
playerScores = Array(playerCount).fill(0);
gameActive = true;
document.getElementById('ws-message').classList.add('hidden');
document.getElementById('ws-score').textContent = '0';
updatePlayerDisplay();
loadWord();
}
function shuffle(arr) {
const a = [...arr];
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function loadWord() {
if (currentWordIdx >= wordQueue.length) {
if (playerCount > 1 && currentPlayer + 1 < playerCount) {
// Next player's turn with same word set
currentPlayer++;
currentWordIdx = 0;
document.getElementById('ws-feedback').className = 'log-info';
document.getElementById('ws-feedback').textContent = `π P${currentPlayer}'s turn complete! Pass to P${currentPlayer + 1}β¦`;
updatePlayerDisplay();
setTimeout(loadWord, 2000);
return;
} else {
endGame();
return;
}
}
currentEntry = wordQueue[currentWordIdx];
scrambled = scrambleWord(currentEntry.word);
hintUsed = false;
timeLeft = WORD_TIME;
document.getElementById('ws-scrambled').textContent = scrambled.toUpperCase();
document.getElementById('ws-hint-text').textContent = 'β';
document.getElementById('ws-category').textContent = currentEntry.category;
document.getElementById('ws-word-num').textContent = `${currentWordIdx + 1}/${wordQueue.length}`;
document.getElementById('ws-input').value = '';
document.getElementById('ws-input').disabled = false;
document.getElementById('ws-feedback').textContent = '';
document.getElementById('ws-feedback').className = '';
document.getElementById('btn-ws-hint').disabled = false;
updateTimerBar();
startTimer();
document.getElementById('ws-input').focus();
}
function startTimer() {
clearInterval(timerInterval);
timerInterval = setInterval(() => {
timeLeft--;
updateTimerBar();
if (timeLeft <= 0) {
clearInterval(timerInterval);
onTimeout();
}
}, 1000);
}
function updateTimerBar() {
const fill = document.getElementById('ws-timer-fill');
const pct = (timeLeft / WORD_TIME) * 100;
fill.style.width = pct + '%';
fill.classList.toggle('warning', pct < 33);
}
function onTimeout() {
document.getElementById('ws-input').disabled = true;
document.getElementById('ws-feedback').textContent = `β° Time! The word was: ${currentEntry.word.toUpperCase()}`;
document.getElementById('ws-feedback').className = 'ws-wrong';
setTimeout(nextWord, 1800);
}
function submitAnswer() {
if (!gameActive) return;
const input = document.getElementById('ws-input').value.trim().toLowerCase();
if (!input) return;
clearInterval(timerInterval);
document.getElementById('ws-input').disabled = true;
if (input === currentEntry.word) {
const timeBonus = Math.ceil(timeLeft * 2);
const hintPenalty = hintUsed ? 5 : 0;
const points = Math.max(5, 10 + timeBonus - hintPenalty);
playerScores[currentPlayer] += points;
document.getElementById('ws-score').textContent = playerScores[currentPlayer];
document.getElementById('ws-feedback').textContent = `β¨ Correct! +${points} pts`;
document.getElementById('ws-feedback').className = 'ws-correct';
setTimeout(nextWord, 1200);
} else {
document.getElementById('ws-feedback').textContent = `β Wrong! Answer: ${currentEntry.word.toUpperCase()}`;
document.getElementById('ws-feedback').className = 'ws-wrong';
setTimeout(nextWord, 1800);
}
}
function nextWord() {
currentWordIdx++;
loadWord();
}
function showHint() {
hintUsed = true;
document.getElementById('ws-hint-text').textContent = currentEntry.hint;
document.getElementById('btn-ws-hint').disabled = true;
}
function updatePlayerDisplay() {
document.getElementById('ws-player').textContent = `P${currentPlayer + 1}`;
document.getElementById('ws-score').textContent = playerScores[currentPlayer];
const banner = document.getElementById('ws-player-banner');
if (playerCount > 1) {
banner.classList.remove('hidden');
const icons = ['π‘', 'π΅', 'π΄', 'π’'];
banner.textContent = `${icons[currentPlayer]} Player ${currentPlayer + 1}'s Turn`;
} else {
banner.classList.add('hidden');
}
}
function endGame() {
gameActive = false;
clearInterval(timerInterval);
document.getElementById('ws-input').disabled = true;
document.getElementById('ws-scrambled').textContent = 'π';
const msg = document.getElementById('ws-message');
msg.classList.remove('hidden');
if (playerCount === 1) {
if (playerScores[0] > bestScore) {
bestScore = playerScores[0];
localStorage.setItem('wordscrambleBest', bestScore);
}
msg.textContent = `π Quest Complete! Final Score: ${playerScores[0]} pts`;
} else {
let maxScore = -1, winner = -1;
playerScores.forEach((s, i) => { if (s > maxScore) { maxScore = s; winner = i; } });
let text = `π Quest Complete!\n`;
text += playerScores.map((s, i) => `P${i + 1}: ${s} pts`).join(' ') + '\n';
text += `\nπ₯ Winner: P${winner + 1} with ${maxScore} pts!`;
msg.textContent = text;
msg.style.whiteSpace = 'pre-line';
}
}
// Event listeners
document.getElementById('btn-ws-submit').addEventListener('click', submitAnswer);
document.getElementById('ws-input').addEventListener('keydown', e => {
if (e.key === 'Enter') submitAnswer();
});
document.getElementById('btn-ws-hint').addEventListener('click', showHint);
document.getElementById('btn-ws-new').addEventListener('click', initGame);
initGame();