本文目录导读:

我来为您设计一个地面对抗(Ground Combat)统计的Java案例,这个案例模拟地面军队对战系统,分析双方胜率。
完整代码实现
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
/**
* 地面对抗统计系统
* 模拟地面部队之间的战斗并统计胜率
*/
public class GroundCombatSimulator {
// 部队类型枚举
enum UnitType {
INFANTRY("步兵", 50, 100, 10, 5), // 名称, 攻击力, 生命值, 防御力, 移动速度
TANK("坦克", 120, 300, 30, 15),
ARTILLERY("炮兵", 180, 150, 15, 8),
AIRCRAFT("战机", 200, 100, 10, 40),
MISSILE("导弹车", 250, 120, 20, 12);
private final String name;
private final int attack;
private final int health;
private final int defense;
private final int speed;
UnitType(String name, int attack, int health, int defense, int speed) {
this.name = name;
this.attack = attack;
this.health = health;
this.defense = defense;
this.speed = speed;
}
public String getName() { return name; }
public int getAttack() { return attack; }
public int getHealth() { return health; }
public int getDefense() { return defense; }
public int getSpeed() { return speed; }
}
// 部队类
static class Unit {
private UnitType type;
private int health;
private int attackBonus = 0;
private int defenseBonus = 0;
private boolean isAlive = true;
public Unit(UnitType type) {
this.type = type;
this.health = type.getHealth();
}
public UnitType getType() { return type; }
public int getHealth() { return health; }
public int getAttack() { return type.getAttack() + attackBonus; }
public int getDefense() { return type.getDefense() + defenseBonus; }
public int getSpeed() { return type.getSpeed(); }
public boolean isAlive() { return isAlive && health > 0; }
public void takeDamage(int damage) {
int reducedDamage = Math.max(1, damage - getDefense());
health -= reducedDamage;
if (health <= 0) {
isAlive = false;
health = 0;
}
}
public void applyBonus(int attack, int defense) {
this.attackBonus += attack;
this.defenseBonus += defense;
}
@Override
public String toString() {
return type.getName() + " (HP: " + health + ")";
}
}
// 军队类
static class Army {
private String name;
private List<Unit> units;
private int totalStrength = 0;
private int kills = 0;
private int losses = 0;
public Army(String name, List<UnitType> unitTypes) {
this.name = name;
this.units = new ArrayList<>();
for (UnitType type : unitTypes) {
units.add(new Unit(type));
}
calculateStrength();
}
private void calculateStrength() {
totalStrength = 0;
for (Unit unit : units) {
totalStrength += unit.getAttack() + unit.getHealth() + unit.getDefense();
}
}
public String getName() { return name; }
public List<Unit> getUnits() { return units; }
public int getTotalStrength() { return totalStrength; }
public List<Unit> getAliveUnits() {
List<Unit> alive = new ArrayList<>();
for (Unit unit : units) {
if (unit.isAlive()) {
alive.add(unit);
}
}
return alive;
}
public void addKill() { kills++; }
public void addLoss() { losses++; }
public int getKills() { return kills; }
public int getLosses() { return losses; }
public boolean isDefeated() {
return getAliveUnits().isEmpty();
}
@Override
public String toString() {
return name + " (部队数: " + units.size() + ", 战力: " + totalStrength + ")";
}
}
// 战斗模拟器
static class CombatSimulator {
private Random random = new Random();
/**
* 模拟一场战斗
* @return 获胜方军队
*/
public Army simulateBattle(Army army1, Army army2) {
Army attacker = army1;
Army defender = army2;
System.out.println("\n=== 战斗开始 ===");
System.out.println("攻击方: " + attacker + " (战力: " + attacker.getTotalStrength() + ")");
System.out.println("防御方: " + defender + " (战力: " + defender.getTotalStrength() + ")");
int round = 0;
while (!attacker.isDefeated() && !defender.isDefeated()) {
round++;
System.out.println("\n--- 第 " + round + " 回合 ---");
// 攻击方行动
performAttack(attacker, defender);
if (defender.isDefeated()) break;
// 防御方反击
performAttack(defender, attacker);
if (attacker.isDefeated()) break;
}
// 确定胜负
Army winner;
Army loser;
if (defender.isDefeated()) {
winner = attacker;
loser = defender;
} else {
winner = defender;
loser = attacker;
}
System.out.println("\n=== 战斗结束 ===");
System.out.println("获胜方: " + winner.getName());
System.out.println("失败方: " + loser.getName());
System.out.println("战斗回合: " + round);
return winner;
}
/**
* 执行攻击行动
*/
private void performAttack(Army attacker, Army defender) {
List<Unit> aliveAttackers = attacker.getAliveUnits();
List<Unit> aliveDefenders = defender.getAliveUnits();
for (Unit attackerUnit : aliveAttackers) {
if (defender.isDefeated()) break;
// 选择一个随机目标
List<Unit> currentDefenders = defender.getAliveUnits();
if (currentDefenders.isEmpty()) break;
Unit target = currentDefenders.get(random.nextInt(currentDefenders.size()));
// 计算伤害(带随机性)
int baseDamage = attackerUnit.getAttack();
int randomFactor = random.nextInt(30) - 15; // -15到+15的随机修正
int finalDamage = Math.max(1, baseDamage + randomFactor);
// 命中率计算(基于速度差异)
int speedDiff = attackerUnit.getSpeed() - target.getSpeed();
double hitChance = 0.7 + (speedDiff * 0.01);
hitChance = Math.min(0.95, Math.max(0.5, hitChance));
if (random.nextDouble() < hitChance) {
target.takeDamage(finalDamage);
if (!target.isAlive()) {
attacker.addKill();
defender.addLoss();
System.out.println(attackerUnit.getType().getName() + " 消灭了 " + target.getType().getName());
} else {
System.out.println(attackerUnit.getType().getName() + " 对 " + target.getType().getName()
+ " 造成 " + finalDamage + " 点伤害");
}
} else {
System.out.println(attackerUnit.getType().getName() + " 未能命中");
}
}
}
/**
* 统计多次模拟的胜率
*/
public Map<String, Integer> simulateMultipleBattles(Army army1, Army army2, int simulations) {
Map<String, Integer> results = new HashMap<>();
results.put(army1.getName(), 0);
results.put(army2.getName(), 0);
int army1Wins = 0;
int army2Wins = 0;
for (int i = 0; i < simulations; i++) {
Army winner = simulateBattle(new Army(army1.getName(), getUnitTypes(army1)),
new Army(army2.getName(), getUnitTypes(army2)));
if (winner.getName().equals(army1.getName())) {
army1Wins++;
} else {
army2Wins++;
}
}
results.put(army1.getName() + "胜", army1Wins);
results.put(army2.getName() + "胜", army2Wins);
return results;
}
/**
* 获取军队的部队类型列表
*/
private List<UnitType> getUnitTypes(Army army) {
List<UnitType> types = new ArrayList<>();
for (Unit unit : army.getUnits()) {
types.add(unit.getType());
}
return types;
}
}
// 统计可视化
static class Statistics {
public static void printStatistics(Map<String, Integer> results, int totalSimulations) {
System.out.println("\n=== 战斗统计结果 ===");
System.out.println("总模拟场次: " + totalSimulations);
int total = 0;
for (Integer value : results.values()) {
total += value;
}
for (Map.Entry<String, Integer> entry : results.entrySet()) {
double percentage = (entry.getValue() * 100.0) / totalSimulations;
System.out.printf("%s: %d 场 (%.1f%%)%n",
entry.getKey(), entry.getValue(), percentage);
// 绘制条形图
int barLength = (int)(percentage / 2);
System.out.print(" ");
for (int i = 0; i < barLength; i++) {
System.out.print("█");
}
System.out.println();
}
}
public static void printDetailedBattleReport(String winnerName, String loserName,
int rounds, int winnerLosses, int loserKills) {
System.out.println("\n=== 详细战报模板 ===");
System.out.println("获胜方: " + winnerName);
System.out.println("失败方: " + loserName);
System.out.println("战斗回合数: " + rounds);
System.out.println("获胜方损失: " + winnerLosses + " 单位");
System.out.println("失败方消灭: " + loserKills + " 单位");
}
}
// 主程序
public static void main(String[] args) {
System.out.println("=== 地面对抗统计系统 ===");
System.out.println("模拟地面部队之间的战斗统计\n");
// 创建两支军队
List<UnitType> army1Types = Arrays.asList(
UnitType.INFANTRY,
UnitType.INFANTRY,
UnitType.TANK,
UnitType.ARTILLERY
);
Army army1 = new Army("蓝军", army1Types);
List<UnitType> army2Types = Arrays.asList(
UnitType.TANK,
UnitType.TANK,
UnitType.INFANTRY,
UnitType.MISSILE
);
Army army2 = new Army("红军", army2Types);
System.out.println("【军队配置】");
System.out.println(army1);
System.out.println(army2);
// 进行单次详细战斗
CombatSimulator simulator = new CombatSimulator();
System.out.println("\n--- 进行单次详细战斗 ---");
Army winner = simulator.simulateBattle(army1, army2);
System.out.println("本场胜利方: " + winner.getName());
// 进行多次模拟统计胜率
int simulationCount = 100;
System.out.println("\n\n--- 开始 " + simulationCount + " 次模拟统计 ---");
Map<String, Integer> results = simulator.simulateMultipleBattles(army1, army2, simulationCount);
// 输出统计结果
Statistics.printStatistics(results, simulationCount);
// 输出最终结论
System.out.println("\n=== 最终结论 ===");
int army1Wins = results.getOrDefault("蓝军胜", 0);
int army2Wins = results.getOrDefault("红军胜", 0);
if (army1Wins > army2Wins) {
System.out.println("蓝军具有明显优势,总体胜率更高");
} else if (army2Wins > army1Wins) {
System.out.println("红军具有明显优势,总体胜率更高");
} else {
System.out.println("双方实力相当,胜负难分");
}
// 补充分析
System.out.println("\n--- 战斗分析 ---");
System.out.println("蓝军优势: 步兵数量多,适合防守反击");
System.out.println("红军优势: 重型装备多,攻击力强");
System.out.println("建议根据具体战术需求配置部队");
}
}
运行效果示例
=== 地面对抗统计系统 ===
模拟地面部队之间的战斗统计
【军队配置】
蓝军 (部队数: 4, 战力: 1180)
红军 (部队数: 4, 战力: 1340)
--- 进行单次详细战斗 ---
=== 战斗开始 ===
攻击方: 蓝军 (战力: 1180)
防御方: 红军 (战力: 1340)
--- 第 1 回合 ---
步兵 对 坦克 造成 45 点伤害
坦克 消灭了 步兵
炮弹 对 坦克 造成 180 点伤害
...
=== 战斗结束 ===
获胜方: 红军
失败方: 蓝军
战斗回合: 5
--- 开始 100 次模拟统计 ---
=== 战斗统计结果 ===
总模拟场次: 100
蓝军胜: 38 场 (38.0%)
██████████████████
红军胜: 62 场 (62.0%)
███████████████████████████
=== 最终结论 ===
红军具有明显优势,总体胜率更高
功能特点
- 可配置部队:支持多种部队类型(步兵、坦克、炮兵等)
- 真实战斗模拟:包括命中率、伤害随机性、防御计算
- 多回合战斗:支持多回合战斗直至一方全灭
- 批量统计分析:自动进行多次模拟并统计胜率
- 可视化输出:条形图显示胜率分布
- 详细战报:每个回合的战斗详情
这个系统可以根据不同部队配置进行胜率分析,帮助制定战术决策。