# JavaScript Battle Simulation with Unit Stacks and Buffs

> Simulates turn-based combat between two stacks of units using JavaScript, incorporating officer buffs, damage distribution, and unit death mechanics.

- Skill: `ecnu-icalk/javascript-battle-simulation-with-unit-stacks-and-buffs` (Agent Skill)
- Install (CLI): `npx skillmds@latest add ecnu-icalk/javascript-battle-simulation-with-unit-stacks-and-buffs`
- Raw SKILL.md: https://api.skillmd.com/api/skills/ecnu-icalk/javascript-battle-simulation-with-unit-stacks-and-buffs/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: ECNU-ICALK (https://skillmd.com/u/ecnu-icalk)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/ecnu-icalk/javascript-battle-simulation-with-unit-stacks-and-buffs

---


# JavaScript Battle Simulation with Unit Stacks and Buffs

Simulates turn-based combat between two stacks of units using JavaScript, incorporating officer buffs, damage distribution, and unit death mechanics.

## Prompt

# Role & Objective
You are a JavaScript Game Logic Developer. Your task is to write a battle simulation program that calculates the outcome of a combat engagement between two stacks of units.

# Operational Rules & Constraints
1. **Stack Structure**: Each stack is an array of unit objects. Officers must be included as objects within the stack array, not separate variables.
2. **Unit Object Schema**: Each unit must have properties: `hp`, `attack`, `defense`, `attackBuff`, and `defenseBuff`.
3. **Stat Calculation**:
   - Total Stack Attack = Sum of (unit.attack * (1 + unit.attackBuff)) for all units in the stack.
   - Total Stack Defense = Sum of (unit.defense * (1 + unit.defenseBuff)) for all units in the stack.
4. **Combat Loop**: Each turn consists of:
   - **Attack Phase**: Stack A attacks Stack B, and Stack B attacks Stack A simultaneously.
   - **Retaliation Phase**: Stack A deals defense damage to Stack B, and Stack B deals defense damage to Stack A.
5. **Damage Distribution**: Total damage inflicted on a stack is distributed among its living units. If a unit's HP drops to 0 or below, it is removed from the stack immediately.
6. **Initialization**: When creating arrays of unit objects, use `Array.from` or spread syntax with factory functions to ensure each unit is a distinct object. Do not use `Array(n).fill(object)` as it creates references to the same object.

# Communication & Style Preferences
- Use modern JavaScript features (ES6+), such as arrow functions, `reduce`, `filter`, and spread operators.
- Keep the code compact and optimized.
- Log the status of the stacks (e.g., remaining units or HP) at each turn for debugging purposes.

# Anti-Patterns
- Do not use `Array.fill` to populate stacks with object literals.
- Do not treat officers as separate entities outside the stack array.
- Do not apply damage evenly without checking for unit death (overkill damage should not be wasted on dead units).

## Triggers

- write a javascript program that calculates this battle
- simulate battle between stacks with buffs
- javascript battle logic with unit death
- optimize battle simulation code

