1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| vm = new Vue({ el: '#app', data: { timer: { timekeeper: null, autoMove: null, }, selectedTowerIndex: null, second: 0, steps: 0, minSteps: 0, levels: 3, start: false, autoStart: false, startLevels: 0, towers: [[], [], []], moves: [], title: '提示', content: '完成', }, methods: { selectTower(index, tower) { if (!this.start) { return; } if (this.selectedTowerIndex === null) { if (tower.length > 0) { this.selectedTowerIndex = index; } } else if (this.selectedTowerIndex === index) { this.selectedTowerIndex = null; } else { selectedTower = this.towers[this.selectedTowerIndex]; if (tower.length === 0 || tower[0] > selectedTower[0]) { tower.unshift(selectedTower.shift()); this.moves.push([this.selectedTowerIndex, index]); this.selectedTowerIndex = null; this.steps++; if (tower.length === this.startLevels) { this.start = false; clearInterval(this.timer.timekeeper); this.title = "系统提示"; this.content = "恭喜完成"; this.showAlert() } } else { this.title = "系统提示"; this.content = "小的放在大的上面"; this.showAlert() } } }, startGame() { if (this.start || this.autoStart) { if (confirm("游戏正在进行,确认重新开始?")) { this.restartGame(); } } else { this.restartGame(); } }, restartGame() { let is_start = this.setTower(); if (!is_start) { return; } clearInterval(this.timer.autoMove); clearInterval(this.timer.timekeeper); this.moves = []; this.start = true; this.autoStart = false; var that = this; this.timer.timekeeper = setInterval(function(){ that.second++; }, 1000); }, setTower() { if (this.levels >= 2 && this.levels <= 10) { this.towers = [[], [], []] for (let i = 1; i <= this.levels; i++) { this.towers[0].push(i) } this.selectedTowerIndex = null this.startLevels = this.levels; this.second = 0; this.steps = 0; this.minSteps = Math.pow(2, this.startLevels) - 1; return true; } else { this.title = "系统提示"; this.content = "层数在2-10"; this.showAlert(); return false; } },
hanoi(levels, a_index, b_index, c_index, moves) { if (levels <= 0) { return moves } if (levels === 1) { moves.push([a_index, c_index]); } else { this.hanoi(levels - 1, a_index, c_index, b_index, moves); moves.push([a_index, c_index]); this.hanoi(levels - 1, b_index, a_index, c_index, moves); } return moves }, autoMove() { if (this.start || this.autoStart) { if (confirm("游戏正在进行,确认重新开始?")) { this.handle(); } } else { this.handle(); } }, handle() { let is_start = this.setTower(); if (!is_start) { return; } clearInterval(this.timer.timekeeper); clearInterval(this.timer.autoMove); this.moves = []; this.start = false; this.autoStart = true; let moves = this.hanoi(this.startLevels, 0, 1, 2, []); let that = this; let i = 0; let len = moves.length; this.timer.autoMove = setInterval(function(){ if (i < len) { that.move(moves[i][0], moves[i][1]) that.moves.push(moves[i]); this.steps++; i++; } else { that.autoStart = false; clearInterval(that.timer.autoMove); } }, 1000); }, move(a_index, b_index) { sourceTower = this.towers[a_index]; destTower = this.towers[b_index]; destTower.unshift(sourceTower.shift()); this.scrollToBottom(); this.selectedTowerIndex = null; }, recall() { if (this.start) { let len = this.moves.length; if (len > 0) { this.move(this.moves[len - 1][1], this.moves[len - 1][0]); this.moves.pop(); this.selectedTowerIndex = null; this.steps--; } } }, scrollToBottom() { let step_moves = this.$refs.step_moves; let diff_height = step_moves.scrollHeight - step_moves.clientHeight; if (diff_height > 0) { step_moves.scrollTop = diff_height; } }, showAlert() { this.$refs.dialog.showModal(); }, closeAlert() { this.$refs.dialog.close(); } } })
console.log("1、汉诺塔模块为奇数时,最上面的移动到目标柱;2、汉诺塔模块为偶数时,最上面的移动到过渡柱");
|