-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a751be
commit a26ee63
Showing
4 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"application": [ | ||
{ | ||
"name": "a", | ||
"matrix": [ | ||
[1, 1, 1, 1, 1], | ||
[0, 0, 1, 0, 0], | ||
[1, 0, 1, 0, 1], | ||
[1, 0, 1, 1, 1], | ||
[0, 1, 0, 0, 1] | ||
], | ||
"optimal_steps": 10 | ||
}, | ||
{ | ||
"name": "b", | ||
"matrix": [ | ||
[1, 0, 1, 0, 0], | ||
[0, 1, 1, 1, 1], | ||
[0, 0, 1, 1, 0], | ||
[0, 0, 1, 0, 0], | ||
[0, 1, 1, 1, 0] | ||
], | ||
"optimal_steps": 8 | ||
}, | ||
{ | ||
"name": "c", | ||
"matrix": [ | ||
[1, 0, 0, 0, 0], | ||
[0, 1, 1, 0, 1], | ||
[1, 0, 0, 1, 1], | ||
[0, 0, 1, 1, 1], | ||
[1, 1, 0, 0, 0] | ||
], | ||
"optimal_steps": 12 | ||
} | ||
] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html lang="uk"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Lights Out Game </title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
<h1>Lights Out Game</h1> | ||
<div>Час: <span id="timer">0</span> секунд</div> | ||
<div>Кроки: <span id="stepCounter">0</span></div> | ||
|
||
<div id="grid" class="grid"></div> | ||
<button id="nextScenarioButton">Наступний сценарій</button> | ||
|
||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const gridSize = 5; | ||
const cells = []; | ||
let stepCounter = 0; | ||
let timer = 0; | ||
let interval; | ||
let scenarioIndex = 0; | ||
let scenarios = []; | ||
|
||
const timerElement = document.getElementById('timer'); | ||
const stepCounterElement = document.getElementById('stepCounter'); | ||
const bestStepsElement = document.getElementById('bestSteps'); | ||
const bestTimeElement = document.getElementById('bestTime'); | ||
const grid = document.getElementById('grid'); | ||
const nextScenarioButton = document.getElementById('nextScenarioButton'); | ||
|
||
nextScenarioButton.addEventListener('click', nextScenario); | ||
|
||
async function fetchScenarios() { | ||
try { | ||
const response = await fetch('application.json'); // Переконайтеся, що шлях правильний | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
scenarios = await response.json(); | ||
loadScenario(scenarios[0]); // Завантаження першого сценарію після отримання даних | ||
} catch (error) { | ||
console.error('Помилка при завантаженні сценаріїв:', error); | ||
} | ||
} | ||
|
||
function createGrid() { | ||
cells.length = 0; | ||
grid.innerHTML = ""; | ||
for (let i = 0; i < gridSize * gridSize; i++) { | ||
const cell = document.createElement('div'); | ||
cell.className = 'cell'; | ||
cell.addEventListener('click', () => { | ||
toggleLights(i); | ||
incrementStep(); | ||
}); | ||
grid.appendChild(cell); | ||
cells.push(cell); | ||
} | ||
} | ||
|
||
function resetGame() { | ||
clearInterval(interval); | ||
stepCounter = 0; | ||
stepCounterElement.innerText = 0; | ||
timerElement.innerText = 0; | ||
cells.forEach(cell => cell.classList.remove('on')); | ||
startTimer(); | ||
} | ||
|
||
function incrementStep() { | ||
stepCounter++; | ||
stepCounterElement.innerText = stepCounter; | ||
checkGameStatus(); | ||
} | ||
|
||
function startTimer() { | ||
clearInterval(interval); | ||
timer = 0; | ||
interval = setInterval(() => { | ||
timer++; | ||
timerElement.innerText = timer; | ||
}, 1000); | ||
} | ||
|
||
function toggleLights(index) { | ||
toggleCell(index); | ||
|
||
if (index >= gridSize) toggleCell(index - gridSize); // верхній сусід | ||
if (index < gridSize * (gridSize - 1)) toggleCell(index + gridSize); // нижній сусід | ||
if (index % gridSize > 0) toggleCell(index - 1); // лівий сусід | ||
if (index % gridSize < gridSize - 1) toggleCell(index + 1); // правий сусід | ||
} | ||
|
||
function toggleCell(index) { | ||
const cell = cells[index]; | ||
cell.classList.toggle('on'); | ||
} | ||
|
||
function randomize() { | ||
resetGame(); | ||
for (let i = 0; i < gridSize; i++) { | ||
const randomIndex = Math.floor(Math.random() * (gridSize * gridSize)); | ||
toggleLights(randomIndex); | ||
} | ||
} | ||
|
||
function loadScenario(scenario) { | ||
resetGame(); | ||
scenario.matrix.forEach((row, r) => { | ||
row.forEach((value, c) => { | ||
if (value === 1) { | ||
const index = r * gridSize + c; | ||
toggleCell(index); | ||
} | ||
}); | ||
}); | ||
bestStepsElement.innerText = scenario.optimalSteps; | ||
} | ||
|
||
function nextScenario() { | ||
if (scenarioIndex < scenarios.length) { | ||
loadScenario(scenarios[scenarioIndex]); | ||
scenarioIndex++; | ||
} else { | ||
scenarioIndex = 0; // Reset the index for the next cycle | ||
randomize(); // Генерація випадкового сценарію після проходження всіх фіксованих сценаріїв | ||
} | ||
} | ||
|
||
function checkGameStatus() { | ||
if (checkVictory()) { | ||
alert("Вітаю! Ви виграли!"); | ||
saveBestTime(); // Збереження найкращого часу на сервері | ||
resetGame(); // Автоматичний перезапуск після виграшу | ||
} | ||
} | ||
|
||
function checkVictory() { | ||
return cells.every(cell => !cell.classList.contains('on')); | ||
} | ||
|
||
function saveBestTime() { | ||
const bestTime = timer; // Зберігаємо найкращий час, коли гравець виграв | ||
fetch('https://example.com/api/save-time', { | ||
method: 'POST', // Відправляємо дані POST-запитом | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ bestTime }), | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
console.log("Найкращий час збережено:", data); | ||
}) | ||
.catch(error => { | ||
console.error('Помилка при збереженні найкращого часу:', error); | ||
}); | ||
} | ||
|
||
createGrid(); | ||
startTimer(); | ||
fetchScenarios(); // Завантаження сценаріїв з JSON при завантаженні сторінки | ||
// getBestTime(); // Завантаження найкращого часу при завантаженні сторінки | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
.grid { | ||
display: grid; | ||
grid-template-columns: repeat(5, 100px); | ||
grid-gap: 5px; | ||
} | ||
.cell { | ||
width: 100px; | ||
height: 100px; | ||
background-color: lightgray; | ||
border: 1px solid black; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
cursor: pointer; | ||
user-select: none; | ||
} | ||
.cell.on { | ||
background-color: rgb(20, 95, 83); | ||
} |