# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
import random
TROOP_TYPES = {
"Infantry": {"ATK": 50, "DEF": 30, "SPD": 20, "RANGED": False},
"Heavy Infantry": {"ATK": 70, "DEF": 50, "SPD": 15, "RANGED": False},
"Ranged Units": {"ATK": 40, "DEF": 20, "SPD": 25, "RANGED": True},
"Skirmishers": {"ATK": 30, "DEF": 15, "SPD": 30, "RANGED": False},
"Cavalry": {"ATK": 60, "DEF": 40, "SPD": 35, "RANGED": False},
"Siege Infantry": {"ATK": 80, "DEF": 60, "SPD": 10, "RANGED": False},
}
TERRAIN_MODIFIERS = {
"forest": {"ATK": 0.8, "DEF": 1.2, "SPD": 0.7},
"desert": {"ATK": 1.1, "DEF": 0.8, "SPD": 0.8},
"mountain": {"ATK": 1.0, "DEF": 1.0, "SPD": 0.6},
"plain": {"ATK": 1.0, "DEF": 1.0, "SPD": 1.0},
}
def battle_simulation(army1, army2, terrain, is_fortified):
terrain_mod = TERRAIN_MODIFIERS.get(terrain, TERRAIN_MODIFIERS["plain"])
total_damage1 = 0
total_damage2 = 0
ranged_damage1 = 0
ranged_damage2 = 0
for troop_type, count in army1["troops"].items():
stats1 = TROOP_TYPES[troop_type]
atk1, def1, spd1, is_ranged = stats1["ATK"], stats1["DEF"], stats1["SPD"], stats1["RANGED"]
atk1 *= terrain_mod["ATK"] * army1["MOR"]
def1 *= terrain_mod["DEF"] * army1["MOR"]
spd1 *= terrain_mod["SPD"]
if is_ranged:
ranged_damage1 += (atk1 * count) - (def1 * count * 0.8)
else:
total_damage1 += (atk1 * count) - (def1 * count * 0.8)
for troop_type, count in army2["troops"].items():
stats2 = TROOP_TYPES[troop_type]
atk2, def2, spd2, is_ranged = stats2["ATK"], stats2["DEF"], stats2["SPD"], stats2["RANGED"]
atk2 *= terrain_mod["ATK"] * army2["MOR"]
def2 *= terrain_mod["DEF"] * army2["MOR"]
spd2 *= terrain_mod["SPD"]
if is_fortified:
def2 *= 1.5
if is_ranged:
ranged_damage2 += (atk2 * count) - (def2 * count * 0.8)
else:
total_damage2 += (atk2 * count) - (def2 * count * 0.8)
rng_shift = random.uniform(0.5, 2.0)
total_damage1 *= rng_shift
total_damage2 *= rng_shift
ranged_damage1 *= rng_shift
ranged_damage2 *= rng_shift
total_damage1 += ranged_damage1
total_damage2 += ranged_damage2
casualties_1 = total_damage1 // 140
casualties_2 = total_damage2 // 150
survivors_1 = sum(army1["troops"].values()) - casualties_1
survivors_2 = sum(army2["troops"].values()) - casualties_2
survivors_1 = max(survivors_1, 0)
survivors_2 = max(survivors_2, 0)
if survivors_1 > survivors_2:
winner = "Army 1 Wins!"
elif survivors_2 > survivors_1:
winner = "Army 2 Wins!"
else:
winner = "It's a Draw!"
return {
"Army 1 Survivors": int(survivors_1),
"Army 2 Survivors": int(survivors_2),
"Army 1 Casualties": int(casualties_1),
"Army 2 Casualties": int(casualties_2),
"Winner": winner
}
print("\nEnter the number of each troop type for Army 1 (e.g., Infantry: 2000, Cavalry: 2000, etc.):")
army1_troops = {
"Infantry": int(input("Infantry: ")),
"Heavy Infantry": int(input("Heavy Infantry: ")),
"Ranged Units": int(input("Ranged Units: ")),
"Skirmishers": int(input("Skirmishers: ")),
"Cavalry": int(input("Cavalry: ")),
"Siege Infantry": int(input("Siege Infantry: "))
}
army1_morale = float(input("Enter Army 1 morale (0 to 2): "))
print("\nEnter the number of each troop type for Army 2 (e.g., Infantry: 2000, Cavalry: 2000, etc.):")
army2_troops = {
"Infantry": int(input("Infantry: ")),
"Heavy Infantry": int(input("Heavy Infantry: ")),
"Ranged Units": int(input("Ranged Units: ")),
"Skirmishers": int(input("Skirmishers: ")),
"Cavalry": int(input("Cavalry: ")),
"Siege Infantry": int(input("Siege Infantry: "))
}
army2_morale = float(input("Enter Army 2 morale (0 to 2): "))
terrain = input("Enter the terrain type (forest, desert, mountain, plain): ").lower()
is_fortified = input("Is Army 2 in a fortification? (yes/no): ").lower() == "yes"
army1_stats = {
"MOR": army1_morale,
"troops": army1_troops
}
army2_stats = {
"MOR": army2_morale,
"troops": army2_troops
}
battle_result = battle_simulation(army1_stats, army2_stats, terrain, is_fortified)
print("\nBattle Result:")
print(f"Army 1 Survivors: {battle_result['Army 1 Survivors']}")
print(f"Army 2 Survivors: {battle_result['Army 2 Survivors']}")
print(f"Army 1 Casualties: {battle_result['Army 1 Casualties']}")
print(f"Army 2 Casualties: {battle_result['Army 2 Casualties']}")
print(battle_result["Winner"])