Advertisement
Spirit13300

Untitled

Sep 18th, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title></title>
  6. </head>
  7. <body>
  8.  
  9. <table border id=damier></table>
  10.  
  11.  
  12. <script
  13.   src="https://code.jquery.com/jquery-3.4.1.min.js"
  14.   integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  15.   crossorigin="anonymous"></script>
  16.  
  17.   <script>
  18.   // Declaration des variable
  19.  
  20.   size = 8;
  21.   nbMines = parseInt(size * size / 10 );
  22.  
  23.  
  24.  
  25.   $(document).ready(function()
  26.     { // Demarage du jeu
  27.  
  28.   for(row = 0; row < size ; row++ )
  29.   {
  30.   $('#damier').append('<tr>');
  31.  
  32.   for(col = 0; col < size ; col++ )
  33.   {
  34.   $('#damier tr:last-child').append('<td><button data-col= '+col + '
  35. data-row=' +row +' id='+ col + '-' + row +'>'); // ajout a la fin du
  36. dernier tr
  37.   }
  38.   }
  39.  
  40.   //tirer au hasard la position des mines
  41.   for (iMine = 0 ; iMine < nbMines; iMine++)
  42.   {
  43.   do
  44.   {
  45.   col = parseInt(Math.random() * size);
  46.   row = parseInt(Math.random() * size);
  47.   }
  48.   while($('#'+col+'-'+row).data('mine') =='true')
  49.   {
  50.   $('#'+col+'-'+row).data('mine','true');
  51.   $('#'+col+'-'+row).html('X');
  52.  
  53.   }
  54.  
  55.  
  56.   }
  57.  
  58.       $('button').on('click',function()
  59.       {
  60.  
  61.         if($(this).data('mine')=='true')
  62.         {
  63.           alert('Vous vous etes fait peter la tete');
  64.           document.location.reload(true);
  65.  
  66.         }
  67.         ouvrir($(this));
  68.  
  69.       })
  70.  
  71.  
  72.  
  73.  
  74.       function nbMinesAutour(col,row)
  75.       {
  76.         var nbMines = 0 ;
  77.         for(iCol = -1; iCol<=1 ;iCol++)
  78.         {
  79.           for(iRow = -1; iRow <= 1 ; iRow++)
  80.           {
  81.             if($('#' + (col+iCol) + '-' + (row + iRow)).data('mine')=='true')
  82.             {
  83.               nbMines++;
  84.             }
  85.  
  86.           }
  87.  
  88.         }
  89.         return nbMines
  90.  
  91.       }
  92.  
  93.  
  94.  
  95.       function ouvrir(boutton)
  96.       {
  97.  
  98.          var col = $(boutton).data('col');
  99.          var row = $(boutton).data('row');
  100.           // Si la case n'est pas deja ouverte
  101.           if($(boutton).data('ouverte')!='true')
  102.           {
  103.  
  104.  
  105.             $(boutton).data('ouverte','true');
  106.               // si il y a des mines autour
  107.             if(nbMinesAutour(col,row))
  108.             {
  109.               //on ecrit le nombre de mines
  110.               $(boutton).html(nbMinesAutour(col,row))
  111.             }
  112.             else
  113.             {
  114.               // sinon
  115.               // on efface la case
  116.               $(boutton).css('visibility','hidden');
  117.               // on ouvre les 4 cases autour
  118.  
  119.               if(col< size) ouvrir($('#'+(col + 1)+'-'+row));
  120.               if(col>0) ouvrir($('#'+(col - 1)+'-'+row));
  121.               if(row>0) ouvrir($('#'+col+'-'+(row-1)));
  122.               if(row<size) ouvrir($('#'+col+'-'+(row+1)));
  123.             }
  124.  
  125.           }
  126.  
  127.               // on écrit le nombre de mines
  128.           //sinon
  129.               // on efface la case
  130.               // on ouvre les 4 cases autour
  131.  
  132.       }
  133.  
  134.     })
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   </script>
  141.   <style>
  142.   button{
  143.   width: 30px;height: 30px;
  144.   }
  145.   </style>
  146. </body>
  147. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement