Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Go through the MAZE</title>
  6.     <style>
  7.         body {
  8.             background-color: palegreen;
  9.             display: flex;
  10.             flex-flow: row nowrap;
  11.         }
  12.  
  13.         .controls {
  14.             display: flex;
  15.             flex-direction: column;
  16.             margin-left: 20px;
  17.         }
  18.  
  19.         .result {
  20.             display: inline-block;
  21.             color: red;
  22.             text-transform: uppercase;
  23.             vertical-align: middle;
  24.         }
  25.  
  26.         .result--success {
  27.             color: green;
  28.         }
  29.  
  30.         .start {
  31.             display: inline-block;
  32.             margin-bottom: 20px;
  33.         }
  34.  
  35.         .field {
  36.             position: relative;
  37.             display: flex;
  38.             flex-wrap: wrap;
  39.             justify-content: flex-start;
  40.             background-color: white;
  41.             width: 500px;
  42.             height: 500px;
  43.             float: left;
  44.         }
  45.  
  46.         .field__cell {
  47.             flex-basis: 50px;
  48.             height: 50px;
  49.             box-shadow: inset 0 0 0 1px palegreen;
  50.         }
  51.  
  52.         .field__cell--up {
  53.             box-shadow: 0 -4px 0 0 black, inset 0 0 0 1px palegreen;
  54.         }
  55.  
  56.         .field__cell--up-right {
  57.             box-shadow: 0 -4px 0 0 black, 4px 0 0 0 black, inset 0 0 0 1px palegreen;
  58.         }
  59.  
  60.         .field__cell--up-left {
  61.             box-shadow: 0 -4px 0 0 black, -4px 0 0 black, inset 0 0 0 1px palegreen;
  62.         }
  63.  
  64.         .field__cell--right {
  65.             box-shadow: 4px 0 0 0 black, inset 0 0 0 1px palegreen;
  66.         }
  67.  
  68.         .field__cell--down {
  69.             box-shadow: 0 4px 0 0 black, inset 0 0 0 1px palegreen;
  70.         }
  71.  
  72.         .field__cell--down-right {
  73.             box-shadow: 0 4px 0 0 black, 4px 0 0 0 black, inset 0 0 0 1px palegreen;
  74.         }
  75.  
  76.         .field__cell--down-left {
  77.             box-shadow: 0 4px 0 0 black, -4px 0 0 black, inset 0 0 0 1px palegreen;
  78.         }
  79.  
  80.         .field__cell--left {
  81.             box-shadow: -4px 0 0 black, inset 0 0 0 1px palegreen;
  82.         }
  83.  
  84.         .field__cell--target:after {
  85.             content: "";
  86.             display: block;
  87.             height: 100%;
  88.             width: 100%;
  89.             background-color: darkorange;
  90.             opacity: 0.5;
  91.         }
  92.  
  93.         .piece {
  94.             position: absolute;
  95.             z-index: 1;
  96.             width: 50px;
  97.             height: 50px;
  98.         }
  99.  
  100.         .piece:before, .piece:after {
  101.             content: "";
  102.             display: block;
  103.             position: absolute;
  104.             top: 50%;
  105.             left: 50%;
  106.             margin-left: -15px;
  107.         }
  108.  
  109.         .piece:after {
  110.             background-color: darkorange;
  111.             border-radius: 7px;
  112.             z-index: 1;
  113.             width: 30px;
  114.             height: 30px;
  115.             box-shadow: 0 0 0 3px darkorange;
  116.             margin-top: -15px;
  117.         }
  118.  
  119.         .piece:before {
  120.             background-color: transparent;
  121.             z-index: 2;
  122.             border: solid 15px transparent;
  123.             width: 0;
  124.             height: 0;
  125.             border-bottom: solid 15px white;
  126.             margin-top: -25px;
  127.         }
  128.  
  129.     </style>
  130. </head>
  131. <body>
  132. <div class="field">
  133.     <div class="piece" style="top: 0; left: 0; display: none;"></div>
  134.     <div class="field__cell field__cell--up-left"></div>
  135.     <div class="field__cell field__cell--up"></div>
  136.     <div class="field__cell field__cell--up"></div>
  137.     <div class="field__cell field__cell--up"></div>
  138.     <div class="field__cell field__cell--up"></div>
  139.     <div class="field__cell field__cell--up"></div>
  140.     <div class="field__cell field__cell--up"></div>
  141.     <div class="field__cell field__cell--up"></div>
  142.     <div class="field__cell field__cell--up"></div>
  143.     <div class="field__cell field__cell--up-right"></div>
  144.  
  145.     <div class="field__cell field__cell--up-left"></div>
  146.     <div class="field__cell field__cell--up"></div>
  147.     <div class="field__cell field__cell--up"></div>
  148.     <div class="field__cell field__cell--up"></div>
  149.     <div class="field__cell field__cell--up"></div>
  150.     <div class="field__cell field__cell--up"></div>
  151.     <div class="field__cell field__cell--up"></div>
  152.     <div class="field__cell field__cell--up"></div>
  153.     <div class="field__cell field__cell--up-right"></div>
  154.     <div class="field__cell field__cell--right"></div>
  155.  
  156.     <div class="field__cell field__cell--left"></div>
  157.     <div class="field__cell field__cell--up-left"></div>
  158.     <div class="field__cell field__cell--up"></div>
  159.     <div class="field__cell field__cell--up"></div>
  160.     <div class="field__cell field__cell--up"></div>
  161.     <div class="field__cell field__cell--up"></div>
  162.     <div class="field__cell field__cell--up"></div>
  163.     <div class="field__cell field__cell--up-right"></div>
  164.     <div class="field__cell field__cell--right"></div>
  165.     <div class="field__cell field__cell--right"></div>
  166.  
  167.     <div class="field__cell field__cell--left"></div>
  168.     <div class="field__cell field__cell--left"></div>
  169.     <div class="field__cell field__cell--up-left"></div>
  170.     <div class="field__cell field__cell--up"></div>
  171.     <div class="field__cell field__cell--up"></div>
  172.     <div class="field__cell field__cell--up"></div>
  173.     <div class="field__cell field__cell--up-right"></div>
  174.     <div class="field__cell field__cell--right"></div>
  175.     <div class="field__cell field__cell--right"></div>
  176.     <div class="field__cell field__cell--right"></div>
  177.  
  178.     <div class="field__cell field__cell--left"></div>
  179.     <div class="field__cell field__cell--left"></div>
  180.     <div class="field__cell field__cell--left"></div>
  181.     <div class="field__cell field__cell--up-left"></div>
  182.     <div class="field__cell field__cell--up"></div>
  183.     <div class="field__cell field__cell--up-right"></div>
  184.     <div class="field__cell field__cell--right"></div>
  185.     <div class="field__cell field__cell--right"></div>
  186.     <div class="field__cell field__cell--right"></div>
  187.     <div class="field__cell field__cell--right"></div>
  188.  
  189.     <div class="field__cell field__cell--left"></div>
  190.     <div class="field__cell field__cell--left"></div>
  191.     <div class="field__cell field__cell--left"></div>
  192.     <div class="field__cell field__cell--left"></div>
  193.     <div class="field__cell field__cell--up-left"></div>
  194.     <div class="field__cell field__cell--down-right"></div>
  195.     <div class="field__cell field__cell--right"></div>
  196.     <div class="field__cell field__cell--right"></div>
  197.     <div class="field__cell field__cell--right"></div>
  198.     <div class="field__cell field__cell--right"></div>
  199.  
  200.     <div class="field__cell field__cell--left"></div>
  201.     <div class="field__cell field__cell--left"></div>
  202.     <div class="field__cell field__cell--left"></div>
  203.     <div class="field__cell field__cell--left"></div>
  204.     <div class="field__cell field__cell--down-left"></div>
  205.     <div class="field__cell field__cell--down"></div>
  206.     <div class="field__cell field__cell--down-right"></div>
  207.     <div class="field__cell field__cell--right"></div>
  208.     <div class="field__cell field__cell--right"></div>
  209.     <div class="field__cell field__cell--right"></div>
  210.  
  211.     <div class="field__cell field__cell--left"></div>
  212.     <div class="field__cell field__cell--left"></div>
  213.     <div class="field__cell field__cell--left"></div>
  214.     <div class="field__cell field__cell--down-left"></div>
  215.     <div class="field__cell field__cell--down"></div>
  216.     <div class="field__cell field__cell--down"></div>
  217.     <div class="field__cell field__cell--down"></div>
  218.     <div class="field__cell field__cell--down-right"></div>
  219.     <div class="field__cell field__cell--right"></div>
  220.     <div class="field__cell field__cell--right"></div>
  221.  
  222.     <div class="field__cell field__cell--left"></div>
  223.     <div class="field__cell field__cell--left"></div>
  224.     <div class="field__cell field__cell--down-left"></div>
  225.     <div class="field__cell field__cell--down"></div>
  226.     <div class="field__cell field__cell--down"></div>
  227.     <div class="field__cell field__cell--down"></div>
  228.     <div class="field__cell field__cell--down"></div>
  229.     <div class="field__cell field__cell--down"></div>
  230.     <div class="field__cell field__cell--down-right"></div>
  231.     <div class="field__cell field__cell--right"></div>
  232.  
  233.     <div class="field__cell field__cell--left field__cell--target"></div>
  234.     <div class="field__cell field__cell--down-left"></div>
  235.     <div class="field__cell field__cell--down"></div>
  236.     <div class="field__cell field__cell--down"></div>
  237.     <div class="field__cell field__cell--down"></div>
  238.     <div class="field__cell field__cell--down"></div>
  239.     <div class="field__cell field__cell--down"></div>
  240.     <div class="field__cell field__cell--down"></div>
  241.     <div class="field__cell field__cell--down"></div>
  242.     <div class="field__cell field__cell--down-right"></div>
  243. </div>
  244.  
  245. <div class="controls">
  246.     <div>
  247.         <button class="start">Start</button>
  248.     </div>
  249.     <div class="result"></div>
  250. </div>
  251.  
  252. <script>
  253.     const successResult = 'success!';
  254.     const failResult = 'fail!';
  255.     const successModifier = 'result--success';
  256.     const start = document.querySelector('.start');
  257.     const result = document.querySelector('.result');
  258.     const fieldElement = document.querySelector('.field');
  259.     const cellsElements = document.querySelectorAll('.field__cell');
  260.     const pieceElement = document.querySelector('.piece');
  261.     const directionNames = ['up', 'right', 'down', 'left'];
  262.     let stepper;
  263.  
  264.     class Field {
  265.         constructor() {
  266.             this.field = fieldElement;
  267.             this.cells = cellsElements;
  268.             this.opposites = {
  269.                 'up': 'down',
  270.                 'down': 'up',
  271.                 'left': 'right',
  272.                 'right': 'left'
  273.             }
  274.         }
  275.  
  276.         onTheSameRow(current, next) {
  277.             return (current - current % 10) / 10 === (next - next % 10) / 10
  278.         }
  279.  
  280.         findNextCell(currentIndex, direction) {
  281.             let nextIndex = currentIndex;
  282.  
  283.             switch (direction) {
  284.                 case 'up':
  285.                     nextIndex = currentIndex - 10;
  286.                     if (nextIndex < 0) {
  287.                         nextIndex = -1;
  288.                     }
  289.                     break;
  290.  
  291.                 case 'right':
  292.                     nextIndex = currentIndex + 1;
  293.                     if (!this.onTheSameRow(currentIndex, nextIndex)) {
  294.                         nextIndex = -1;
  295.                     }
  296.                     break;
  297.  
  298.                 case 'down':
  299.                     nextIndex = currentIndex + 10;
  300.                     if (nextIndex > 100) {
  301.                         nextIndex = -1;
  302.                     }
  303.                     break;
  304.  
  305.                 case 'left':
  306.                     nextIndex = currentIndex - 1;
  307.                     if (!this.onTheSameRow(currentIndex, nextIndex)) {
  308.                         nextIndex = -1;
  309.                     }
  310.                     break;
  311.  
  312.                 default:
  313.                     nextIndex = -1;
  314.                     break;
  315.             }
  316.  
  317.             return this.cells[nextIndex];
  318.         }
  319.  
  320.         isTheWallThere(position, direction) {
  321.             let cellIndex = position.top * 10 + position.left;
  322.             let cell = this.cells[cellIndex];
  323.             let nextCell = this.findNextCell(cellIndex, direction);
  324.  
  325.             return (
  326.                 (cell.classList.toString().indexOf(direction) !== -1) ||
  327.                 (typeof nextCell === 'undefined') ||
  328.                 (nextCell.classList.toString().indexOf(this.opposites[direction]) !== -1)
  329.             );
  330.         }
  331.     }
  332.  
  333.     function createPiece () {
  334.  
  335.         let field = new Field();
  336.  
  337.         let rotatePiece = direction => {
  338.             let dir = direction;
  339.             moves.unshift(function () {
  340.                 pieceElement.style.transform = 'rotate(' + dir * 90 + 'deg)';
  341.             })
  342.         };
  343.  
  344.         let movePiece = position => {
  345.             let top = (position.top) * 50;
  346.             let left = (position.left) * 50;
  347.  
  348.             moves.unshift(function () {
  349.                 pieceElement.style.top = top + 'px';
  350.                 pieceElement.style.left = left + 'px';
  351.             })
  352.         };
  353.  
  354.         let isPieceOut = () => {
  355.             return pieceElement.style.top === '450px' && pieceElement.style.left === '0px';
  356.         };
  357.  
  358.         let direction = Math.floor(Math.random() * directionNames.length);
  359.         let stepsCount = 0;
  360.         let turnsCount = 0;
  361.         let moves = [];
  362.         let position = {
  363.             left: Math.floor(Math.random() * 10),
  364.             top: Math.floor(Math.random() * 10)
  365.         };
  366.  
  367.         let piecePublicApi = {
  368.             isThereWay() {
  369.                 const isTheWallThere = field.isTheWallThere(position, directionNames[direction]);
  370.                 return !isTheWallThere
  371.             },
  372.  
  373.             turnLeft() {
  374.                 turnsCount += 1;
  375.                 direction -= 1;
  376.                 direction = direction < 0 ? 3 : direction;
  377.                 rotatePiece(direction);
  378.             },
  379.  
  380.             turnRight() {
  381.                 turnsCount += 1;
  382.                 direction += 1;
  383.                 direction = direction > 3 ? 0 : direction;
  384.                 rotatePiece(direction);
  385.             },
  386.  
  387.             goForward() {
  388.                 stepsCount += 1;
  389.                 if (!this.isThereWay()) {
  390.                     return false;
  391.                 }
  392.  
  393.                 switch (direction) {
  394.                     case 0:
  395.                         position.top -= 1;
  396.                         break;
  397.                     case 1:
  398.                         position.left += 1;
  399.                         break;
  400.                     case 2:
  401.                         position.top += 1;
  402.                         break;
  403.                     case 3:
  404.                         position.left -= 1;
  405.                         break;
  406.                     default:
  407.                         return false
  408.                 }
  409.  
  410.                 movePiece(position);
  411.             },
  412.             amIFree() {
  413.                 return isPieceOut();
  414.             }
  415.         };
  416.  
  417.         stepper = setInterval(() => {
  418.  
  419.             let nextMove = moves.pop();
  420.  
  421.             if (typeof nextMove === 'function') {
  422.                 nextMove();
  423.             } else {
  424.                 let success = isPieceOut();
  425.  
  426.                 if (success) {
  427.                     result.textContent = successResult;
  428.                     result.classList.add(successModifier);
  429.                 } else {
  430.                     result.textContent = failResult;
  431.                     result.classList.remove(successModifier);
  432.                 }
  433.  
  434.                 clearInterval(stepper);
  435.                 console.log({stepsCount, turnsCount});
  436.             }
  437.         }, 200);
  438.  
  439.         pieceElement.style.top = position.top * 50 + 'px';
  440.         pieceElement.style.left = position.left * 50 + 'px';
  441.         pieceElement.style.transform = 'rotate(' + direction * 90 + 'deg)';
  442.         pieceElement.style.display = 'block';
  443.  
  444.         return piecePublicApi;
  445.     }
  446.  
  447.     function resetField() {
  448.         clearInterval(stepper);
  449.         result.textContent = '';
  450.         result.classList.remove(successModifier);
  451.     }
  452.  
  453.     function escapePlan() {
  454.         const piece = createPiece();
  455.         // piece.isThereWay();
  456.         // piece.goForward();
  457.         // piece.turnRight();
  458.         // piece.turnLeft();
  459.         // piece.amIFree();
  460.     }
  461.  
  462.     function main() {
  463.         resetField();
  464.         escapePlan();
  465.     }
  466.  
  467.     start.addEventListener('click', main);
  468.  
  469. </script>
  470. </body>
  471. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement