Advertisement
tomuwhu

Kirakós motorja

Jan 7th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.   <div>
  3.     <table>
  4.       <tr v-for="(row,i) in x" :key="i">
  5.         <td
  6.           v-for="(cell,j) in row"
  7.           :key="j"
  8.           @click="katt(i,j)"
  9.           :class="cell?'x':'y'"
  10.         >{{cell?cell:''}}</td>
  11.       </tr>
  12.     </table>
  13.   </div>
  14. </template>
  15.  
  16. <script>
  17. var n = 5,
  18.   np = {
  19.     x: 0,
  20.     y: 0
  21.   };
  22. export default {
  23.   data() {
  24.     return {
  25.       x: Array(n)
  26.         .fill(0)
  27.         .map((v, i) =>
  28.           Array(n)
  29.             .fill(0)
  30.             .map((u, j) => n * i + j)
  31.         ),
  32.       test: 0
  33.     };
  34.   },
  35.   methods: {
  36.     katt(i, j) {
  37.       if (Math.abs(i - np.y) + Math.abs(j - np.x) === 1) {
  38.         this.x[np.y][np.x] = this.x[i][j];
  39.         this.x[i][j] = 0;
  40.         this.$forceUpdate();
  41.         np.y = i;
  42.         np.x = j;
  43.       }
  44.     },
  45.     kever() {
  46.       var i;
  47.       for (i = 1; i < 10 * n ** 2; i++) {
  48.         var ir = Math.round(Math.random() * 4);
  49.         if (ir === 0) {
  50.           if (this.x[np.y][np.x + 1]) {
  51.             let csv = this.x[np.y][np.x + 1];
  52.             this.x[np.y][np.x + 1] = this.x[np.y][np.x];
  53.             this.x[np.y][np.x] = csv;
  54.             np.x++;
  55.           }
  56.         }
  57.         if (ir === 1) {
  58.           if (this.x[np.y][np.x - 1]) {
  59.             let csv = this.x[np.y][np.x - 1];
  60.             this.x[np.y][np.x - 1] = this.x[np.y][np.x];
  61.             this.x[np.y][np.x] = csv;
  62.             np.x--;
  63.           }
  64.         }
  65.         if (ir === 2) {
  66.           if (this.x[np.y + 1]) {
  67.             let csv = this.x[np.y + 1][np.x];
  68.             this.x[np.y + 1][np.x] = this.x[np.y][np.x];
  69.             this.x[np.y][np.x] = csv;
  70.             np.y++;
  71.           }
  72.         }
  73.         if (ir === 3) {
  74.           if (this.x[np.y - 1]) {
  75.             let csv = this.x[np.y - 1][np.x];
  76.             this.x[np.y - 1][np.x] = this.x[np.y][np.x];
  77.             this.x[np.y][np.x] = csv;
  78.             np.y--;
  79.           }
  80.         }
  81.       }
  82.       this.$forceUpdate();
  83.     }
  84.   },
  85.   mounted() {
  86.     this.kever();
  87.   }
  88. };
  89. </script>
  90. <style>
  91. td {
  92.   font-size: 26px;
  93.   width: 40px;
  94.   height: 40px;
  95.   text-align: center;
  96.   user-select: none;
  97. }
  98. td.x {
  99.   color: rgb(243, 245, 218);
  100.   text-shadow: 0px 0px 5px rgb(0, 0, 0);
  101.   background-color: rgb(101, 148, 148);
  102.   cursor: pointer;
  103.   border-radius: 3px;
  104.   box-shadow: 1px 1px 2px black;
  105. }
  106. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement