diff --git a/Lekcja_04/index.html b/Lekcja_04/index.html index e1f9d28..f0f4bcd 100644 --- a/Lekcja_04/index.html +++ b/Lekcja_04/index.html @@ -79,16 +79,13 @@ } .odkryte-pole { - background-color: transparent; + background-color: darkgray; + font-weight: bold; } #message { text-align: center; } - - .debug-czerwony { - background-color: red; - } @@ -132,43 +129,98 @@ let m = 0 let gra = true + let polozono_miny = false let odkryte_pola = 0 // 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"] - function flaga() { - console.log(this, this.dataset.x, this.dataset.y) - - let x = this.dataset.x - let y = this.dataset.y + function flaga(btn) { + let x = btn.dataset.x + let y = btn.dataset.y if (!gra) { return } event.preventDefault() - console.log(`Oflagowano pole (${x}, ${y})`) + if (btn.classList.contains("nieodkryte-pole")) { + plansza[y][x].flaga = !plansza[y][x].flaga; + plansza[y][x].flaga ? btn.innerHTML = '🚩' : btn.innerHTML = ''; + } } - function klik() { - console.log(this, this.dataset.x, this.dataset.y) + function klik(btn) { + let x = btn.dataset.x + let y = btn.dataset.y - let x = this.dataset.x - let y = this.dataset.y + if (!polozono_miny) { + losuj_miny(x, y) + polozono_miny = true + } if (!gra) { return } - console.log(`Kliknięto pole (${x}, ${y})`) + if (plansza[y][x].flaga) { return } + if (document.getElementById(`p${x}${y}`).classList.contains("odkryte-pole")) { return } if (plansza[y][x].mina) { - alert("xdddd No i kurna przegrałeś") + alert("Koniec gry, przegrana!") + document.getElementById(`p${x}${y}`).style.backgroundColor = "red"; gra = false + } else { + btn.classList.remove("nieodkryte-pole") + btn.classList.add("odkryte-pole") + if (plansza[y][x].sasiednie_miny != 0) { + btn.innerText = plansza[y][x].sasiednie_miny + btn.style.color = kolorki[plansza[y][x].sasiednie_miny - 1] + } + odkryte_pola++ + if (odkryte_pola == (w * h) - m) { + alert("Koniec gry, wygrana!") + gra = false + } + } + + // jeśli nie ma min dookoła to odkryj pola sąsiednie + if (plansza[y][x].sasiednie_miny == 0) { + let i = y; + 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}`)) + } + } + + 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}`)) + } + } + + j = x; + 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}`)) + } + } + + 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}`)) + } + } } } function przygotuj() { plansza = [] - const plansza_table = document.getElementById("plansza") plansza_table.innerHTML = '' @@ -178,36 +230,28 @@ plansza.push([]) n_html += '
diff --git a/Lekcja_05/codeshare_zdarzenia.md b/Lekcja_05/codeshare_zdarzenia.md new file mode 100644 index 0000000..c841d98 --- /dev/null +++ b/Lekcja_05/codeshare_zdarzenia.md @@ -0,0 +1,69 @@ + + + +
+ + +
+ + + +
+ +