Star Wars Battles
Project Overview Event-Driven Battle Simulation System
This project implements a sophisticated real-time battle simulation system modeling the conflict between Jedi and Sith forces across multiple planets. The system processes time-ordered troop deployments, executes battles using priority-based force matching, and provides comprehensive analytical outputs including battle statistics, general performance evaluations, running median calculations, and cinematic event detection for optimal "movie moments."
Technical Architecture
The system employs a dual priority queue architecture where each planet maintains separate queues for Jedi and Sith battalions with opposing priority schemes. Jedi battalions prioritize lower force sensitivity (making them vulnerable), while Sith battalions prioritize higher force sensitivity (making them aggressive). Battles occur when the top Sith's force meets or exceeds the top Jedi's force, with tie-breaking based on arrival timestamp and deployment order to ensure deterministic outcomes across all scenarios.
Core Algorithmic Components
Battalion Management: Each battalion carries critical metadata including force sensitivity level, timestamp, troop count, commanding general, faction type, and deployment order. The system processes battles immediately upon each deployment, matching compatible forces and calculating casualties. Survivors with remaining troops are re-inserted into their respective queues, maintaining the priority invariant while allowing for multi-stage engagements where a single battalion might fight multiple opponents.
Statistical Analysis Engine: Implements a dual-heap median tracker using max-heap for lower values and min-heap for upper values, enabling O(log n) insertions and O(1) median queries. The system maintains running medians for total troops lost per planet across all timestamps, automatically balancing the heaps to ensure the median is always accessible at the top of the appropriate heap. General evaluation tracks deployment counts and survival rates, computing aggregate statistics across both Jedi and Sith forces for each commander.
Cinematic Event Detector: The "movie watcher" mode identifies the most dramatic potential battle scenarios by tracking optimal ambush opportunities (Sith arriving before Jedi with maximum force differential) and attack scenarios (Jedi arriving before Sith with maximum force differential). Rather than storing all deployments, the system maintains running state of the strongest Sith and weakest Jedi seen so far, updating best-case scenarios on-the-fly as new troops arrive, achieving O(1) space complexity per planet regardless of deployment count.
Performance & Design Considerations
The implementation leverages STL priority queues with custom comparators for efficient force matching, supporting both manual input and pseudo-random deployment generation for testing at scale. The system enforces strict timestamp monotonicity, validates all general and planet IDs, and ensures positive values for force sensitivity and troop counts. Memory efficiency is achieved through inline median calculation (no storage of individual battle records unless verbose mode is enabled) and incremental movie watcher updates (tracking only extrema rather than full deployment history). The modular design allows independent toggling of verbose output, median tracking, general evaluation, and movie watcher analysis through command-line flags.