Created
February 23, 2023 20:59
-
-
Save moiseslodeiro/6b7bcd6d4b2cc57e080969cd9e55d076 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pokemon { | |
constructor (name, level, type) { | |
this.hp = 200 | |
this.name = name | |
this.type = type | |
this.level = level | |
this.attack = 0 | |
this.defense = 0 | |
} | |
attack (yourType, opponentType, attack, defense) { | |
const types = { | |
fire: { fire: 0.5, grass: 2, water: 0.5, electric: 1 }, | |
grass: { fire: 0.5, grass: 0.5, water: 2, electric: 1 }, | |
water: { fire: 2, grass: 0.5, water: 0.5, electric: 0.5 }, | |
electric: { fire: 1, grass: 1, water: 2, electric: 0.5 } | |
} | |
return 50 * (attack / defense) * types[yourType][opponentType] | |
} | |
} | |
class WaterPokemon extends Pokemon { | |
constructor (name, level, attack, defense) { | |
super(name, level, 'water') | |
this.attack = attack | |
this.defense = defense | |
} | |
regularAttack (oponent) { | |
return super.attack(this.type, oponent.type, this.attack, oponent.defense) + this.level | |
} | |
bubbleAttack () { | |
return 40 | |
} | |
waterfallAttack () { | |
return 80 | |
} | |
specialAttack () { | |
const randomAttack = Math.floor(Math.random() * 2) | |
const ATTACKS = { | |
0: this.bubbleAttack(), | |
1: this.waterfallAttack() | |
} | |
return ATTACKS[randomAttack] | |
} | |
toString () { | |
return `${this.name} (tipo ${this.type}, vida ${this.hp}, nivel ${this.level}, ataque ${this.attack}, defensa ${this.defense})` | |
} | |
} | |
class Inventory { | |
constructor () { | |
this.inventory = [] | |
} | |
add (item) { | |
this.inventory.push(item) | |
} | |
get () { | |
return this.inventory | |
} | |
} | |
class HealItem { | |
constructor (healPoints) { | |
this.recover = healPoints | |
} | |
toString () { | |
return `Item de recuperación de ${this.recover} HP` | |
} | |
} | |
class Trainer { | |
constructor (name, region, leader = false, type = undefined) { | |
this.name = name | |
this.region = region | |
this.isLeader = leader | |
this.type = type | |
this.inventory = new Inventory() | |
} | |
addItem (item) { | |
// Aquí faltaría algo | |
this.inventory.add(item) | |
} | |
getInventory () { | |
let inventory = '' | |
this.inventory.get().forEach((item) => { | |
inventory += `[+] ${item}\n` | |
}) | |
return inventory | |
} | |
getPokemons () { | |
const pokeballs = [] | |
let itemName = '' | |
for (let i = 0; i < this.inventory.get().length; i++) { | |
itemName = this.inventory.get()[i].constructor.name | |
if (itemName.indexOf('Pokemon') >= 0) { | |
pokeballs.push(this.inventory.get()[i]) | |
} | |
} | |
return pokeballs | |
} | |
getHealingItems () { | |
const healingItems = [] | |
let itemName = '' | |
for (let i = 0; i < this.inventory.get().length; i++) { | |
itemName = this.inventory.get()[i].constructor.name | |
if (itemName.indexOf('Heal') >= 0) { | |
healingItems.push(this.inventory.get()[i]) | |
} | |
} | |
return healingItems | |
} | |
action (rival) { | |
const randomAction = Math.floor(Math.random() * 11) | |
const aliveRivalPokemons = rival.getPokemons().filter(pokemon => pokemon.hp > 0) | |
const myPokemons = this.getPokemons().filter(pokemon => pokemon.hp > 0) | |
if (aliveRivalPokemons.length > 0) { | |
const oponent = aliveRivalPokemons[0] | |
const myPokemon = myPokemons[0] | |
let damage = 0 | |
if (randomAction >= 6) { | |
damage = myPokemon.regularAttack(oponent) | |
console.log(`(${this.name}) ${myPokemon.name} (${myPokemon.hp}) - Ataca normal a ${oponent.name} causando ${damage} de daño`) | |
oponent.hp -= damage | |
} else if (randomAction >= 3 && randomAction < 6) { | |
damage = myPokemon.specialAttack() | |
console.log(`(${this.name}) ${myPokemon.name} (${myPokemon.hp}) - Ataca especial a ${oponent.name} causando ${damage} de daño`) | |
oponent.hp -= damage | |
} else if (this.getHealingItems().length > 0) { | |
// Aquí falta algo | |
console.log('Esto hay que terminarlo') | |
} else { | |
console.log(`(${this.name}) Ups! Parece que no tenemos items.. tu pokemon se ha echado una pokesiesta`) | |
} | |
} else { | |
// Aquí falta algo | |
console.log(`${this.name} gana la batalla!`) | |
} | |
} | |
} | |
class Battle { | |
constructor (trainer1, trainer2) { | |
this.trainer1 = trainer1 | |
this.trainer2 = trainer2 | |
} | |
start () { | |
let alivePokemons1, alivePokemons2 | |
let firstTurn = true | |
do { | |
firstTurn ? this.trainer1.action(this.trainer2) : this.trainer2.action(this.trainer1) | |
firstTurn = !firstTurn | |
alivePokemons1 = this.trainer1.getPokemons().filter(pokemon => pokemon.hp > 0) | |
alivePokemons2 = this.trainer2.getPokemons().filter(pokemon => pokemon.hp > 0) | |
} while (alivePokemons1.length >= 1 && alivePokemons2.length >= 1) | |
console.log('Ha finalizado la batalla!') | |
} | |
} | |
// | |
const p1 = new Trainer('Moi', 'Kanto') | |
p1.addItem(new WaterPokemon('Squirtle', 38, 45, 70)) | |
p1.addItem(new HealItem(20)) | |
p1.addItem(new WaterPokemon('Lapras', 94, 85, 70)) | |
// | |
const r1 = new Trainer('Manz', 'Paldea', true, 'water') | |
r1.addItem(new WaterPokemon('Magikarp', 1, 1, 90)) | |
r1.addItem(new WaterPokemon('Vaporeon', 71, 85, 60)) | |
// | |
const BATTLE = new Battle(p1, r1) | |
BATTLE.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment