Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solucion reto #36 typescript #258

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,140 @@ import Foundation
* https://retosdeprogramacion.com/semanales2022.
*
*/


enum LightUnits {
Peloso = 1, Sureno_bueno = 2, Enano = 3, Númenóreano = 4, Elfos = 5
}

enum DarkUnits {
Sureno_malo = 1, Orco = 2, Goblin = 3, Huargo = 4, Trolls = 5
}

abstract class Army {
army: Unit[];
UnitFactory: UnitFactory;
name: string
value: number;

constructor(unitFactory: UnitFactory, name: string) {
this.UnitFactory = unitFactory;
this.army = [];
this.value = 0;
this.name = name;
}

private _addUnit(unit: Unit | null): boolean {
if (unit != null) {
this.army.push(unit);
this.value += unit.damage;
return true;
}
return false;
}

public addUnit(index: number): boolean {
let unit: Unit | null = null;
unit = this.UnitFactory.CreateUnit(index);
return this._addUnit(unit);
}

public Random() {
let min: number = 1;
let max: number = 10;
let numUnits = Math.floor(Math.random() * (max - min + 1)) + min;
for (let index = 0; index < numUnits; index++) {
let unit = Math.floor(Math.random() * (5 - 1 + 1)) + 1;
if (!this.addUnit(unit)) index--;
}
}
}

class Light extends Army {
constructor() {
super(new LightUnitsFactory(), "Light");
}
}

class Dark extends Army {
constructor() {
super(new DarkUnitsFactory(), "Dark");
}
}

abstract class UnitFactory {
public abstract CreateUnit(index: number): Unit | null;
}

class LightUnitsFactory extends UnitFactory {
public CreateUnit(index: number): Unit | null {
switch (index) {
case LightUnits.Sureno_bueno:
return new Unit("Light", 2, "Sureño_bueno");
case LightUnits.Elfos:
return new Unit("Light", 1, "Peloso");
case LightUnits.Enano:
return new Unit("Light", 3, "Enano");
case LightUnits.Númenóreano:
return new Unit("Light", 4, "Númenóreano");
case LightUnits.Peloso:
return new Unit("Light", 5, "Elfo");
default:
return null
}
}
}

class DarkUnitsFactory extends UnitFactory {

public CreateUnit(index: number): Unit | null {
switch (index) {
case DarkUnits.Sureno_malo:
return new Unit("Dark", 2, "Sureño_malo");
case DarkUnits.Orco:
return new Unit("Dark", 1, "Orco");
case DarkUnits.Goblin:
return new Unit("Dark", 2, "Goblin");
case DarkUnits.Huargo:
return new Unit("Dark", 4, "Huargo");
case DarkUnits.Trolls:
return new Unit("Dark", 5, "Troll");
default:
return null
}
}

}

class Unit {

type: string;
name: string;
damage: number;

constructor(type: string, damage: number, name: string) {
this.type = type;
this.name = name;
this.damage = damage;
}

}

function Battle(Army1: Army, Army2: Army) {

let winner: string;

if (Army1.value == Army2.value) {
winner = "Empate";
} else {
winner = Army1.value > Army2.value ? Army1.name : Army2.name;
}
console.log(`El resultado es: ${winner}`)

}

let army1 = new Light;
let army2 = new Dark;
army1.Random();
army2.Random();
Battle(army1,army2)