{}
CODE VISUALIZER
Master DSA, Python and C with step-by-step code visualization.
See it in action
CODE VISUALIZER
Master DSA, Python and C with step-by-step code visualization.
See it in action
run-icon
main.py
import random # General class representing a leader with special abilities class General: def __init__(self, name, ability): self.name = name self.ability = ability def apply_ability(self, army): ability_map = { "Boost Morale": lambda: self._boost_morale(army), "Increase Attack": lambda: self._increase_attack(army), "Increase Defense": lambda: self._increase_defense(army), "Tactical Withdrawal": lambda: self._tactical_withdrawal(army), "Force March": lambda: self._force_march(army), } ability_map.get(self.ability, lambda: None)() def _boost_morale(self, army): army.morale += 20 print(f"{self.name} boosts morale by 20!") def _increase_attack(self, army): army.attack += 10 print(f"{self.name} increases attack strength by 10!") def _increase_defense(self, army): army.defense += 10 print(f"{self.name} increases defense strength by 10!") def _tactical_withdrawal(self, army): army.morale -= 10 army.attack -= 5 print(f"{self.name} orders a tactical withdrawal, sacrificing attack power but preserving troops.") def _force_march(self, army): army.attack += 5 army.morale -= 10 print(f"{self.name} orders a force march, increasing attack but decreasing morale due to exhaustion.") # Army class representing the army's stats and abilities class Army: def __init__(self, name, troops, attack, defense, morale, unit_type, general=None, home_field=False, is_naval=False): self.name = name self.troops = troops self.attack = attack self.defense = defense self.morale = morale self.unit_type = unit_type self.base_attack = attack self.base_defense = defense self.general = general self.home_field = home_field self.is_naval = is_naval # Flag to check if the battle is naval self.exhaustion = 0 # Tracks troop exhaustion over time self.history = [] def calculate_combat_effectiveness(self, terrain, weather): effectiveness = (self.attack + self.defense + self.morale) * self.troops effectiveness *= self._apply_terrain_effect(terrain) effectiveness *= self._apply_weather_effect(weather) effectiveness *= self._apply_home_field_effect() effectiveness *= self._apply_unit_type_bonus() effectiveness *= random.uniform(0.8, 1.2) # Unpredictability factor effectiveness *= self._apply_exhaustion_penalty() return effectiveness def _apply_terrain_effect(self, terrain): terrain_effects = { "urban": 1.1, "desert": 1.2, "forest": 0.9, "jungle": 0.8, "mountain": 1.15, "arctic": 0.85, "coastal": 1.05, "swamp": 0.75, "volcanic": 1.3, "ocean": 0.5, "cave": 0.7, "plains": 1.0, "tundra": 0.9, "savannah": 1.1, "wetlands": 0.85, "river": 1.05, "crater": 1.2, "prairie": 1.0, "canyon": 1.2, "glacier": 0.7, "caves": 0.8, "highlands": 1.2, "marsh": 0.7, "steppe": 1.0 } return terrain_effects.get(terrain, 1.0) def _apply_weather_effect(self, weather): weather_effects = { "clear": 1.0, "thunderstorm": 0.8, "sandstorm": 0.6, "fog": 0.7, "blizzard": 0.7, "storm": 0.6, "rain": 0.85, "hurricane": 0.5, "sunny": 1.0, "hail": 0.6, "dust": 0.7, "overcast": 0.9, "heatwave": 0.8, "snow": 0.75, "tornado": 0.5, "lightning": 0.85 } return weather_effects.get(weather, 1.0) def _apply_home_field_effect(self): return 1.2 if self.home_field else 1.0 def _apply_unit_type_bonus(self): unit_bonuses = { "tank": 1.3, "infantry": 1.0, "artillery": 1.2, "helicopter": 1.5, "drone": 1.4, "special forces": 1.25, "navy": 1.4, "submarine": 1.6 } return unit_bonuses.get(self.unit_type, 1.0) def _apply_exhaustion_penalty(self): return max(1 - (self.exhaustion * 0.05), 0.5) def take_casualties(self, casualties): casualties = min(self.troops, casualties) # Ensure casualties do not exceed current troops self.troops -= casualties self.exhaustion += casualties // 10 # Increased exhaustion with casualties return casualties def add_to_history(self, event): self.history.append(event) # Random event generator function def random_event(): events = [ "Reinforcements have arrived!", "An ambush has occurred!", "Morale shift: Troops are demoralized!", "Drone strike! Heavy damage to infantry!", "Artillery barrage! Significant damage to the enemy!", "Naval bombardment! Devastating blow from the sea!", "Sudden hailstorm! Troops suffer casualties!", "Sneak attack! Special forces cause massive chaos!", "A supply drop! Your forces gain additional resources!", "Exhaustion is taking a toll on your forces!", "A deadly heatwave weakens your troops' stamina!", "Troops are fighting through a blizzard, morale drops!" ] event = random.choice(events) return event # Apply random events to army def apply_random_event(army, event): event_actions = { "Reinforcements have arrived!": lambda: apply_reinforcements(army), "An ambush has occurred!": lambda: apply_ambush(army), "Morale shift: Troops are demoralized!": lambda: apply_morale_shift(army), "Drone strike! Heavy damage to infantry!": lambda: apply_drone_strike(army), "Artillery barrage! Significant damage to the enemy!": lambda: apply_artillery_barrage(army), "Naval bombardment! Devastating blow from the sea!": lambda: apply_naval_bombardment(army), "Sudden hailstorm! Troops suffer casualties!": lambda: apply_hailstorm(army), "Sneak attack! Special forces cause massive chaos!": lambda: apply_special_forces_attack(army), "A supply drop! Your forces gain additional resources!": lambda: apply_supply_drop(army), "Exhaustion is taking a toll on your forces!": lambda: apply_exhaustion(army), "A deadly heatwave weakens your troops' stamina!": lambda: apply_heatwave(army), "Troops are fighting through a blizzard, morale drops!": lambda: apply_blizzard(army) } event_actions.get(event, lambda: None)() def apply_reinforcements(army): reinforcement_troops = random.randint(10, 50) army.troops += reinforcement_troops army.morale += 10 army.add_to_history(f"Reinforcements have arrived, bolstering {army.name}'s troops by {reinforcement_troops}. Morale increased by 10.") def apply_ambush(army): army.morale -= 15 army.add_to_history(f"{army.name} was caught in an ambush! Morale drops by 15.") def apply_morale_shift(army): army.morale -= 20 army.add_to_history(f"Morale shift: {army.name}'s troops feel demoralized. Morale drops by 20.") def apply_drone_strike(army): if army.unit_type == "infantry": casualties = random.randint(10, 30) army.take_casualties(casualties) army.add_to_history(f"{army.name}'s infantry suffered {casualties} casualties from a devastating drone strike.") def apply_artillery_barrage(army): casualties = random.randint(20, 40) army.take_casualties(casualties) army.add_to_history(f"{army.name} took {casualties} casualties from a brutal artillery barrage.") def apply_naval_bombardment(army): if army.is_naval: casualties = random.randint(30, 60) army.take_casualties(casualties) army.add_to_history(f"{army.name} suffered {casualties} casualties from a naval bombardment!") else: army.add_to_history(f"{army.name} is not a naval force. The naval bombardment missed.") def apply_hailstorm(army): casualties = random.randint(5, 15) army.take_casualties(casualties) army.add_to_history(f"A sudden hailstorm hit {army.name}, causing {casualties} casualties.") def apply_special_forces_attack(army): casualties = random.randint(15, 30) army.take_casualties(casualties) army.add_to_history(f"Special forces launched a sneak attack on {army.name}, causing {casualties} casualties.") def apply_supply_drop(army): reinforcement_troops = random.randint(20, 40) army.troops += reinforcement_troops army.add_to_history(f"A supply drop arrived, boosting {army.name}'s forces by {reinforcement_troops} troops.") def apply_exhaustion(army): army.morale -= 10 army.attack -= 5 army.add_to_history(f"Exhaustion has taken its toll on {army.name}. Morale dropped by 10 and attack strength reduced by 5.") def apply_heatwave(army): army.attack -= 10 army.add_to_history(f"A heatwave hit {army.name}, weakening attack strength by 10.") def apply_blizzard(army): army.morale -= 15 army.add_to_history(f"A fierce blizzard has struck {army.name}, causing morale to drop by 15.") def simulate_battle(army1, army2, terrain, weather): round_counter = 1 battle_story = [] # List to hold the battle narrative while army1.troops > 0 and army2.troops > 0 and round_counter <= 10: battle_story.append(f"\nRound {round_counter} - The battle rages on...") battle_story.append(f"{army1.name} has {army1.troops} troops left.") battle_story.append(f"{army2.name} has {army2.troops} troops left.") # Random events in each round event1 = random_event() apply_random_event(army1, event1) event2 = random_event() apply_random_event(army2, event2) # Generals apply abilities if army1.general: army1.general.apply_ability(army1) if army2.general: army2.general.apply_ability(army2) # Calculate combat effectiveness with terrain and weather modifiers army1_effectiveness = army1.calculate_combat_effectiveness(terrain, weather) army2_effectiveness = army2.calculate_combat_effectiveness(terrain, weather) # Calculate damage and casualties army1_damage = max(army1_effectiveness - army2.defense * army2.troops, 0) army2_damage = max(army2_effectiveness - army1.defense * army1.troops, 0) army1_casualties = army1.take_casualties(int(army1_damage // 10)) army2_casualties = army2.take_casualties(int(army2_damage // 10)) battle_story.append(f"{army1.name} lost {army1_casualties} troops.") battle_story.append(f"{army2.name} lost {army2_casualties} troops.") # Add to the history for deeper storytelling battle_story.append("\nKey Events and Outcomes:") battle_story.extend(army1.history[-3:]) battle_story.extend(army2.history[-3:]) round_counter += 1 # Check if either army is defeated if army1.troops <= 0: battle_story.append(f"\n{army1.name} has been completely defeated. The battlefield falls silent...") break if army2.troops <= 0: battle_story.append(f"\n{army2.name} has been wiped out. Victory belongs to {army1.name}!") break return battle_story def create_army(army_name): print(f"Creating army for {army_name}") name = input(f"Enter name for {army_name}: ") troops = int(input(f"How many troops does {army_name} have? ")) attack = int(input(f"Enter attack strength for {army_name} (1-100): ")) defense = int(input(f"Enter defense strength for {army_name} (1-100): ")) morale = int(input(f"Enter morale for {army_name} (1-100): ")) unit_type = input(f"Enter unit type for {army_name} (tank/infantry/artillery/helicopter/drone/special forces/navy/submarine): ").lower() home_field = input(f"Does {army_name} have home field advantage? (yes/no): ").lower() == "yes" is_naval = input(f"Is {army_name} a naval force? (yes/no): ").lower() == "yes" general_name = input(f"Enter the general's name for {army_name}: ") general_ability = input(f"Enter the general's ability for {army_name} (Boost Morale/Increase Attack/Increase Defense/Tactical Withdrawal/Force March): ").lower() general = General(general_name, general_ability) return Army(name, troops, attack, defense, morale, unit_type, general, home_field, is_naval) def main(): print("Welcome to the Advanced Warfare Simulator!") # Army creation army1 = create_army("Army 1") army2 = create_army("Army 2") terrain = input("Enter terrain (urban, desert, forest, jungle, mountain, arctic, coastal, swamp, volcanic, ocean, cave, plains, tundra, savannah, wetlands, river, crater, prairie, canyon, glacier, highlands, marsh, steppe): ").lower() weather = input("Enter weather (clear, thunderstorm, sandstorm, fog, blizzard, storm, rain, hail, dust, sunny, overcast, heatwave, snow, tornado, lightning): ").lower() # Simulate battle battle_story = simulate_battle(army1, army2, terrain, weather) # Print the battle story print("\nBattle Story:") for line in battle_story: print(line) if __name__ == "__main__": main()
Output