FatCat_Tanks_Challenge/index.ts

60 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-12-05 18:52:03 +00:00
/**
* FatCat Coders tank battle challenge :)
*
* There will be 5 tanks, representing different
* programming langugages.
*
* May the best language live
*
* KAPOW!!!
*/
import { buildTank, ITank } from "./Tank";
const tankPrototypes = [
"Rust",
"JavaScript",
"TypeScript",
"Haskell",
"HTML", // If it wins, it's a programming language
];
const initialTanks = tankPrototypes.map(buildTank);
type CritMultiplier = (health: number) => number;
const critMultiplier: CritMultiplier = (health) =>
Math.floor(Math.random() * 10) >= 10 - health / 10 ? 1 : 2;
type SetStats = (tank: ITank) => ITank;
const setStats: SetStats = (tank) => ({
...tank,
attackDelay: tank.attackDelay === 0 ? Math.floor(tank.health / 10) : tank.attackDelay - 1,
} as ITank);
type Attack = (_: ITank[], tank: ITank, tIndex: number, tanks: ITank[]) => ITank[];
const attack: Attack = (_acc, tank, _iTank, tanks) => {
if (tank.attackDelay === 0) {
const target = tanks[Math.floor(Math.random() * tanks.length)];
const attackDamage = critMultiplier(tank.health) * tank.health / 100;
target.health -= attackDamage;
};
return tanks.map(setStats);
};
type IsLive = (tank: ITank) => boolean;
const isLive: IsLive = (tank) => tank.health >= 0;
type Attacks = (tanks: ITank[]) => ITank[];
const attacksRound: Attacks = (tanks) =>
tanks
.reduce(attack, tanks)
.filter(isLive);
type Battle = (remainingTanks: ITank[]) => ITank;
const battleItOut: Battle = (remainingTanks: ITank[]) =>
remainingTanks.length === 1
? remainingTanks[0]
: battleItOut(attacksRound(remainingTanks));
console.log(battleItOut(initialTanks).name);