Code style refactors [minor]

This commit is contained in:
texhno 2023-12-05 20:22:01 +01:00
parent ad3a648765
commit 6e617b970e
2 changed files with 5 additions and 3 deletions

View File

@ -1,9 +1,9 @@
export interface ITank {
name: string;
health: number;
attackDelay: number;
}
type BuildTank = (name: string) => ITank;
export const buildTank: BuildTank = (name) => ({
name,
@ -11,7 +11,6 @@ export const buildTank: BuildTank = (name) => ({
attackDelay: 0,
} as ITank);
type CritMultiplier = (health: number) => number;
const critMultiplier: CritMultiplier = (health) =>
Math.floor(Math.random() * 10) >= 10 - health / 10 ? 1 : 2;
@ -24,6 +23,7 @@ const setStats: SetStats = (tank) => ({
type Attack = (_: ITank[], tank: ITank, tIndex: number, tanks: ITank[]) => ITank[];
const attack: Attack = (_acc, tank, _iTank, tanks) => {
// Must refactor this imperative block
if (tank.attackDelay === 0) {
const target = tanks[Math.floor(Math.random() * tanks.length)];
const attackDamage = critMultiplier(tank.health) * tank.health / 100;

View File

@ -20,4 +20,6 @@ const tankPrototypes = [
];
const initialTanks = tankPrototypes.map(buildTank);
console.log(battleItOut(initialTanks).name);
const winner = battleItOut(initialTanks);
console.log(winner);