Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.33 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
  5.     <meta charset="UTF-8">
  6.     <title>Title</title>
  7. </head>
  8. <body>
  9.  
  10. <div class="container"></div>
  11. </body>
  12. </html>
  13.  
  14.  
  15. <script type="text/javascript">
  16.     var container = $('.container');
  17.  
  18.     $(document).ready(function() {
  19.         $('.cell').click(function () {
  20.             var currentCell = $(this);
  21.  
  22.             if (checkMove(currentCell)) {
  23.                 swapCells(currentCell);
  24.             }
  25.  
  26.             console.log(checkMove(currentCell));
  27.             console.log($(this).attr('id'));
  28.         });
  29.     });
  30.  
  31.     function getEmptyCell() {
  32.         return $('#cell-16');
  33.     }
  34.    
  35.     function swapCells(el) {
  36.         var emptyCell = getEmptyCell();
  37.         var cloneEmptyCell = $(emptyCell).clone();
  38.        
  39.         el.after(cloneEmptyCell);
  40.        
  41.         emptyCell.replaceWith(el);
  42.  
  43.     }
  44.  
  45.     function checkMove(el) {
  46.         var emptyCell = getEmptyCell()[0];
  47.  
  48.         if (el.prev()[0] === emptyCell) {
  49.             return true;
  50.         }
  51.  
  52.         if (el.next()[0] === emptyCell) {
  53.             return true
  54.         }
  55.  
  56.         if (el.prevAll().eq(3)[0] === emptyCell) {
  57.             return true;
  58.         }
  59.  
  60.         if (el.nextAll().eq(3)[0] === emptyCell) {
  61.             return true;
  62.         }
  63.  
  64.         return false;
  65.     }
  66.  
  67.  
  68.     function render() {
  69.         for (var i = 1; i <= 16; i++) {
  70.            var cell = $('<div></div>')
  71.                 .addClass('cell')
  72.                 .attr('id', 'cell-' + i)
  73.                 .html(i)
  74.             ;
  75.  
  76.             container.append(cell);
  77.         }
  78.     }
  79.  
  80.     render();
  81. </script>
  82.  
  83. <style type="text/css">
  84.     body {
  85.         margin: 0;
  86.         padding: 0;
  87.         line-height: 0;
  88.     }
  89.  
  90.     .container {
  91.         width: 1032px;
  92.         height: 1032px;
  93.         border: 1px solid #0a0a0a;
  94.         text-align: center;
  95.         margin: 0 auto;
  96.     }
  97.         .container .cell {
  98.             font-size: 120px;
  99.             font-weight: bold;
  100.             border: 1px solid #0a0a0a;
  101.             width: 256px;
  102.             height: 256px;
  103.             display: inline-block;
  104.             margin: 0;
  105.             padding: 0;
  106.             line-height: 242px;
  107.         }
  108.  
  109.         .container #cell-16 {
  110.             color: white;
  111.         }
  112. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement