|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * ¡Deadpool y Wolverine se enfrentan en una batalla épica! |
| 4 | + * Crea un programa que simule la pelea y determine un ganador. |
| 5 | + * El programa simula un combate por turnos, donde cada protagonista posee unos |
| 6 | + * puntos de vida iniciales, un daño de ataque variable y diferentes cualidades |
| 7 | + * de regeneración y evasión de ataques. |
| 8 | + * Requisitos: |
| 9 | + * 1. El usuario debe determinar la vida inicial de cada protagonista. |
| 10 | + * 2. Cada personaje puede impartir un daño aleatorio: |
| 11 | + * - Deadpool: Entre 10 y 100. |
| 12 | + * - Wolverine: Entre 10 y 120. |
| 13 | + * 3. Si el daño es el máximo, el personaje que lo recibe no ataca en el |
| 14 | + * siguiente turno, ya que tiene que regenerarse (pero no aumenta vida). |
| 15 | + * 4. Cada personaje puede evitar el ataque contrario: |
| 16 | + * - Deadpool: 25% de posibilidades. |
| 17 | + * - Wolverine: 20% de posibilidades. |
| 18 | + * 5. Un personaje pierde si sus puntos de vida llegan a cero o menos. |
| 19 | + * Acciones: |
| 20 | + * 1. Simula una batalla. |
| 21 | + * 2. Muestra el número del turno (pausa de 1 segundo entre turnos). |
| 22 | + * 3. Muestra qué pasa en cada turno. |
| 23 | + * 4. Muestra la vida en cada turno. |
| 24 | + * 5. Muestra el resultado final. |
| 25 | + */ |
| 26 | + |
| 27 | +const readline = require('readline'); |
| 28 | + |
| 29 | +const rl = readline.createInterface({ |
| 30 | + input: process.stdin, |
| 31 | + output: process.stdout, |
| 32 | +}); |
| 33 | + |
| 34 | +const random = (min, max) => { |
| 35 | + max = max | 0; |
| 36 | + [min, max] = [Math.min(min, max), Math.max(min, max)]; |
| 37 | + return Math.floor(Math.random() * (max - min) + min); |
| 38 | +}; |
| 39 | + |
| 40 | +class Damage { |
| 41 | + #min; |
| 42 | + #max; |
| 43 | + |
| 44 | + constructor(min, max) { |
| 45 | + this.#min = min; |
| 46 | + this.#max = max; |
| 47 | + } |
| 48 | + |
| 49 | + calculate() { |
| 50 | + return random(this.min, this.max); |
| 51 | + } |
| 52 | + |
| 53 | + get min() { |
| 54 | + return this.#min; |
| 55 | + } |
| 56 | + |
| 57 | + get max() { |
| 58 | + return this.#max; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class Fighter { |
| 63 | + #name; |
| 64 | + #damage; |
| 65 | + #evade; |
| 66 | + #life; |
| 67 | + #rests; |
| 68 | + |
| 69 | + constructor(name, damage, evade, life, rests) { |
| 70 | + this.#name = name; |
| 71 | + this.#damage = damage; |
| 72 | + this.#evade = evade; |
| 73 | + this.#life = life; |
| 74 | + this.#rests = rests; |
| 75 | + } |
| 76 | + |
| 77 | + attack() { |
| 78 | + if (this.#rests) { |
| 79 | + console.log(`${this.#name} se está recuperando...`); |
| 80 | + this.#rests = false; |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + // Calculate the damage value |
| 85 | + const damage = this.#damage.calculate(); |
| 86 | + // Check if fighter will need to rest in his next turn |
| 87 | + this.#rests = damage === this.#damage.max; |
| 88 | + if (this.#rests) { |
| 89 | + console.log( |
| 90 | + `Golpe crítico! ${ |
| 91 | + this.#name |
| 92 | + } deberá descansar en el siguiente turno...` |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + return damage; |
| 97 | + } |
| 98 | + |
| 99 | + evade() { |
| 100 | + return Math.random() < this.#evade; |
| 101 | + } |
| 102 | + |
| 103 | + get name() { |
| 104 | + return this.#name; |
| 105 | + } |
| 106 | + |
| 107 | + get life() { |
| 108 | + return this.#life; |
| 109 | + } |
| 110 | + |
| 111 | + set life(life) { |
| 112 | + this.#life = life; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +const D = 'Deadpool'; |
| 117 | +const W = 'Wolverine'; |
| 118 | + |
| 119 | +function isWinner(fighters) { |
| 120 | + return fighters.some((fighter) => fighter.life <= 0); |
| 121 | +} |
| 122 | + |
| 123 | +function showLife(fighters) { |
| 124 | + for (const fighter of fighters) { |
| 125 | + console.log(` - Vida de ${fighter.name}:`, fighter.life); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function simulate(dLife, wLife) { |
| 130 | + const fighters = [ |
| 131 | + new Fighter(D, new Damage(10, 100), 0.25, dLife), |
| 132 | + new Fighter(W, new Damage(10, 120), 0.2, wLife), |
| 133 | + ]; |
| 134 | + |
| 135 | + console.log('\nVidas iniciales:'); |
| 136 | + showLife(fighters); |
| 137 | + |
| 138 | + // Select who starts the fight |
| 139 | + const firstFighter = Math.random() < 0.5 ? fighters[0] : fighters[1]; |
| 140 | + const secondFighter = |
| 141 | + fighters[(fighters.indexOf(firstFighter) + 1) % fighters.length]; |
| 142 | + |
| 143 | + console.log(`\n¡${firstFighter.name.toUpperCase()} COMIENZA EL COMBATE!`); |
| 144 | + |
| 145 | + let turnCounter = 1; |
| 146 | + function battle(currentFighter, otherFighter) { |
| 147 | + function fight(currentFighter, otherFighter) { |
| 148 | + const damage = currentFighter.attack(); |
| 149 | + if (damage > 0) { |
| 150 | + if (otherFighter.evade()) { |
| 151 | + console.log( |
| 152 | + `${otherFighter.name} ha esquivado el ataque!` |
| 153 | + ); |
| 154 | + } else { |
| 155 | + console.log( |
| 156 | + `${otherFighter.name} ha recibido ${damage} puntos de año!` |
| 157 | + ); |
| 158 | + otherFighter.life -= damage; |
| 159 | + if (otherFighter.life < 0) { |
| 160 | + otherFighter.life = 0; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + showLife(fighters); |
| 166 | + |
| 167 | + if (isWinner(fighters)) { |
| 168 | + console.log( |
| 169 | + `\n${currentFighter.name.toUpperCase()} HA GANADO EL COMBATE!` |
| 170 | + ); |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + console.log(`\nTURNO ${turnCounter}:`); |
| 178 | + |
| 179 | + // Set the otherFighter |
| 180 | + otherFighter = |
| 181 | + fighters[(fighters.indexOf(currentFighter) + 1) % fighters.length]; |
| 182 | + |
| 183 | + // Fight |
| 184 | + let winner = fight(firstFighter, secondFighter); |
| 185 | + if (winner) { |
| 186 | + rl.close(); |
| 187 | + return; |
| 188 | + } |
| 189 | + winner = fight(secondFighter, firstFighter); |
| 190 | + if (winner) { |
| 191 | + rl.close(); |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + turnCounter++; |
| 196 | + setTimeout(() => battle(firstFighter, secondFighter), 1000); |
| 197 | + } |
| 198 | + |
| 199 | + battle(firstFighter, secondFighter); |
| 200 | +} |
| 201 | + |
| 202 | +rl.question(`Ingresa la vida inicial de ${D}: `, (dLife) => { |
| 203 | + rl.question(`Ingresa la vida inicial de ${W}: `, (wLife) => { |
| 204 | + simulate(parseInt(dLife), parseInt(wLife)); |
| 205 | + }); |
| 206 | +}); |
0 commit comments