Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. setInterval(function () { time() }, 1000); // co sekundę wywołaj funkcje time
  2. var sec = 0;
  3.  
  4. function time() {
  5.     if (sec > -1) {
  6.         $('#time').html(sec++);
  7.     }
  8. }
  9.  
  10.  
  11. var board = new Array(5)
  12. for (var i = 0; i < board.length; i++) {
  13.     board[i] = new Array(5);
  14.     for (var j = 0; j < board[i].length; j++) {
  15.         board[i][j] = 0;
  16.     }
  17. }
  18.  
  19. randomMines();
  20. countNeighbors();
  21.  
  22. // Funkcje
  23. function randomMines() {
  24.     var tmp = 0;
  25.     while (tmp < 5) {
  26.         var r = Math.floor((Math.random() * 5));
  27.         var c = Math.floor((Math.random() * 5));
  28.         if (board[r][c] != -1) {
  29.             board[r][c] = -1;
  30.             tmp++;
  31.         }
  32.     }
  33. }
  34.  
  35. function countNeighbors() {
  36.     for (var i = 0; i < board.length; i++) {
  37.         for (var j = 0; j < board[i].length; j++) {
  38.             if (board[i][j] != -1) {
  39.                 board[i][j] = countN(i, j);
  40.             }
  41.         }
  42.     }
  43. }
  44.  
  45. function countN(r, c) {
  46.     var count = 0;
  47.  
  48.     for (var i = r - 1; i <= r + 1; i++) {
  49.         for (var j = c - 1; j <= c + 1; j++) {
  50.             if (i > -1 && i < board.length && j > -1 && j < board[i].length) {
  51.                 if (board[i][j] == -1) {
  52.                     count++;
  53.                 }
  54.             }
  55.         }
  56.     }
  57.     return count;
  58. }
  59.  
  60. function cellClick(obj) {
  61.     let clicks = 0;////
  62.     var id = obj.id;
  63.     var idR = id.substring(0, id.indexOf('_'));
  64.     var idC = id.substring(id.indexOf('_') + 1, id.length);
  65.  
  66.     $('#' + id).removeClass();
  67.     if (board[idR - 1][idC - 1] == -1) {
  68.         $('#' + id).addClass('mine');
  69.  
  70.     } else if (board[idR - 1][idC - 1] == 0) {
  71.         $('#' + id).addClass('emptyN');
  72.         clicks++;//////////
  73.     } else {
  74.         $('#' + id).addClass('empty');
  75.         clicks++;/////////////
  76.     }
  77.  
  78.     if (clicks == 20) {
  79.         alert("Kim jesteś? Jesteś zwycięzcą!");
  80.         //Jak będzie clicks równe 20 czyli ilości pól (kliknięych) bez bomb to wtedy pokaże zwycięstwo (alert)
  81.     }
  82.  
  83.     $('#' + id).html(board[idR - 1][idC - 1]);
  84.  
  85.     if (board[idR - 1][idC - 1] != -1) {
  86.         let bombTmp = 0;
  87.         for (i = 0; i < board.length; i++) {
  88.             for (j = 0; j < board[i].length; j++) {
  89.                 if (board[i][j] == -1 && $('#' + (i + 1) + '_' + (j + 1)).hasClass('mine')) {
  90.  
  91.                     bombTmp++;
  92.  
  93.                 }
  94.             }
  95.         }
  96.         if (bombTmp > 0) {
  97.             $('#time').html(sec);
  98.             sec = -1;
  99.         }
  100.     }
  101.  
  102.  
  103.     if (board[idR - 1][idC - 1] == -1) {
  104.         alert('Koniec gry');
  105.         $('#time').html(sec);
  106.         sec = -1;
  107.         for (i = 0; i < board.length; i++) {
  108.             for (j = 0; j < board[i].length; j++) {
  109.                 if (board[i][j] == -1) {
  110.                     $('#' + (i + 1) + '_' + (j + 1)).html('X');
  111.                 }
  112.             }
  113.         }
  114.         $('td').removeAttr('onclick');
  115.         $('td').removeAttr('ondblclick');
  116.  
  117.     }
  118.  
  119.     if ((board * board) - clicks == 5) {
  120.         alert('Koniec gry');
  121.         $('#time').html(sec);
  122.         sec = -1;
  123.         $('td').removeAttr('onclick');
  124.         $('td').removeAttr('ondblclick');
  125.     }
  126.  
  127. }
  128.  
  129.  
  130.  
  131. function mine(obj) {
  132.     var id = obj.id;
  133.     var idR = id.substring(0, id.indexOf('_'));
  134.     var idC = id.substring(id.indexOf('_') + 1, id.length);
  135.     $('#' + id).html('x');
  136.  
  137.     $('#' + id).removeClass();
  138.     $('#' + id).addClass('black');
  139.  
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement