|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * ¡Disney ha presentado un montón de novedades en su D23! |
| 4 | + * Pero... ¿Dónde está Mickey? |
| 5 | + * Mickey Mouse ha quedado atrapado en un laberinto mágico |
| 6 | + * creado por Maléfica. |
| 7 | + * Desarrolla un programa para ayudarlo a escapar. |
| 8 | + * Requisitos: |
| 9 | + * 1. El laberinto está formado por un cuadrado de 6x6 celdas. |
| 10 | + * 2. Los valores de las celdas serán: |
| 11 | + * - ⬜️ Vacío |
| 12 | + * - ⬛️ Obstáculo |
| 13 | + * - 🐭 Mickey |
| 14 | + * - 🚪 Salida |
| 15 | + * Acciones: |
| 16 | + * 1. Crea una matriz que represente el laberinto (no hace falta |
| 17 | + * que se genere de manera automática). |
| 18 | + * 2. Interactúa con el usuario por consola para preguntarle hacia |
| 19 | + * donde se tiene que desplazar (arriba, abajo, izquierda o derecha). |
| 20 | + * 3. Muestra la actualización del laberinto tras cada desplazamiento. |
| 21 | + * 4. Valida todos los movimientos, teniendo en cuenta los límites |
| 22 | + * del laberinto y los obstáculos. Notifica al usuario. |
| 23 | + * 5. Finaliza el programa cuando Mickey llegue a la salida. |
| 24 | + */ |
| 25 | + |
| 26 | +const readline = require('readline'); |
| 27 | + |
| 28 | +const rl = readline.createInterface({ |
| 29 | + input: process.stdin, |
| 30 | + output: process.stdout, |
| 31 | +}); |
| 32 | + |
| 33 | +const EMPTY = '⬜️'; |
| 34 | +const PLAYER = '🐭'; |
| 35 | +const EXIT = '🚪'; |
| 36 | +const OBSTACLE = '⬛️'; |
| 37 | + |
| 38 | +const maze = [ |
| 39 | + [PLAYER, OBSTACLE, OBSTACLE, OBSTACLE, OBSTACLE, OBSTACLE], |
| 40 | + [EMPTY, OBSTACLE, OBSTACLE, OBSTACLE, EMPTY, OBSTACLE], |
| 41 | + [EMPTY, OBSTACLE, OBSTACLE, OBSTACLE, EMPTY, OBSTACLE], |
| 42 | + [EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY], |
| 43 | + [OBSTACLE, EMPTY, OBSTACLE, EMPTY, OBSTACLE, OBSTACLE], |
| 44 | + [OBSTACLE, EMPTY, OBSTACLE, EMPTY, EMPTY, EXIT], |
| 45 | +]; |
| 46 | + |
| 47 | +const actions = { |
| 48 | + up: 'w', |
| 49 | + down: 's', |
| 50 | + left: 'a', |
| 51 | + right: 'd', |
| 52 | + exit: 'q', |
| 53 | +}; |
| 54 | + |
| 55 | +class Position { |
| 56 | + #maxRow; |
| 57 | + #maxCol; |
| 58 | + |
| 59 | + constructor(row, col, maxRow, maxCol) { |
| 60 | + this.row = row; |
| 61 | + this.col = col; |
| 62 | + this.#maxRow = maxRow; |
| 63 | + this.#maxCol = maxCol; |
| 64 | + } |
| 65 | + |
| 66 | + equals(other) { |
| 67 | + if (other instanceof Position) { |
| 68 | + return this.row === other.row && this.col === other.col; |
| 69 | + } |
| 70 | + |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + hashCode(other) { |
| 75 | + return `${this.row} ${this.col}`.hashCode(); |
| 76 | + } |
| 77 | + |
| 78 | + move(direction) { |
| 79 | + if (direction === actions.up) { |
| 80 | + if (this.row > 0) { |
| 81 | + this.row--; |
| 82 | + } |
| 83 | + } else if (direction === actions.down) { |
| 84 | + if (this.row < this.#maxRow) { |
| 85 | + this.row++; |
| 86 | + } |
| 87 | + } else if (direction === actions.left) { |
| 88 | + if (this.col > 0) { |
| 89 | + this.col--; |
| 90 | + } |
| 91 | + } else if (direction === actions.right) { |
| 92 | + if (this.col < this.#maxCol) { |
| 93 | + this.col++; |
| 94 | + } |
| 95 | + } else { |
| 96 | + console.log(`La dirección '${direction}' no es posible.`); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function getPosition(maze, item) { |
| 102 | + for (const row in maze) { |
| 103 | + for (const col in maze[row]) { |
| 104 | + if (maze[row][col] === item) { |
| 105 | + return new Position( |
| 106 | + parseInt(row), |
| 107 | + parseInt(col), |
| 108 | + maze.length, |
| 109 | + maze[parseInt(row)].length |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function printOptions() { |
| 117 | + console.log('Movimientos posibles:'); |
| 118 | + for (const action in actions) { |
| 119 | + console.log(` [${actions[action]}]: ${action}`); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function printMaze(maze) { |
| 124 | + console.log(); |
| 125 | + for (const row of maze) { |
| 126 | + console.log(row.join('')); |
| 127 | + } |
| 128 | + console.log(); |
| 129 | +} |
| 130 | + |
| 131 | +function updateMaze(maze, newPosition) { |
| 132 | + const oldPlayerPos = getPosition(maze, PLAYER); |
| 133 | + |
| 134 | + maze[oldPlayerPos.row][oldPlayerPos.col] = EMPTY; |
| 135 | + maze[newPosition.row][newPosition.col] = PLAYER; |
| 136 | + |
| 137 | + return maze; |
| 138 | +} |
| 139 | + |
| 140 | +function getAvailableActions(maze, position) { |
| 141 | + const available = [EMPTY, EXIT]; |
| 142 | + const acts = []; |
| 143 | + |
| 144 | + // Up |
| 145 | + if ( |
| 146 | + position.row > 0 && |
| 147 | + available.includes(maze[position.row - 1][position.col]) |
| 148 | + ) { |
| 149 | + acts.push(actions.up); |
| 150 | + } |
| 151 | + |
| 152 | + // Down |
| 153 | + if ( |
| 154 | + position.row < maze.length - 1 && |
| 155 | + available.includes(maze[position.row + 1][position.col]) |
| 156 | + ) { |
| 157 | + acts.push(actions.down); |
| 158 | + } |
| 159 | + |
| 160 | + // Left |
| 161 | + if ( |
| 162 | + position.col > 0 && |
| 163 | + available.includes(maze[position.row][position.col - 1]) |
| 164 | + ) { |
| 165 | + acts.push(actions.left); |
| 166 | + } |
| 167 | + |
| 168 | + // Right |
| 169 | + if ( |
| 170 | + position.col < maze.length - 1 && |
| 171 | + available.includes(maze[position.row][position.col + 1]) |
| 172 | + ) { |
| 173 | + acts.push(actions.right); |
| 174 | + } |
| 175 | + |
| 176 | + return acts; |
| 177 | +} |
| 178 | + |
| 179 | +function run(maze) { |
| 180 | + const exitPosition = getPosition(maze, EXIT); |
| 181 | + const playerPos = getPosition(maze, PLAYER); |
| 182 | + |
| 183 | + printMaze(maze); |
| 184 | + printOptions(); |
| 185 | + rl.question('Selecciona qué hacer:\n > ', (selected) => { |
| 186 | + if (selected.toLowerCase() === actions.exit) { |
| 187 | + rl.close(); |
| 188 | + return; |
| 189 | + } else if (!Object.values(actions).includes(selected.toLowerCase())) { |
| 190 | + console.log( |
| 191 | + '\nDebes introducir una de las siguientes letras:', |
| 192 | + Object.values(actions).join(', ') |
| 193 | + ); |
| 194 | + } else if ( |
| 195 | + !getAvailableActions(maze, playerPos).includes( |
| 196 | + selected.toLowerCase() |
| 197 | + ) |
| 198 | + ) { |
| 199 | + console.log('\nLa acción introducida no es posible.'); |
| 200 | + } else { |
| 201 | + playerPos.move(selected); |
| 202 | + maze = updateMaze(maze, playerPos); |
| 203 | + |
| 204 | + if ( |
| 205 | + playerPos.row === exitPosition.row && |
| 206 | + playerPos.col === exitPosition.col |
| 207 | + ) { |
| 208 | + printMaze(maze); |
| 209 | + console.log('\n\n¡HAS ENCONTRADO LA SALIDA!'); |
| 210 | + rl.close(); |
| 211 | + return; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + run(maze); |
| 216 | + return; |
| 217 | + }); |
| 218 | +} |
| 219 | + |
| 220 | +run(maze); |
0 commit comments