Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. ```<template>
  2. <div class="box">
  3. <ul class="puzzle-wrap">
  4. <li
  5. :class="{'puzzle': true, 'puzzle-empty': !puzzle}"
  6. v-for="(puzzle, index) in puzzles"
  7. :key="index"
  8. v-text="puzzle"
  9. @click="moveFn(index)"
  10. ></li>
  11. </ul>
  12. <button class="btn-reset" @click="renderFn">重置游戏</button>
  13. </div>
  14. </template>
  15.  
  16. <script>
  17. export default {
  18. data () {
  19. return {
  20. puzzles: []
  21. }
  22. },
  23. mounted() {
  24. this.renderFn()
  25. },
  26. methods: {
  27. // 重置渲染
  28. renderFn() {
  29. let puzzleArr = Array.from({ length: 15 }, (value, index) => index + 1);
  30.  
  31. // 页面显示
  32. this.puzzles = this.shuffle(puzzleArr)
  33. this.puzzles.push('')
  34. },
  35. // 随机打乱数组
  36. shuffle(arr) {
  37. let len = arr.length
  38.  
  39. for (let i = 0; i < len - 1; i++) {
  40. let idx = Math.floor(Math.random() * (len - i))
  41. let temp = arr[idx]
  42. arr[idx] = arr[len - i - 1]
  43. arr[len - i - 1] = temp
  44. }
  45.  
  46. return arr
  47. },
  48. // 点击方块
  49. moveFn(index) {
  50. let puzzles = this.puzzles
  51. // 获取点击位置上下左右的值
  52. let leftNum = this.puzzles[index - 1],
  53. rightNum = this.puzzles[index + 1],
  54. topNum = this.puzzles[index - 4],
  55. bottomNum = this.puzzles[index + 4]
  56. // 和为空的位置交换数值
  57. if (leftNum === '' && index % 4) {
  58. this.setPuzzle(index, -1)
  59. } else if (rightNum === '' && 3 !== index % 4) {
  60. this.setPuzzle(index, 1)
  61. } else if (topNum === '') {
  62. this.setPuzzle(index, -4)
  63. } else if (bottomNum === '') {
  64. this.setPuzzle(index, 4)
  65. }
  66. this.passFn()
  67. },
  68. // 设置数组值
  69. setPuzzle(index, num) {
  70. let curNum = this.puzzles[index]
  71. this.puzzles[index + num] = curNum
  72. this.puzzles[index] = ''
  73. this.$forceUpdate()
  74. // this.$set(this.puzzles, index + num, curNum)
  75. // this.$set(this.puzzles, index, '')
  76. },
  77. // 校验是否过关
  78. passFn() {
  79. if (this.puzzles[15] === '') {
  80. let newPuzzles = this.puzzles.slice(0, 15)
  81. let isPass = newPuzzles.every((e, i) => e === i + 1)
  82. if (isPass) {
  83. alert ('恭喜,闯关成功!')
  84. }
  85. }
  86. }
  87. },
  88. }
  89. </script>
  90.  
  91. <style lang="less" scoped>
  92. .puzzle-wrap {
  93. width: 328px;
  94. height: 328px;
  95. padding: 0;
  96. margin: 50px auto 0;
  97. background: #ccc;
  98. list-style: none;
  99. }
  100. .puzzle {
  101. float: left;
  102. width: 80px;
  103. height: 80px;
  104. font-size: 20px;
  105. background: #f90;
  106. text-align: center;
  107. line-height: 80px;
  108. border: 1px solid #ccc;
  109. cursor: pointer;
  110. }
  111. .puzzle-empty {
  112. background: #ccc;
  113. box-shadow: inset 2px 2px 18px;
  114. }
  115. .btn-reset {
  116. display: block;
  117. width: 200px;
  118. height: 40px;
  119. line-height: 40px;
  120. margin: 20px auto 0;
  121. text-align: center;
  122. border-radius: 5px;
  123. border: none;
  124. background: #ccc;
  125. }
  126. </style>
  127. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement