|
| 1 | +// Hecho por @Rafacv23 | https://github.com/Rafacv23 | https://twitter.com/rafacanosa | https://www.rafacanosa.dev |
| 2 | + |
| 3 | +// Global constants |
| 4 | +const EVADE_CHANCE: number = 0.2 |
| 5 | +const HEALTH: number = 100 |
| 6 | + |
| 7 | +class Fighter { |
| 8 | + nombre: string |
| 9 | + velocidad: number |
| 10 | + ataque: number |
| 11 | + defensa: number |
| 12 | + salud: number |
| 13 | + |
| 14 | + constructor( |
| 15 | + nombre: string, |
| 16 | + velocidad: number, |
| 17 | + ataque: number, |
| 18 | + defensa: number |
| 19 | + ) { |
| 20 | + this.nombre = nombre |
| 21 | + this.velocidad = this.checkAttribute(velocidad, "velocidad") |
| 22 | + this.ataque = this.checkAttribute(ataque, "ataque") |
| 23 | + this.defensa = this.checkAttribute(defensa, "defensa") |
| 24 | + this.salud = HEALTH // Inicialmente, todos tienen 100 de salud |
| 25 | + } |
| 26 | + |
| 27 | + // ensure that the attribute are between 0 and 100 |
| 28 | + checkAttribute(value: number, attribute: string): number { |
| 29 | + if (value < 0 || value > 100) { |
| 30 | + throw new Error(`El valor de ${attribute} debe ser entre 0 y 100`) |
| 31 | + } |
| 32 | + return value |
| 33 | + } |
| 34 | + |
| 35 | + attack(defender: Fighter) { |
| 36 | + if (defender.salud === 0) { |
| 37 | + console.log(`${defender.nombre} ha perdido toda su salud`) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + // calculamos si el defensor evita el ataque o no |
| 42 | + const evade = Math.random() |
| 43 | + |
| 44 | + if (evade < EVADE_CHANCE) { |
| 45 | + console.log(`${defender.nombre} ha esquivado el ataque`) |
| 46 | + return |
| 47 | + } |
| 48 | + // Calcular el daño |
| 49 | + let damage: number |
| 50 | + |
| 51 | + if (defender.defensa > this.ataque || defender.defensa === this.ataque) { |
| 52 | + damage = this.ataque * 0.1 // Recibe solo el 10% del ataque |
| 53 | + console.log(`${defender.nombre} bloquea gran parte del daño!`) |
| 54 | + } else { |
| 55 | + damage = this.ataque - defender.defensa // Full damage |
| 56 | + } |
| 57 | + |
| 58 | + // Evitar que el daño sea negativo |
| 59 | + if (damage < 0) damage = 0 |
| 60 | + |
| 61 | + // Aplicar el daño al defensor |
| 62 | + defender.salud -= damage |
| 63 | + |
| 64 | + console.log( |
| 65 | + `${this.nombre} ha atacado a ${defender.nombre} con ${damage.toFixed( |
| 66 | + 2 |
| 67 | + )} de daño` |
| 68 | + ) |
| 69 | + console.log(`Salud restante de ${defender.nombre}: ${defender.salud}`) |
| 70 | + } |
| 71 | + |
| 72 | + showInfo() { |
| 73 | + console.log(`Nombre: ${this.nombre}`) |
| 74 | + console.log(`Velocidad: ${this.velocidad}`) |
| 75 | + console.log(`Ataque: ${this.ataque}`) |
| 76 | + console.log(`Defensa: ${this.defensa}`) |
| 77 | + console.log(`Salud: ${this.salud}`) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +const fighters: Fighter[] = [ |
| 82 | + new Fighter("Goku", 85, 95, 80), |
| 83 | + new Fighter("Vegeta", 80, 90, 85), |
| 84 | + new Fighter("Piccolo", 70, 75, 90), |
| 85 | + new Fighter("Gohan", 88, 92, 78), |
| 86 | + new Fighter("Trunks", 78, 88, 82), |
| 87 | + new Fighter("Kefla", 60, 90, 85), |
| 88 | + new Fighter("Freezer", 75, 85, 80), |
| 89 | + new Fighter("Cell", 83, 87, 90), |
| 90 | +] |
| 91 | + |
| 92 | +function shuffleFighters(fighters: Fighter[]): Fighter[] { |
| 93 | + for (let i = fighters.length - 1; i > 0; i--) { |
| 94 | + const j = Math.floor(Math.random() * (i + 1)) |
| 95 | + ;[fighters[i], fighters[j]] = [fighters[j], fighters[i]] |
| 96 | + } |
| 97 | + return fighters |
| 98 | +} |
| 99 | + |
| 100 | +function drawFighters() { |
| 101 | + let round = 1 |
| 102 | + |
| 103 | + while (fighters.length > 1) { |
| 104 | + if (fighters.length === 2) { |
| 105 | + console.log(`\n--- Ronda Final ---`) |
| 106 | + } else { |
| 107 | + console.log(`\n--- Ronda ${round} ---`) |
| 108 | + } |
| 109 | + |
| 110 | + // Shuffle fighters for random matchups |
| 111 | + shuffleFighters(fighters) |
| 112 | + |
| 113 | + // Process battles in pairs |
| 114 | + for (let i = 0; i < fighters.length; i++) { |
| 115 | + const fighter1 = fighters[i] |
| 116 | + const fighter2 = fighters[i + 1] |
| 117 | + |
| 118 | + console.log( |
| 119 | + `\n${fighter1.nombre} (Salud: ${fighter1.salud}) vs ${fighter2.nombre} (Salud: ${fighter2.salud})` |
| 120 | + ) |
| 121 | + |
| 122 | + // Decide who attacks first based on speed |
| 123 | + let attacker: Fighter, defender: Fighter |
| 124 | + if (fighter1.velocidad >= fighter2.velocidad) { |
| 125 | + attacker = fighter1 |
| 126 | + defender = fighter2 |
| 127 | + } else { |
| 128 | + attacker = fighter2 |
| 129 | + defender = fighter1 |
| 130 | + } |
| 131 | + |
| 132 | + // Fight until one of them loses |
| 133 | + while (attacker.salud > 0 && defender.salud > 0) { |
| 134 | + attacker.attack(defender) |
| 135 | + if (defender.salud > 0) { |
| 136 | + defender.attack(attacker) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // Remove the defeated fighter |
| 141 | + if (attacker.salud <= 0) { |
| 142 | + console.log(`${attacker.nombre} ha sido eliminado`) |
| 143 | + console.log(`${defender.nombre} avanza a la siguiente ronda`) |
| 144 | + fighters.splice(fighters.indexOf(attacker), 1) |
| 145 | + } else if (defender.salud <= 0) { |
| 146 | + console.log(`${defender.nombre} ha sido eliminado`) |
| 147 | + console.log(`${attacker.nombre} avanza a la siguiente ronda`) |
| 148 | + fighters.splice(fighters.indexOf(defender), 1) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + round++ |
| 153 | + } |
| 154 | + |
| 155 | + // The last remaining fighter is the winner |
| 156 | + if (fighters.length === 1) { |
| 157 | + console.log("\n¡El torneo ha terminado! El ganador/a es:") |
| 158 | + fighters[0].showInfo() |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// function that sims the tournament |
| 163 | +;(function tournament() { |
| 164 | + if ( |
| 165 | + (fighters.length & (fighters.length - 1)) !== 0 || |
| 166 | + fighters.length === 0 |
| 167 | + ) { |
| 168 | + console.log("El número de luchadores debe ser una potencia de 2") |
| 169 | + return |
| 170 | + } |
| 171 | + drawFighters() |
| 172 | +})() |
0 commit comments