Ćwiczenia z dat, dodanie timera do sapera i poprawienie kolizji id pól w saperze

This commit is contained in:
Adam Łukasik committed 2026-10-20 08:54:22 +02:00
1 parent cb6e9f8739
commit 9790eb5b02
2 files changed
+166 -11

No files matched your search

+55 -11
View File
@@ -86,6 +86,19 @@
#message {
text-align: center;
}
.timer {
display: flex;
justify-content: space-between;
}
.timer>p {
font-size: 1.5em;
}
.timer>p:last-child {
font-weight: bold;
}
</style>
</head>
@@ -112,6 +125,10 @@
</div>
<hr>
</div>
<div class="timer" id="timer-wrapper">
<p>Twój czas:</p>
<p id="timer-time">0:00</p>
</div>
<div class="game">
<table id="plansza">
</table>
@@ -132,6 +149,11 @@
let polozono_miny = false
let odkryte_pola = 0
let czas_startu;
let czas_obecny;
let ms_gry;
let interwal;
// tablica z polami
let plansza = [] // tablice obiektów (pól) { x: int, y: int, mina: true/false, sasiednie_miny: int }
const kolorki = ["red", "green", "blue", "darkseagreen", "cornflowerblue", "beige", "orange", "navy"]
@@ -154,18 +176,36 @@
let y = btn.dataset.y
if (!polozono_miny) {
czas_startu = new Date();
if (interwal != undefined) { clearInterval(interwal); }
interwal = setInterval(() => {
czas_obecny = new Date();
ms_gry = czas_obecny.getTime() - czas_startu.getTime();
let min = Math.floor(ms_gry / (60000));
let sec = Math.floor((ms_gry % 60000) / 1000);
let piekny_string = min + ":" + (sec > 9 ? sec : `0${sec}`);
document.getElementById("timer-time").innerText = piekny_string;
}, 500); // mniej niż sekunda bo moze sie zdarzyć, że interwał potrwa więcej niż sekundę
// i wtedy zegar nam przeskoczy o dwie sekundy i to by było brzydkie dość
losuj_miny(x, y)
polozono_miny = true
}
if (!gra) { return }
if (plansza[y][x].flaga) { return }
if (document.getElementById(`p${x}${y}`).classList.contains("odkryte-pole")) { return }
if (document.getElementById(`p${x}-${y}`).classList.contains("odkryte-pole")) { return }
if (plansza[y][x].mina) {
alert("Koniec gry, przegrana!")
document.getElementById(`p${x}${y}`).style.backgroundColor = "red";
document.getElementById(`p${x}-${y}`).style.backgroundColor = "red";
gra = false
clearInterval(interwal);
} else {
btn.classList.remove("nieodkryte-pole")
btn.classList.add("odkryte-pole")
@@ -177,6 +217,7 @@
if (odkryte_pola == (w * h) - m) {
alert("Koniec gry, wygrana!")
gra = false
clearInterval(interwal);
}
}
@@ -186,16 +227,16 @@
let j = x - 1;
if (i >= 0 && i < h && j >= 0 && j < w) {
if (document.getElementById(`p${j}${i}`).classList.contains("nieodkryte-pole")) {
klik(document.getElementById(`p${j}${i}`))
if (document.getElementById(`p${j}-${i}`).classList.contains("nieodkryte-pole")) {
klik(document.getElementById(`p${j}-${i}`))
}
}
j += 2;
if (i >= 0 && i < h && j >= 0 && j < w) {
if (document.getElementById(`p${j}${i}`).classList.contains("nieodkryte-pole")) {
klik(document.getElementById(`p${j}${i}`))
if (document.getElementById(`p${j}-${i}`).classList.contains("nieodkryte-pole")) {
klik(document.getElementById(`p${j}-${i}`))
}
}
@@ -203,16 +244,16 @@
i = y - 1;
if (i >= 0 && i < h && j >= 0 && j < w) {
if (document.getElementById(`p${j}${i}`).classList.contains("nieodkryte-pole")) {
klik(document.getElementById(`p${j}${i}`))
if (document.getElementById(`p${j}-${i}`).classList.contains("nieodkryte-pole")) {
klik(document.getElementById(`p${j}-${i}`))
}
}
i += 2;
if (i >= 0 && i < h && j >= 0 && j < w) {
if (document.getElementById(`p${j}${i}`).classList.contains("nieodkryte-pole")) {
klik(document.getElementById(`p${j}${i}`))
if (document.getElementById(`p${j}-${i}`).classList.contains("nieodkryte-pole")) {
klik(document.getElementById(`p${j}-${i}`))
}
}
}
@@ -231,11 +272,14 @@
n_html += '<tr>'
for (let j = 0; j < w; j++) {
plansza[i].push({ x: j, y: i, mina: false, sasiednie_miny: 0, flaga: false })
n_html += `<td class="nieodkryte-pole" id="p${j}${i}" onclick="klik(this)" oncontextmenu="flaga(this)" oncontextmenu="flaga" data-x="${j}" data-y="${i}"></td>`
n_html += `<td class="nieodkryte-pole" id="p${j}-${i}" onclick="klik(this)" oncontextmenu="flaga(this)" oncontextmenu="flaga" data-x="${j}" data-y="${i}"></td>`
}
n_html += '</tr>'
}
plansza_table.innerHTML += n_html
document.getElementById("timer-wrapper").style.marginBottom = ".5em";
document.getElementById("timer-time").innerText = "0:00";
}
function losuj_miny(unikaj_x = 0, unikaj_y = 0) {