-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
464 lines (415 loc) · 17.8 KB
/
index.html
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Семейная игра Jeopardy</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
}
.game-container {
max-width: 1200px;
margin: 0 auto;
}
.header {
text-align: center;
margin-bottom: 20px;
}
.score-board {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
padding: 10px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.player-score {
text-align: center;
padding: 10px;
flex: 1;
margin: 0 10px;
border-radius: 4px;
}
.player-score.active {
background-color: #e6f3ff;
box-shadow: 0 0 5px rgba(0,0,0,0.2);
}
.game-board {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 10px;
background-color: #000033;
padding: 10px;
border-radius: 8px;
}
.category {
background-color: #000099;
color: white;
padding: 10px;
text-align: center;
font-weight: bold;
min-height: 60px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 4px;
}
.question-cell {
background-color: #000099;
color: #ffd700;
padding: 20px;
text-align: center;
font-weight: bold;
font-size: 24px;
cursor: pointer;
border: 2px solid #000033;
border-radius: 4px;
transition: background-color 0.3s;
}
.question-cell:hover {
background-color: #0000cc;
}
.question-cell.used {
background-color: #000033;
cursor: not-allowed;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.7);
}
.modal-content {
position: relative;
background-color: #000099;
color: white;
margin: 15% auto;
padding: 20px;
width: 70%;
border-radius: 8px;
text-align: center;
}
.close-btn {
position: absolute;
right: 10px;
top: 5px;
color: white;
font-size: 24px;
cursor: pointer;
}
.answer-btn {
background-color: #ffd700;
color: #000033;
border: none;
padding: 10px 20px;
margin: 10px;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
}
.answer-btn:hover {
background-color: #ffed4a;
}
.controls {
margin-top: 20px;
text-align: center;
}
.score-btn {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
margin: 5px;
border-radius: 4px;
cursor: pointer;
}
.current-player {
text-align: center;
margin: 10px 0;
font-weight: bold;
color: #000099;
}
</style>
</head>
<body onload="loadGame()">
<div class="game-container">
<div class="header">
<h1>Семейная игра Jeopardy</h1>
<button id="settingsBtn">Настройки</button>
</div>
<div id="settingsModal" class="modal">
<div class="modal-content">
<span class="close-btn" onclick="closeSettings()">×</span>
<h2>Настройки игры</h2>
<label for="numPlayers">Количество игроков:</label>
<input type="number" id="numPlayers" min="2" max="5" value="4"><br><br>
<div id="playerNames"></div>
<label for="numSections">Количество разделов:</label>
<input type="number" id="numSections" min="6" max="6" value="6" readonly><br><br>
<div id="categoryInputs"></div>
<button onclick="saveSettings()">Сохранить</button>
<button onclick="resetToDefaults()">Сброс</button>
<button onclick="closeSettings()">Отмена</button>
</div>
</div>
<div class="score-board" id="scoreBoard">
<!-- Scoreboard will be dynamically populated here -->
</div>
<div class="current-player" id="currentPlayer">
<!-- Current player will be displayed here -->
</div>
<div class="game-board" id="gameBoard">
<!-- Заполняется через JavaScript -->
</div>
<div id="questionModal" class="modal">
<div class="modal-content">
<span class="close-btn">×</span>
<h2 id="questionText"></h2>
<button class="answer-btn" id="showAnswer">Показать ответ</button>
<p id="answerText" style="display: none;"></p>
<div class="controls">
<!-- Buttons for adding points to players will be added here dynamically -->
</div>
</div>
</div>
</div>
<script>
let playerNames = ['Мама', 'Маша', 'Папа', 'Миша'];
const allCategories = ['Общие знания', 'История России', 'Животный мир', 'Наука и технологии', 'Еда', 'Да или нет?', 'Литература', 'Кино', 'Музыка', 'Спорт', 'География', 'Искусство', 'Политика', 'Экономика', 'Религия', 'Философия', 'Психология', 'Социология', 'Языки', 'Культура', 'Технологии', 'Природа', 'Космос', 'Медицина', 'Математика', 'Физика', 'Химия', 'Биология', 'Информатика', 'Экология'];
const defaultCategories = ['Общие знания', 'История России', 'Животный мир', 'Наука и технологии', 'Еда', 'Да или нет?'];
const defaultSettings = {
categories: defaultCategories,
playerNames: playerNames
};
function resetToDefaults() {
gameData.categories = defaultCategories.slice(); // Create a copy of defaultCategories
// Reset player names
playerNames.length = 0; // Clear the existing array
defaultSettings.playerNames.forEach(name => playerNames.push(name));
localStorage.setItem('jeopardySettings', JSON.stringify(defaultSettings));
// Reset scores
for (let i = 1; i <= 4; i++) {
scores[`player${i}`] = 0;
}
updateScoreboard();
createGameBoard();
currentPlayer = 0; // Reset current player before updating the display
updateCurrentPlayer();
closeSettings();
}
let scores = {
player1: 0,
player2: 0,
player3: 0,
player4: 0
};
let gameData; // This will hold our game data from the JSON
let currentQuestionValue = 0;
let currentPlayer = 0;
async function loadGame() {
try {
const response = await fetch('gamedata.json');
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
gameData = await response.json();
let savedSettings = localStorage.getItem('jeopardySettings');
if (savedSettings) {
savedSettings = JSON.parse(savedSettings);
gameData.categories = savedSettings.categories;
if (savedSettings.playerNames && Array.isArray(savedSettings.playerNames)) {
playerNames = savedSettings.playerNames;
} else {
playerNames = defaultSettings.playerNames; // Fallback to defaults if player names are invalid
}
}
updateScoreboard();
createGameBoard();
updateCurrentPlayer();
} catch (error) {
console.error('Failed to load game data:', error);
// Handle the error appropriately, e.g., display a message to the user
}
}
function saveSettings() {
const numPlayers = Math.max(2, Math.min(5, parseInt(document.getElementById('numPlayers').value)));
playerNames = [];
for (let i = 1; i <= numPlayers; i++) {
const playerNameInput = document.getElementById(`playerName${i}`);
playerNames.push(playerNameInput ? playerNameInput.value : `Игрок ${i}`);
}
gameData.categories = [];
for (let i = 1; i <= 6; i++) {
const categorySelect = document.getElementById(`category${i}`);
gameData.categories.push(categorySelect ? categorySelect.value : `Раздел ${i}`);
}
localStorage.setItem('jeopardySettings', JSON.stringify({
categories: gameData.categories,
playerNames: playerNames
}));
updateScoreboard();
createGameBoard();
closeSettings();
}
function updateScoreboard() {
const scoreboard = document.querySelector('.score-board');
scoreboard.innerHTML = ''; // Clear existing scoreboard
for (let i = 0; i < playerNames.length; i++) {
const playerScoreDiv = document.createElement('div');
playerScoreDiv.className = 'player-score';
playerScoreDiv.id = `player${i + 1}`;
playerScoreDiv.innerHTML = `<h3>${playerNames[i]}</h3><p id="score${i + 1}">0</p>`;
scoreboard.appendChild(playerScoreDiv);
}
}
function openSettings() {
const settingsModal = document.getElementById('settingsModal');
settingsModal.style.display = 'block';
// Populate settings inputs with current values
document.getElementById('numPlayers').value = playerNames.length;
// Create player name inputs
const playerNamesDiv = document.getElementById('playerNames');
playerNamesDiv.innerHTML = '';
for (let i = 1; i <= playerNames.length; i++) {
playerNamesDiv.innerHTML += `<label for="playerName${i}">Имя игрока ${i}:</label><input type="text" id="playerName${i}" value="${playerNames[i - 1]}"><br>`;
}
// Create category inputs
const categoryInputsDiv = document.getElementById('categoryInputs');
categoryInputsDiv.innerHTML = '';
for (let i = 1; i <= 6; i++) {
let options = '';
allCategories.forEach(cat => {
options += `<option value="${cat}" ${gameData.categories[i - 1] === cat ? 'selected' : ''}>${cat}</option>`;
});
categoryInputsDiv.innerHTML += `
<label for="category${i}">Раздел ${i}:</label>
<select id="category${i}">
<option value="random" ${gameData.categories[i - 1] === 'random' ? 'selected' : ''}>Случайная категория</option>
${options}
</select><br>
`;
}
}
function closeSettings() {
document.getElementById('settingsModal').style.display = 'none';
}
document.getElementById('settingsBtn').addEventListener('click', openSettings);
function createGameBoard() {
const board = document.getElementById('gameBoard');
board.innerHTML = '';
const selectedCategories = new Set();
const savedCategories = gameData.categories;
for (let i = 0; i < savedCategories.length; i++) {
if (savedCategories[i] === 'random') {
while (true) {
const randomIndex = Math.floor(Math.random() * allCategories.length);
const randomCategory = allCategories[randomIndex];
if (!selectedCategories.has(randomCategory)) {
selectedCategories.add(randomCategory);
break;
}
}
} else {
selectedCategories.add(savedCategories[i]);
}
}
selectedCategories.forEach(category => {
const categoryHeader = document.createElement('div');
categoryHeader.className = 'category';
categoryHeader.textContent = category;
board.appendChild(categoryHeader);
});
for (let value = 100; value <= 500; value += 100) {
selectedCategories.forEach(category => {
const cell = document.createElement('div');
cell.className = 'question-cell';
cell.textContent = value;
cell.onclick = () => showQuestion(category, value);
board.appendChild(cell);
});
}
}
function updateCurrentPlayer() {
currentPlayer = (currentPlayer % playerNames.length) + 1;
const currentPlayerDisplay = document.getElementById('currentPlayer');
if (currentPlayerDisplay) {
currentPlayerDisplay.textContent = `Ход игрока: ${playerNames[currentPlayer - 1]}`;
}
// Update active player highlight if the elements exist
const playerScoreElements = document.querySelectorAll('.player-score');
playerScoreElements.forEach(el => el.classList.remove('active'));
const currentPlayerElement = document.getElementById(`player${currentPlayer}`);
if (currentPlayerElement) currentPlayerElement.classList.add('active');
}
function showQuestion(category, value) {
const cell = event.target;
if (cell.classList.contains('used')) return;
currentQuestionValue = value;
const modal = document.getElementById('questionModal');
const questionText = document.getElementById('questionText');
const answerText = document.getElementById('answerText');
// Dynamically add buttons for adding points
const controlsDiv = document.querySelector('#questionModal .controls');
controlsDiv.innerHTML = ''; // Clear existing buttons
for (let i = 0; i < playerNames.length; i++) {
const button = document.createElement('button');
button.className = 'score-btn';
button.textContent = `${playerNames[i]}: +${value}`;
button.onclick = () => addPoints(i + 1);
controlsDiv.appendChild(button);
}
// Get questions for the specific category and value
let questions;
if (!gameData.questions[category] || !gameData.questions[category][value]) {
questions = [{ "q": "Вопрос не найден", "a": "Ответ не найден" }];
} else {
questions = gameData.questions[category][value];
}
// Pick a random question
const randomIndex = Math.floor(Math.random() * questions.length);
const randomQuestion = questions[randomIndex];
questionText.textContent = randomQuestion.q;
answerText.textContent = randomQuestion.a;
answerText.style.display = 'none';
modal.style.display = 'block';
cell.classList.add('used');
}
function addPoints(player) {
if (player >= 1 && player <= playerNames.length) {
scores[`player${player}`] = (scores[`player${player}`] || 0) + currentQuestionValue;
document.getElementById(`score${player}`).textContent = scores[`player${player}`];
document.getElementById('questionModal').style.display = 'none';
updateCurrentPlayer();
} else {
console.error('Invalid player index:', player);
}
}
// Modal controls
document.querySelector('.close-btn').onclick = function() {
document.getElementById('questionModal').style.display = 'none';
updateCurrentPlayer();
}
document.getElementById('showAnswer').onclick = function() {
document.getElementById('answerText').style.display = 'block';
}
// Close settings modal when clicking the close button
document.querySelector('#settingsModal .close-btn').onclick = function() {
closeSettings();
}
window.onclick = function(event) {
const modal = document.getElementById('questionModal');
if (event.target == modal) {
modal.style.display = 'none';
updateCurrentPlayer();
}
}
</script>
</body>
</html>