Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Saper dla pana Mendeli</title>
- <link href="https://fonts.googleapis.com/css?family=Henny+Penny&display=swap" rel="stylesheet">
- <style>
- * {
- font-family: "henny penny";
- -moz-user-select: none;
- user-select: none;
- }
- #wysdiv {
- float: left;
- margin-left: 40%;
- margin-top: 5%;
- margin-bottom: 15px;
- -moz-user-select: none;
- user-select: none;
- }
- #szerdiv {
- float: left;
- margin-left: 40%;
- margin-bottom: 15px;
- -moz-user-select: none;
- user-select: none;
- }
- #bombdiv {
- float: left;
- margin-left: 40%;
- margin-bottom: 15px;
- -moz-user-select: none;
- user-select: none;
- }
- #container {
- clear: both;
- float: left;
- margin-left: 45%;
- }
- #plansza {
- float: left;
- margin-top: 2%;
- margin-left: 40%;
- clear: both;
- width: 100%;
- }
- #ilebomb {
- margin-top: 10px;
- }
- .komorka {
- width: 10px;
- height: 10px;
- padding: 10px;
- border: 1px solid black;
- float: left;
- background-color: gray;
- color: grey;
- text-align: center;
- -moz-user-select: none;
- user-select: none;
- }
- .nic {
- width: 10px;
- height: 10px;
- padding: 10px;
- border: 1px solid black;
- float: left;
- background-color: gray;
- color: grey;
- text-align: center;
- -moz-user-select: none;
- user-select: none;
- }
- #rzad {
- width: 100%;
- height: 32px;
- clear: both;
- }
- .bomba {
- width: 10px;
- height: 10px;
- padding: 10px;
- border: 1px solid black;
- float: left;
- background-color: gray;
- color: grey;
- text-align: center;
- -moz-user-select: none;
- user-select: none;
- background: url(tlo.png) grey;
- }
- .disabled {
- pointer-events: none;
- }
- </style>
- </head>
- <body>
- <script>
- var div = document.createElement("div")
- var wysdiv = document.createElement("div")
- var szerdiv = document.createElement("div")
- var bombdiv = document.createElement("div") //Tworzymy tutaj wszystkie elementy na strone, ktre na końcu dokumentu przypiszemy konkretnym elementom html (takim jak body czy umieścimy je w jakimś divie)
- var ilebomb = document.createElement("div")
- var plansza = document.createElement("div")
- var p = document.createElement("p")
- var wys = document.createElement("input")
- var szer = document.createElement("input")
- var bomb = document.createElement("input")
- var iloscbomb = 0
- var n = 0
- var wygranko = 0
- var u = 0
- var l = 0
- var v = 0
- var klik = 0
- var pierwszyklik = false
- var licznikbomb = 0
- var wymaganailosc = true
- var list = Array()
- var list2 = Array()
- p.id = "czolgi"
- div.id = "container"
- wys.id = "wys"
- szer.id = "szer" //Ustalamy tutaj id wcześniej stworzonych elementów
- bomb.id = "bomb"
- bombdiv.id = "bombdiv"
- wysdiv.id = "wysdiv"
- szerdiv.id = "szerdiv"
- ilebomb.id = "ilebomb"
- plansza.id = "plansza"
- wysdiv.append("Podaj Wysokość: ")
- szerdiv.append("Podaj szerokość: ") //Ddodajemy tutaj zawartość do utworzonych wcześniej divów aby zawierały tekst który potrzebujemy
- bombdiv.append("Podaj ilość bomb: ")
- wys.setAttribute("type", "text")
- szer.setAttribute("type", "text") //Ustalamy atrybuty utworzonych wcześniej "inputów"
- bomb.setAttribute("type", "text")
- function shuffle(array) {
- var currentIndex = array.length, temporaryValue, randomIndex;
- // While there remain elements to shuffle...
- while (0 !== currentIndex) {
- // Pick a remaining element...
- randomIndex = Math.floor(Math.random() * currentIndex); //Funkcja ta pozwoli nam losowo rozłozyć elementy w tabli później w kodzie
- currentIndex -= 1;
- // And swap it with the current element.
- temporaryValue = array[currentIndex];
- array[currentIndex] = array[randomIndex];
- array[randomIndex] = temporaryValue;
- }
- return array;
- }
- plansza.addEventListener('contextmenu', function (evt) {
- evt.preventDefault();
- }, false);
- wys.oninput = function () { //
- var that = this //
- var aaa = setInterval(function () { //
- if (isNaN(that.value)) { //
- that.value = "" //
- clearInterval(aaa) //
- } //
- else { //
- clearInterval(aaa) //
- } //
- }, 1000) //
- } //
- szer.oninput = function () { //
- var that = this //
- var aaa = setInterval(function () { //
- if (isNaN(that.value)) { //
- that.value = "" //
- clearInterval(aaa) // W tym miejscu mamy funkcje które sprawdzają czy w nasze inputy wprowadzmy liczby, w przeciwnym wypadku po sekundzie czyszczą pole
- } //
- else { //
- clearInterval(aaa) //
- } //
- }, 1000) //
- } //
- bomb.oninput = function () { //
- var that = this //
- var aaa = setInterval(function () { //
- if (isNaN(that.value)) { //
- that.value = "" //
- clearInterval(aaa) //
- } //
- else { //
- clearInterval(aaa) //
- } //
- }, 1000) //
- } //
- var sek = 0
- var button = document.createElement("button");
- button.innerHTML = "GENERUJ";
- button.onclick = function () {
- p.innerHTML = "Czas 0 sek"
- var pole = wys.value * szer.value
- var ttt = setInterval(function () {
- sek++ //Wprowadzamy odliczanie
- p.innerHTML = "Czas " + sek + " sek"
- }, 1000)
- if (bomb.value == "" || wys.value == "" || szer.value == "" || bomb.value > pole || bomb.value == 0) { //Tutaj ustalamy działanie przycisku generuj - w tej konkretnej linijce funkcja sprawdzająca czy wszystkie dane są wprowadzone
- alert("Podaj poprawne watości")
- clearInterval(ttt)
- }
- else {
- container.className = "disabled"
- var bomby = setInterval(function () {
- ile = bomb.value - licznikbomb //licznik bomb
- ilebomb.innerHTML = "Zostało " + ile + " bomb"
- }, 1)
- function rekurencja(rekurencja) {
- var to = this.id
- var tot = to.split("komorka");
- parseInt(tot)
- console.log(tot)
- //document.getElementById(nnadlewo).click()
- //document.getElementById(nnad).click()
- //document.getElementById(nnadprawo).click()
- //document.getElementById(ooboklewo).click()
- //document.getElementById("komorka"+to).click()
- //document.getElementById(oobokprawo).click()
- //document.getElementById(ppodlewo).click()
- //document.getElementById(ppod).click()
- //document.getElementById(ppodprawo).click()
- //document.getElementById("komorka" + 3).click()
- //document.getElementById("komorka" + n).click()
- //console.log(to)
- }
- function klikanie(klikanie) {
- if (klik == 0 || klik == 3) {
- klik = 2
- this.style.backgroundColor = "white" //Funkcja klikania się komórek planszy
- this.style.color = "Black"
- this.style.backgroundImage = "url('nic.png')";
- this.style.fontSize = "15px"
- wygranko++
- if (wygranko == pole - bomb.value) {
- alert("Wygrales")
- clearInterval(ttt)
- licznikbomb = bomb.value
- plansza.className = "disabled"
- var style = document.createElement('style');
- style.innerHTML = `
- .komorka {
- color: Black;
- background-color: White !important;
- background: url(nic.png) !important;
- }
- .nic {
- color: Black;
- background-color: White !important;
- background: url(nic.png) !important;
- }
- .bomba {
- color: Black;
- background-color: gray;
- background: url(bomba.png), gray !important;
- }
- `;
- document.head.appendChild(style);
- }
- this.setAttribute("oncontextmenu", "return false;")
- }
- }
- function klikaniebomba(klikaniebomba) {
- if (klik == 0 || klik == 3) {
- klik = 2
- plansza.className = "disabled"
- v = 0
- licznikbomb = bomb.value
- var style = document.createElement('style');
- style.innerHTML = `
- .bomba {
- color: Black;
- background-color: gray;
- background: url(bomba.png), gray !important;
- }
- `;
- document.head.appendChild(style); //Funkcja klikania bomby
- clearInterval(ttt)
- var aaa = confirm("Przegrales, aby rozpocząć od nowa kliknij przycisk 'ok'")
- if (aaa == true) {
- location.reload();
- }
- if (aaa == false) {
- /*var style = document.createElement('style');
- style.innerHTML = `
- .komorka {
- color: Black;
- background-color: White !important;
- background: url(nic.png) !important;
- }
- .nic {
- color: Black;
- background-color: White !important;
- background: url(nic.png) !important;
- }
- `;
- document.head.appendChild(style);*/
- }
- }
- }
- for (i = 0; i < pole; i++) {
- l++
- list.push(l)
- }
- list = shuffle(list)
- for (i = 0; i < bomb.value; i++) {
- var liczba = list.slice(i, i + 1) //Tutaj ustalamy w których komórkach będą bomby
- list2.push(liczba)
- }
- list2.sort(function (a, b) { return a - b }); //i sortujemy roznąco
- var pod = szer.value
- var podlewo = pod - 1
- var podprawo = pod
- podprawo++
- //potrzebne liczniki do liczb na planszy
- var nad = 0 - szer.value
- var nadlewo = nad - 1
- var nadprawo = nad
- nadprawo++
- var obokprawo = 1
- var oboklewo = -1
- for (i = 0; i < wys.value; i++) {
- var rzad = document.createElement("div")
- rzad.id = "rzad"
- plansza.appendChild(rzad) //Generowanie się komórek i rzędów planszy
- for (k = 0; k < szer.value; k++) {
- parseInt(n)
- parseInt(pod)
- parseInt(podlewo)
- parseInt(podprawo)
- parseInt(nad)
- parseInt(nadlewo)
- parseInt(nadprawo)
- parseInt(obokprawo)
- parseInt(oboklewo)
- n++
- pod++
- podlewo++
- podprawo++
- nad++
- nadlewo++
- nadprawo++
- obokprawo++
- oboklewo++
- var wyss = wys.value
- var lewakrawedz = (pole - szer.value) + 2
- var dolnakrawedz = (pole - szer.value) + 1
- komorka = document.createElement("div")
- komorka.id = "komorka" + n
- //komorka.className = "komorka"
- rzad.appendChild(komorka)
- if (szer.value == 1) {
- if (n == 1) { //
- v = 0
- for (c = 0; c < list2.length; c++) { //
- if (pod == list2[c]) { //
- v++ //
- } //
- }
- }
- if (n > 1 && n < pole) {
- v = 0
- for (c = 0; c < list2.length; c++) { //
- if (nad == list2[c]) { //
- v++ //
- } //
- }
- for (c = 0; c < list2.length; c++) { //
- if (pod == list2[c]) { //
- v++ //
- } //
- }
- }
- if (n == pole) { //
- v = 0
- for (c = 0; c < list2.length; c++) { //
- if (nad == list2[c]) { //
- v++ //
- } //
- }
- //
- }
- }
- if (szer.value != 1) {
- v = 0
- if (n > 1 && n < szer.value) { //
- //
- for (c = 0; c < list2.length; c++) { //
- if (oboklewo == list2[c]) { //
- v++ //
- } //
- } //
- for (c = 0; c < list2.length; c++) { //
- if (obokprawo == list2[c]) { //
- v++ //
- } //
- } //
- for (c = 0; c < list2.length; c++) { //
- if (podlewo == list2[c]) { //
- v++ // gorna krawedz bez rogow (fixed?)
- } //
- } //
- for (c = 0; c < list2.length; c++) { //
- if (pod == list2[c]) { //
- v++ //
- } //
- } //
- for (c = 0; c < list2.length; c++) { //
- if (podprawo == list2[c]) { //
- v++ //
- } //
- } //
- } //
- if (((n - 1) % szer.value) == 0 && n < lewakrawedz) { //lewa krawedz
- v = 0
- for (c = 0; c < list2.length; c++) {
- if (nad == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (nadprawo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (obokprawo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (pod == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (podprawo == list2[c]) {
- v++
- }
- }
- }
- if (n > szer.value && n % szer.value == 0 && n < pole) { //prawa krawedz bez rogow
- v = 0
- for (c = 0; c < list2.length; c++) {
- if (nadlewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (nad == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (oboklewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (podlewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (pod == list2[c]) {
- v++
- }
- }
- }
- if (n > dolnakrawedz && n < pole) { //dolna krawedz bez rogow
- v = 0
- for (c = 0; c < list2.length; c++) {
- if (nadlewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (nad == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (nadprawo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (oboklewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (obokprawo == list2[c]) {
- v++
- }
- }
- }
- if (n == 1) { //lewy gorny rog (fixed?)
- v = 0
- for (c = 0; c < list2.length; c++) {
- if (n + 1 == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (pod == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (podprawo == list2[c]) {
- v++
- }
- }
- }
- if (n == szer.value) { //prawy gorny rog (fixed?)
- v = 0
- for (c = 0; c < list2.length; c++) {
- if (n - 1 == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (podlewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (pod == list2[c]) {
- v++
- }
- }
- }
- if (n == dolnakrawedz) { //lewy dolny rog
- v = 0
- for (c = 0; c < list2.length; c++) {
- if (nad == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (nadprawo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (obokprawo == list2[c]) {
- v++
- }
- }
- }
- if (n == pole) { //prawy dolny rog
- v = 0
- for (c = 0; c < list2.length; c++) {
- if (nadlewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (nad == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (oboklewo == list2[c]) {
- v++
- }
- }
- }
- if (n > szer.value && (n - 1) % szer.value != 0 && n % szer.value != 0 && n < dolnakrawedz) { //srodek
- v = 0
- for (c = 0; c < list2.length; c++) {
- if (nadlewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (nad == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (nadprawo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (oboklewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (obokprawo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (podlewo == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (pod == list2[c]) {
- v++
- }
- }
- for (c = 0; c < list2.length; c++) {
- if (podprawo == list2[c]) {
- v++
- }
- }
- }
- }
- if (n != list2[u]) {
- komorka.style.color = "grey"
- if (v == 0) {
- komorka.className = "nic"
- komorka.innerHTML = "0"
- komorka.addEventListener('click', (rekurencja), { once: true });
- }
- if (v != 0) {
- komorka.className = "komorka"
- komorka.innerHTML = v
- }
- komorka.onmouseenter = function () {
- if (this.style.color == "yellow") {
- klik = 1
- }
- if (this.style.color == "red") {
- klik = 2
- }
- if (this.style.color == "grey") {
- klik = 0
- }
- if (this.style.color == "black") {
- klik = 2
- }
- }
- komorka.oncontextmenu = function (rklikanie) {
- klik++
- if (klik == 1) {
- console.log(klik)
- this.style.backgroundColor = "grey" //Funkcja klikania się komórek planszy
- this.style.color = "yellow"
- this.style.backgroundImage = "url('flaga.png')";
- this.style.fontSize = "0px"
- pierwszyklik = true
- licznikbomb++
- }
- if (klik == 2) {
- console.log(klik)
- this.style.backgroundColor = "grey" //Funkcja klikania się komórek planszy
- this.style.color = "red"
- this.style.backgroundImage = "url('pytajnik.png')";
- this.style.fontSize = "0px"
- pierwszyklik = false
- licznikbomb--
- }
- if (klik == 3) {
- console.log(klik)
- this.style.backgroundColor = "grey" //Funkcja klikania się komórek planszy
- this.style.color = "grey"
- this.style.backgroundImage = "url('nic.png')";
- this.style.fontSize = "0px"
- pierwszyklik = false
- klik = 0
- }
- }
- komorka.onmouseleave = function () {
- klik = 0
- }
- komorka.addEventListener('click', (klikanie), { once: false }); //implementacja funkcji klikania sie komorek planszy
- }
- if (n == list2[u]) {
- komorka.style.color = "grey"
- komorka.innerHTML = ""
- u++
- v = 0
- komorka.className = "bomba"
- komorka.onmouseenter = function () {
- if (this.style.color == "yellow") {
- klik = 1
- }
- if (this.style.color == "red") {
- klik = 2
- }
- if (this.style.color == "grey") {
- klik = 0
- }
- if (this.style.color == "black") {
- klik = 2
- }
- }
- komorka.oncontextmenu = function (rklikanie) {
- klik++
- if (klik == 1) {
- console.log(klik)
- this.style.backgroundColor = "grey" //Funkcja klikania się komórek planszy
- this.style.color = "yellow"
- this.style.backgroundImage = "url('flaga.png')";
- this.style.fontSize = "0px"
- pierwszyklik = true
- licznikbomb++
- }
- if (klik == 2) {
- console.log(klik)
- this.style.backgroundColor = "grey" //Funkcja klikania się komórek planszy
- this.style.color = "red"
- this.style.backgroundImage = "url('pytajnik.png')";
- this.style.fontSize = "0px"
- pierwszyklik = false
- licznikbomb--
- }
- if (klik == 3) {
- console.log(klik)
- this.style.backgroundColor = "grey" //Funkcja klikania się komórek planszy
- this.style.color = "grey"
- this.style.backgroundImage = "url('nic.png')";
- this.style.fontSize = "0px"
- pierwszyklik = false
- klik = 0
- }
- v = 0
- }
- komorka.onmouseleave = function () {
- klik = 0
- }
- komorka.addEventListener('click', (klikaniebomba), { once: false }); //implementacja funkcji klikania sie komorek planszy
- }
- }
- }
- list2 = 0
- }
- }
- div.appendChild(button);
- document.body.appendChild(wysdiv);
- document.body.appendChild(szerdiv);
- document.body.appendChild(bombdiv);
- document.body.appendChild(div); //Dodajemy wszystkie stworzsone wcześniej elementy do konkretnych miejsc na stronie w przeciwnym wypadku nie będzie ich nigdzie
- container.appendChild(ilebomb);
- document.body.appendChild(plansza);
- div.appendChild(p);
- wysdiv.appendChild(wys);
- szerdiv.appendChild(szer);
- bombdiv.appendChild(bomb);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement