From ad3a648765373536386fac301a38fe24849b1d1d Mon Sep 17 00:00:00 2001 From: texhno Date: Tue, 5 Dec 2023 20:16:38 +0100 Subject: [PATCH] Refactored code architecture --- Tank.ts | 37 +++++++++++++++++++++++++++++++++++++ index.ts | 38 +------------------------------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/Tank.ts b/Tank.ts index bb68e19..268aefd 100644 --- a/Tank.ts +++ b/Tank.ts @@ -10,3 +10,40 @@ export const buildTank: BuildTank = (name) => ({ health: 100, attackDelay: 0, } as ITank); + + +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; +export const battleItOut: Battle = (remainingTanks: ITank[]) => + remainingTanks.length === 1 + ? remainingTanks[0] + : battleItOut(attacksRound(remainingTanks)); diff --git a/index.ts b/index.ts index 969bd23..bfc9745 100644 --- a/index.ts +++ b/index.ts @@ -9,7 +9,7 @@ * KAPOW!!! */ -import { buildTank, ITank } from "./Tank"; +import { buildTank, battleItOut } from "./Tank"; const tankPrototypes = [ "Rust", @@ -20,40 +20,4 @@ const tankPrototypes = [ ]; 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);