Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
273 lines (253 loc) Β· 8.78 KB
/
Copy pathgame.js
File metadata and controls
273 lines (253 loc) Β· 8.78 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const COLORS = [
{ id: 'red', hex: '#e74c3c' },
{ id: 'orange', hex: '#e67e22' },
{ id: 'yellow', hex: '#f1c40f' },
{ id: 'green', hex: '#2ecc71' },
{ id: 'blue', hex: '#3498db' },
{ id: 'purple', hex: '#9b59b6' },
{ id: 'pink', hex: '#e91e8c' },
{ id: 'cyan', hex: '#1abc9c' },
];
const CODE_LENGTH = 4;
const MAX_TURNS = 10;
// Multiplayer
const playerCount = parseInt(localStorage.getItem('miniGamePlayers') || '1', 10);
let mpCurrentPlayer = 0;
let mpTurnsUsed = Array(playerCount).fill(0);
let mpSolved = Array(playerCount).fill(false);
let mpActive = Array(playerCount).fill(true);
let secretCode = [];
let currentGuess = Array(CODE_LENGTH).fill(null);
let selectedColor = null;
let turnNumber = 0;
let gameOver = false;
function updatePlayerBanner() {
const banner = document.getElementById('mm-player-banner');
if (!banner) return;
if (playerCount > 1) {
const icons = ['π‘', 'π΅', 'π΄', 'π’'];
banner.textContent = `${icons[mpCurrentPlayer]} Player ${mpCurrentPlayer + 1}'s Turn`;
banner.classList.remove('hidden');
} else {
banner.classList.add('hidden');
}
}
function initGame() {
secretCode = generateCode();
currentGuess = Array(CODE_LENGTH).fill(null);
selectedColor = null;
turnNumber = 0;
gameOver = false;
mpCurrentPlayer = 0;
mpTurnsUsed = Array(playerCount).fill(0);
mpSolved = Array(playerCount).fill(false);
mpActive = Array(playerCount).fill(true);
document.getElementById('board').innerHTML = '';
document.getElementById('message').classList.add('hidden');
document.getElementById('btn-guess').disabled = true;
buildPalette();
renderCurrentGuess();
updatePlayerBanner();
}
function generateCode() {
return Array.from({ length: CODE_LENGTH }, () =>
COLORS[Math.floor(Math.random() * COLORS.length)].id
);
}
function buildPalette() {
const palette = document.getElementById('color-palette');
palette.innerHTML = '';
COLORS.forEach(c => {
const btn = document.createElement('button');
btn.className = 'color-btn';
btn.style.background = c.hex;
btn.setAttribute('aria-label', c.id);
btn.addEventListener('click', () => selectColor(c.id));
palette.appendChild(btn);
});
}
function selectColor(colorId) {
if (gameOver) return;
selectedColor = colorId;
document.querySelectorAll('.color-btn').forEach(btn => {
btn.classList.toggle('active', btn.getAttribute('aria-label') === colorId);
});
const emptySlot = currentGuess.indexOf(null);
if (emptySlot !== -1) {
currentGuess[emptySlot] = colorId;
renderCurrentGuess();
}
}
function renderCurrentGuess() {
const slots = document.querySelectorAll('.slot');
slots.forEach((slot, i) => {
const colorId = currentGuess[i];
if (colorId) {
const color = COLORS.find(c => c.id === colorId);
slot.style.background = color.hex;
slot.style.borderColor = 'rgba(255,255,255,0.4)';
slot.style.borderStyle = 'solid';
} else {
slot.style.background = '';
slot.style.borderColor = '';
slot.style.borderStyle = '';
}
slot.onclick = () => {
if (!gameOver && currentGuess[i]) {
currentGuess[i] = null;
renderCurrentGuess();
}
};
});
document.getElementById('btn-guess').disabled = currentGuess.includes(null);
}
function submitGuess() {
if (gameOver || currentGuess.includes(null)) return;
const feedback = evaluateGuess(secretCode, currentGuess);
addBoardRow(turnNumber, [...currentGuess], feedback);
turnNumber++;
mpTurnsUsed[mpCurrentPlayer]++;
if (feedback.black === CODE_LENGTH) {
mpSolved[mpCurrentPlayer] = true;
if (playerCount === 1) {
endGame(true);
} else {
// This player solved it
const msg = document.getElementById('message');
msg.classList.remove('hidden');
msg.textContent = `π P${mpCurrentPlayer + 1} cracked it in ${turnNumber} tries!`;
msg.style.color = '#2ecc71';
// Check if all players have had a turn
mpActive[mpCurrentPlayer] = false;
const stillPlaying = mpActive.some(a => a);
if (!stillPlaying) {
endMultiplayerGame();
} else {
setTimeout(() => {
msg.classList.add('hidden');
switchToNextPlayer();
}, 1500);
}
}
} else if (turnNumber >= MAX_TURNS) {
mpActive[mpCurrentPlayer] = false;
if (playerCount === 1) {
endGame(false);
} else {
const stillPlaying = mpActive.some(a => a);
if (!stillPlaying) {
endMultiplayerGame();
} else {
const msg = document.getElementById('message');
msg.classList.remove('hidden');
msg.textContent = `π P${mpCurrentPlayer + 1} ran out of tries!`;
setTimeout(() => {
msg.classList.add('hidden');
switchToNextPlayer();
}, 1500);
}
}
} else {
currentGuess = Array(CODE_LENGTH).fill(null);
renderCurrentGuess();
document.getElementById('btn-guess').disabled = true;
}
}
function switchToNextPlayer() {
let next = (mpCurrentPlayer + 1) % playerCount;
let checked = 0;
while (!mpActive[next] && next !== mpCurrentPlayer && checked < playerCount) {
next = (next + 1) % playerCount;
checked++;
}
if (!mpActive[next]) { endMultiplayerGame(); return; }
mpCurrentPlayer = next;
turnNumber = mpTurnsUsed[mpCurrentPlayer];
currentGuess = Array(CODE_LENGTH).fill(null);
renderCurrentGuess();
document.getElementById('btn-guess').disabled = true;
updatePlayerBanner();
document.getElementById('board').innerHTML = '';
}
function endMultiplayerGame() {
gameOver = true;
const solvedPlayers = mpSolved.map((s, i) => s ? i : -1).filter(i => i >= 0);
const msg = document.getElementById('message');
msg.classList.remove('hidden');
if (solvedPlayers.length === 0) {
const code = secretCode.map(id => {
const c = COLORS.find(c => c.id === id);
return `<span style="display:inline-block;width:18px;height:18px;border-radius:50%;background:${c.hex};vertical-align:middle;margin:0 2px;"></span>`;
}).join('');
msg.innerHTML = `π Nobody cracked it! The code was: ${code}`;
msg.style.color = '#e74c3c';
} else {
// Winner = solved with fewest turns
let bestTurns = Infinity, winner = -1;
solvedPlayers.forEach(i => { if (mpTurnsUsed[i] < bestTurns) { bestTurns = mpTurnsUsed[i]; winner = i; } });
const scoreStr = mpTurnsUsed.map((t, i) => `P${i+1}:${mpSolved[i] ? t+' tries' : 'failed'}`).join(' | ');
msg.textContent = `π P${winner+1} wins! ${scoreStr}`;
msg.style.color = '#2ecc71';
}
}
function evaluateGuess(secret, guess) {
let black = 0, white = 0;
const sLeft = [], gLeft = [];
for (let i = 0; i < CODE_LENGTH; i++) {
if (secret[i] === guess[i]) { black++; }
else { sLeft.push(secret[i]); gLeft.push(guess[i]); }
}
gLeft.forEach(g => {
const idx = sLeft.indexOf(g);
if (idx !== -1) { white++; sLeft.splice(idx, 1); }
});
return { black, white };
}
function addBoardRow(turn, guess, feedback) {
const row = document.createElement('div');
row.className = 'board-row';
const guessEl = document.createElement('div');
guessEl.className = 'board-guess';
guess.forEach(colorId => {
const peg = document.createElement('div');
peg.className = 'board-peg';
const color = COLORS.find(c => c.id === colorId);
if (color) peg.style.background = color.hex;
guessEl.appendChild(peg);
});
const feedbackEl = document.createElement('div');
feedbackEl.className = 'board-feedback';
for (let i = 0; i < CODE_LENGTH; i++) {
const peg = document.createElement('div');
peg.className = 'feedback-peg';
if (i < feedback.black) peg.classList.add('black');
else if (i < feedback.black + feedback.white) peg.classList.add('white');
feedbackEl.appendChild(peg);
}
row.appendChild(guessEl);
row.appendChild(feedbackEl);
document.getElementById('board').appendChild(row);
}
function endGame(won) {
gameOver = true;
const msg = document.getElementById('message');
msg.classList.remove('hidden');
if (won) {
msg.textContent = `π You cracked it in ${turnNumber} tries!`;
msg.style.color = '#2ecc71';
} else {
const code = secretCode.map(id => COLORS.find(c => c.id === id).hex);
msg.innerHTML = `π Out of tries! The code was: ${secretCode.map(id => {
const c = COLORS.find(c => c.id === id);
return `<span style="display:inline-block;width:18px;height:18px;border-radius:50%;background:${c.hex};vertical-align:middle;margin:0 2px;"></span>`;
}).join('')}`;
msg.style.color = '#e74c3c';
}
}
document.getElementById('btn-guess').addEventListener('click', submitGuess);
document.getElementById('btn-clear').addEventListener('click', () => {
currentGuess = Array(CODE_LENGTH).fill(null);
renderCurrentGuess();
});
document.getElementById('btn-new-game').addEventListener('click', initGame);
initGame();