Ćwiczenia z dat, dodanie timera do sapera i poprawienie kolizji id pól w saperze
This commit is contained in:
1 parent
cb6e9f8739
commit
9790eb5b02
2 files changed
+166
-11
No files matched your search
+55
-11
@@ -86,6 +86,19 @@
|
|||||||
#message {
|
#message {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.timer {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
|
||||||
|
.timer>p {
|
||||||
|
font-size: 1.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.timer>p:last-child {
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
@@ -112,6 +125,10 @@
|
|||||||
</div>
|
</div>
|
||||||
<hr>
|
<hr>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="timer" id="timer-wrapper">
|
||||||
|
<p>Twój czas:</p>
|
||||||
|
<p id="timer-time">0:00</p>
|
||||||
|
</div>
|
||||||
<div class="game">
|
<div class="game">
|
||||||
<table id="plansza">
|
<table id="plansza">
|
||||||
</table>
|
</table>
|
||||||
@@ -132,6 +149,11 @@
|
|||||||
let polozono_miny = false
|
let polozono_miny = false
|
||||||
let odkryte_pola = 0
|
let odkryte_pola = 0
|
||||||
|
|
||||||
|
let czas_startu;
|
||||||
|
let czas_obecny;
|
||||||
|
let ms_gry;
|
||||||
|
let interwal;
|
||||||
|
|
||||||
// tablica z polami
|
// tablica z polami
|
||||||
let plansza = [] // tablice obiektów (pól) { x: int, y: int, mina: true/false, sasiednie_miny: int }
|
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"]
|
const kolorki = ["red", "green", "blue", "darkseagreen", "cornflowerblue", "beige", "orange", "navy"]
|
||||||
@@ -154,18 +176,36 @@
|
|||||||
let y = btn.dataset.y
|
let y = btn.dataset.y
|
||||||
|
|
||||||
if (!polozono_miny) {
|
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)
|
losuj_miny(x, y)
|
||||||
polozono_miny = true
|
polozono_miny = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!gra) { return }
|
if (!gra) { return }
|
||||||
if (plansza[y][x].flaga) { 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) {
|
if (plansza[y][x].mina) {
|
||||||
alert("Koniec gry, przegrana!")
|
alert("Koniec gry, przegrana!")
|
||||||
document.getElementById(`p${x}${y}`).style.backgroundColor = "red";
|
document.getElementById(`p${x}-${y}`).style.backgroundColor = "red";
|
||||||
gra = false
|
gra = false
|
||||||
|
clearInterval(interwal);
|
||||||
} else {
|
} else {
|
||||||
btn.classList.remove("nieodkryte-pole")
|
btn.classList.remove("nieodkryte-pole")
|
||||||
btn.classList.add("odkryte-pole")
|
btn.classList.add("odkryte-pole")
|
||||||
@@ -177,6 +217,7 @@
|
|||||||
if (odkryte_pola == (w * h) - m) {
|
if (odkryte_pola == (w * h) - m) {
|
||||||
alert("Koniec gry, wygrana!")
|
alert("Koniec gry, wygrana!")
|
||||||
gra = false
|
gra = false
|
||||||
|
clearInterval(interwal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,16 +227,16 @@
|
|||||||
let j = x - 1;
|
let j = x - 1;
|
||||||
|
|
||||||
if (i >= 0 && i < h && j >= 0 && j < w) {
|
if (i >= 0 && i < h && j >= 0 && j < w) {
|
||||||
if (document.getElementById(`p${j}${i}`).classList.contains("nieodkryte-pole")) {
|
if (document.getElementById(`p${j}-${i}`).classList.contains("nieodkryte-pole")) {
|
||||||
klik(document.getElementById(`p${j}${i}`))
|
klik(document.getElementById(`p${j}-${i}`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
j += 2;
|
j += 2;
|
||||||
|
|
||||||
if (i >= 0 && i < h && j >= 0 && j < w) {
|
if (i >= 0 && i < h && j >= 0 && j < w) {
|
||||||
if (document.getElementById(`p${j}${i}`).classList.contains("nieodkryte-pole")) {
|
if (document.getElementById(`p${j}-${i}`).classList.contains("nieodkryte-pole")) {
|
||||||
klik(document.getElementById(`p${j}${i}`))
|
klik(document.getElementById(`p${j}-${i}`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,16 +244,16 @@
|
|||||||
i = y - 1;
|
i = y - 1;
|
||||||
|
|
||||||
if (i >= 0 && i < h && j >= 0 && j < w) {
|
if (i >= 0 && i < h && j >= 0 && j < w) {
|
||||||
if (document.getElementById(`p${j}${i}`).classList.contains("nieodkryte-pole")) {
|
if (document.getElementById(`p${j}-${i}`).classList.contains("nieodkryte-pole")) {
|
||||||
klik(document.getElementById(`p${j}${i}`))
|
klik(document.getElementById(`p${j}-${i}`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
i += 2;
|
i += 2;
|
||||||
|
|
||||||
if (i >= 0 && i < h && j >= 0 && j < w) {
|
if (i >= 0 && i < h && j >= 0 && j < w) {
|
||||||
if (document.getElementById(`p${j}${i}`).classList.contains("nieodkryte-pole")) {
|
if (document.getElementById(`p${j}-${i}`).classList.contains("nieodkryte-pole")) {
|
||||||
klik(document.getElementById(`p${j}${i}`))
|
klik(document.getElementById(`p${j}-${i}`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,11 +272,14 @@
|
|||||||
n_html += '<tr>'
|
n_html += '<tr>'
|
||||||
for (let j = 0; j < w; j++) {
|
for (let j = 0; j < w; j++) {
|
||||||
plansza[i].push({ x: j, y: i, mina: false, sasiednie_miny: 0, flaga: false })
|
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>'
|
n_html += '</tr>'
|
||||||
}
|
}
|
||||||
plansza_table.innerHTML += n_html
|
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) {
|
function losuj_miny(unikaj_x = 0, unikaj_y = 0) {
|
||||||
|
|||||||
@@ -0,0 +1,111 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="pl">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Ćwiczenia daty</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
Otwórz konsolę
|
||||||
|
<script>
|
||||||
|
function format_date_to_text(date_object) {
|
||||||
|
const t = date_object.toString().split(' ');
|
||||||
|
let s = '';
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
i == 3 ? s += t[i] : s += t[i] + ' ';
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(format_date_to_text(new Date()));
|
||||||
|
|
||||||
|
function get_current_period(date_object) {
|
||||||
|
return [date_object.getFullYear(), date_object.getMonth()];
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(get_current_period(new Date()));
|
||||||
|
|
||||||
|
function is_weekend(date_object) {
|
||||||
|
return date_object.getDay() == 0 || date_object.getDay() == 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(is_weekend(new Date()))
|
||||||
|
console.log(is_weekend(new Date("2026-10-18")));
|
||||||
|
|
||||||
|
function create_christmas_2026() {
|
||||||
|
return new Date("2026-12-24 15:30:00");
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(create_christmas_2026().toString());
|
||||||
|
|
||||||
|
function get_timestamp_with_new_date(date_str) {
|
||||||
|
const d = new Date(date_str);
|
||||||
|
return d.getTime();
|
||||||
|
}
|
||||||
|
function get_timestamp_with_parse(date_str) {
|
||||||
|
return Date.parse(date_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(get_timestamp_with_new_date("2026-10-07"));
|
||||||
|
console.log(get_timestamp_with_parse("2026-10-07"));
|
||||||
|
|
||||||
|
function set_as_midnight(date_object) {
|
||||||
|
date_object.setUTCHours("0");
|
||||||
|
date_object.setUTCMinutes("0");
|
||||||
|
date_object.setUTCSeconds("0");
|
||||||
|
date_object.setUTCMilliseconds("0");
|
||||||
|
}
|
||||||
|
|
||||||
|
let myDate = new Date("2026-10-07T14:45:30");
|
||||||
|
set_as_midnight(myDate);
|
||||||
|
console.log(myDate.toISOString());
|
||||||
|
|
||||||
|
function has_expired(exp, today) {
|
||||||
|
const d = exp.getTime() - today.getTime();
|
||||||
|
return d < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let exp = new Date("2026-05-01");
|
||||||
|
let today = new Date("2026-10-07");
|
||||||
|
console.log(has_expired(exp, today));
|
||||||
|
|
||||||
|
function measure_loop(a) {
|
||||||
|
const przed = new Date().getTime();
|
||||||
|
for (let i = 0; i < a; i++) {
|
||||||
|
let whata;
|
||||||
|
i % 2 == 0 ? whata = 'frika' : whata = 'afrika';
|
||||||
|
}
|
||||||
|
const po = new Date().getTime();
|
||||||
|
|
||||||
|
return "Operacja trwała: " + (po - przed) + " ms";
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(measure_loop(1000000));
|
||||||
|
|
||||||
|
function add_days(date_object, days) {
|
||||||
|
const ms = days * 86400000;
|
||||||
|
|
||||||
|
date_object.setTime(date_object.getTime() + ms);
|
||||||
|
|
||||||
|
return date_object;
|
||||||
|
}
|
||||||
|
|
||||||
|
let d = new Date("2026-10-20");
|
||||||
|
console.log(d.toString());
|
||||||
|
console.log(add_days(d, 5).toString());
|
||||||
|
|
||||||
|
function days_until_new_year(date_object) {
|
||||||
|
let jan = new Date(`${date_object.getFullYear() + 1}-01-01`);
|
||||||
|
|
||||||
|
let days = Math.ceil((jan.getTime() - date_object.getTime()) / 86400000);
|
||||||
|
|
||||||
|
return days;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(days_until_new_year(new Date("2026-12-26 15:00:00")));
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
Reference in new issue
Block a user