德意志的欧洲
wangyuanb
·
·
个人记录
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <limits>
#include <cstdlib>
#include <cstdio>
using namespace std;
// 清屏函数
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// 等待用户输入
void waitForInput() {
printf("\n按Enter继续...");
while (getchar() != '\n');
getchar();
}
// 获取用户输入的整数
int getIntegerInput(const char* prompt, int min = INT_MIN, int max = INT_MAX) {
int value;
while (1) {
printf(prompt);
if (scanf("%d", &value) != 1) {
while (getchar() != '\n'); // 清除无效输入
printf("输入无效,请输入一个整数。\n");
continue;
}
while (getchar() != '\n'); // 清除行末的换行符
if (value >= min && value <= max) {
return value;
} else {
printf("输入无效,请输入%d到%d之间的整数。\n", min, max);
}
}
}
// 地形类型枚举
enum class TerrainType {
PLAINS,
FOREST,
MOUNTAINS,
RIVER
};
// 军队类型枚举
enum class MilitaryType {
INFANTRY, // 步兵
CAVALRY, // 骑兵
ARTILLERY, // 炮兵
AIR_FORCE_FIGHTER, // 空军战斗机
AIR_FORCE_BOMBER, // 空军轰炸机
NAVY_FRIGATE, // 海军护卫舰
NAVY_SUBMARINE // 海军潜艇
};
// 国家枚举
enum class CountryType {
GERMANY,
POLAND,
SOVIET_UNION
};
// 领土类
class Territory {
public:
Territory(string name, TerrainType terrain, int population, int resources)
: name(move(name)), terrain(terrain), population(population), resources(resources) {}
// 获取领土名称
string getName() const { return name; }
// 获取领土地形
TerrainType getTerrain() const { return terrain; }
// 获取领土人口
int getPopulation() const { return population; }
// 获取领土资源
int getResources() const { return resources; }
// 增加人口
void growPopulation() {
population = min(population + (rand() % 10) + 1, 1000);
}
// 增加资源
void produceResources() {
resources = min(resources + (rand() % 20) + 1, 500);
}
// 从领土征收资金
int collectTaxes() {
int taxes = (population / 10) + (resources / 5);
resources -= (resources / 10);
return taxes;
}
private:
string name;
TerrainType terrain;
int population;
int resources;
};
// 军队单位类
class MilitaryUnit {
public:
// 构造函数
MilitaryUnit(MilitaryType type, int number, int cost)
: type(type), number(number), cost(cost) {}
// 获取军队单位的类型
MilitaryType getType() const { return type; }
// 获取军队单位的人数
int getNumber() const { return number; }
// 获取军队单位的成本
int getCost() const { return cost; }
// 减少军队单位的人数
void reduceNumber(int amount) {
number = max(0, number - amount);
}
private:
MilitaryType type; // 军队单位的类型
int number; // 军队单位的人数
int cost; // 军队单位的成本
};
// 国家类
class Country {
public:
// 构造函数
Country(string name, int money, CountryType countryType)
: name(move(name)), money(money), countryType(countryType), airSupremacy(0), seaSupremacy(0) {}
// 获取国家的名称
const string& getName() const { return name; }
// 获取国家的资金
int getMoney() const { return money; }
// 获取国家的制空权
int getAirSupremacy() const { return airSupremacy; }
// 获取国家的制海权
int getSeaSupremacy() const { return seaSupremacy; }
// 获取国家类型
CountryType getCountryType() const { return countryType; }
// 为国家添加一个军队单位
void addUnit(const MilitaryUnit& unit) {
if (money >= unit.getCost()) {
units.push_back(unit);
money -= unit.getCost();
updateSupremacy(unit.getType());
printf("【系统】%s 成功招募了 %d 名%s!\n", name.c_str(), unit.getNumber(), getTypeString(unit.getType()).c_str());
} else {
printf("【系统】%s 资金不足,招募失败!\n", name.c_str());
}
}
// 显示国家的所有军队单位信息
void showUnits() const {
clearScreen();
printf("【%s 的军队】\n", name.c_str());
printf("资金: %d | 制空权: %d | 制海权: %d\n", money, airSupremacy, seaSupremacy);
printf("----------------------------------------\n");
if (units.empty()) {
printf("没有军队单位!\n");
} else {
for (size_t i = 0; i < units.size(); ++i) {
const auto& unit = units[i];
printf("%d. %s - 人数: %d - 成本: %d\n", i+1, getTypeString(unit.getType()).c_str(), unit.getNumber(), unit.getCost());
}
}
waitForInput();
}
// 获取国家的军队单位列表的引用
vector<MilitaryUnit>& getUnits() { return units; }
// 获取国家的军队单位列表的常量引用
const vector<MilitaryUnit>& getUnits() const { return units; }
// 调动国家的军队单位位置
void moveUnit(int fromIndex, int toIndex) {
if (fromIndex >= 0 && fromIndex < units.size() &&
toIndex >= 0 && toIndex < units.size()) {
swap(units[fromIndex], units[toIndex]);
printf("【系统】军队位置已调整!\n");
} else {
printf("【系统】无效的位置索引!\n");
}
waitForInput();
}
// 国家获得一定数量的资金
void gainMoney(int amount) {
money += amount;
printf("【系统】%s 获得了 %d 资金!\n", name.c_str(), amount);
}
// 将军队类型枚举转换为对应的字符串描述
string getTypeString(MilitaryType type) const {
switch (type) {
case MilitaryType::INFANTRY: return "步兵";
case MilitaryType::CAVALRY: return "骑兵";
case MilitaryType::ARTILLERY: return "炮兵";
case MilitaryType::AIR_FORCE_FIGHTER: return "空军战斗机";
case MilitaryType::AIR_FORCE_BOMBER: return "空军轰炸机";
case MilitaryType::NAVY_FRIGATE: return "海军护卫舰";
case MilitaryType::NAVY_SUBMARINE: return "海军潜艇";
default: return "未知兵种";
}
}
// 自动招募军队
void autoRecruit() {
printf("【系统】%s 开始自动招募军队...\n", name.c_str());
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> typeDis(1, 7);
while (true) {
int typeChoice = typeDis(gen);
MilitaryType type;
switch (typeChoice) {
case 1: type = MilitaryType::INFANTRY; break;
case 2: type = MilitaryType::CAVALRY; break;
case 3: type = MilitaryType::ARTILLERY; break;
case 4: type = MilitaryType::AIR_FORCE_FIGHTER; break;
case 5: type = MilitaryType::AIR_FORCE_BOMBER; break;
case 6: type = MilitaryType::NAVY_FRIGATE; break;
case 7: type = MilitaryType::NAVY_SUBMARINE; break;
default: continue;
}
int maxNumber = 0;
switch (type) {
case MilitaryType::INFANTRY: maxNumber = money; break;
case MilitaryType::CAVALRY: maxNumber = money / 2; break;
case MilitaryType::ARTILLERY: maxNumber = money / 3; break;
case MilitaryType::AIR_FORCE_FIGHTER: maxNumber = money / 5; break;
case MilitaryType::AIR_FORCE_BOMBER: maxNumber = money / 8; break;
case MilitaryType::NAVY_FRIGATE: maxNumber = money / 10; break;
case MilitaryType::NAVY_SUBMARINE: maxNumber = money / 15; break;
default: break;
}
if (maxNumber <= 0) break;
uniform_int_distribution<> numberDis(1, maxNumber);
int number = numberDis(gen);
int cost = 0;
switch (type) {
case MilitaryType::INFANTRY: cost = number; break;
case MilitaryType::CAVALRY: cost = number * 2; break;
case MilitaryType::ARTILLERY: cost = number * 3; break;
case MilitaryType::AIR_FORCE_FIGHTER: cost = number * 5; break;
case MilitaryType::AIR_FORCE_BOMBER: cost = number * 8; break;
case MilitaryType::NAVY_FRIGATE: cost = number * 10; break;
case MilitaryType::NAVY_SUBMARINE: cost = number * 15; break;
default: break;
}
if (cost > 0 && cost <= money) {
addUnit(MilitaryUnit(type, number, cost));
} else {
break;
}
}
printf("【系统】%s 招募完毕!\n", name.c_str());
}
// 添加领土
void addTerritory(Territory* territory) {
territories.push_back(territory);
printf("【系统】%s 获得了领土: %s!\n", name.c_str(), territory->getName().c_str());
}
// 移除领土
bool removeTerritory(Territory* territory) {
for (auto it = territories.begin(); it != territories.end(); ++it) {
if (*it == territory) {
territories.erase(it);
printf("【系统】%s 失去了领土: %s!\n", name.c_str(), territory->getName().c_str());
return true;
}
}
return false;
}
// 显示国家的领土
void showTerritories() const {
clearScreen();
printf("【%s 的领土】\n", name.c_str());
printf("----------------------------------------\n");
if (territories.empty()) {
printf("没有领土!\n");
} else {
for (const auto& territory : territories) {
printf(" - %s (地形: ", territory->getName().c_str());
switch (territory->getTerrain()) {
case TerrainType::PLAINS: printf("平原"); break;
case TerrainType::FOREST: printf("森林"); break;
case TerrainType::MOUNTAINS: printf("山地"); break;
case TerrainType::RIVER: printf("河流"); break;
default: printf("未知"); break;
}
printf(", 人口: %d, 资源: %d)\n",
territory->getPopulation(),
territory->getResources());
}
}
waitForInput();
}
// 获取国家的领土列表
const vector<Territory*>& getTerritories() const {
return territories;
}
// 国家发展回合
void develop() {
printf("【系统】%s 开始发展...\n", name.c_str());
// 领土发展
int totalTaxes = 0;
for (auto& territory : territories) {
territory->growPopulation();
territory->produceResources();
totalTaxes += territory->collectTaxes();
}
// 获得税收
gainMoney(totalTaxes);
printf("【系统】%s 从领土获得了 %d 资金税收!\n", name.c_str(), totalTaxes);
// 自动招募一些基础部队
if (territories.size() > 0 && money > 100) {
int recruitMoney = money / 5; // 用20%的资金自动招募
if (recruitMoney > 0) {
printf("【系统】%s 用 %d 资金自动补充军队...\n", name.c_str(), recruitMoney);
// 优先补充步兵
if (recruitMoney >= 10) {
int infantryCount = min(recruitMoney, 100) / 1;
addUnit(MilitaryUnit(MilitaryType::INFANTRY, infantryCount, infantryCount));
recruitMoney -= infantryCount;
}
// 剩余资金随机招募其他兵种
while (recruitMoney > 0) {
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> typeDis(2, 7);
MilitaryType type = static_cast<MilitaryType>(typeDis(gen) - 1);
int cost = 0;
switch (type) {
case MilitaryType::CAVALRY: cost = 2; break;
case MilitaryType::ARTILLERY: cost = 3; break;
case MilitaryType::AIR_FORCE_FIGHTER: cost = 5; break;
case MilitaryType::AIR_FORCE_BOMBER: cost = 8; break;
case MilitaryType::NAVY_FRIGATE: cost = 10; break;
case MilitaryType::NAVY_SUBMARINE: cost = 15; break;
default: cost = 1; break;
}
if (recruitMoney >= cost) {
int count = min(recruitMoney / cost, 50);
addUnit(MilitaryUnit(type, count, count * cost));
recruitMoney -= count * cost;
} else {
break;
}
}
}
}
waitForInput();
}
private:
// 更新制空权和制海权
void updateSupremacy(MilitaryType type) {
switch (type) {
case MilitaryType::AIR_FORCE_FIGHTER:
case MilitaryType::AIR_FORCE_BOMBER:
airSupremacy++;
break;
case MilitaryType::NAVY_FRIGATE:
case MilitaryType::NAVY_SUBMARINE:
seaSupremacy++;
break;
default:
break;
}
}
string name; // 国家的名称
int money; // 国家的资金
vector<MilitaryUnit> units; // 国家的军队单位列表
int airSupremacy; // 国家的制空权
int seaSupremacy; // 国家的制海权
CountryType countryType; // 国家类型
vector<Territory*> territories; // 国家的领土
};
// 获取地形对军队类型的加成或限制
double getTerrainModifier(TerrainType terrain, MilitaryType militaryType) {
switch (terrain) {
case TerrainType::PLAINS:
return (militaryType == MilitaryType::CAVALRY) ? 1.2 :
(militaryType == MilitaryType::ARTILLERY) ? 1.1 : 1.0;
case TerrainType::FOREST:
return (militaryType == MilitaryType::INFANTRY) ? 1.2 :
(militaryType == MilitaryType::CAVALRY) ? 0.8 : 1.0;
case TerrainType::MOUNTAINS:
return (militaryType == MilitaryType::INFANTRY) ? 1.1 :
(militaryType == MilitaryType::CAVALRY || militaryType == MilitaryType::ARTILLERY) ? 0.7 : 1.0;
case TerrainType::RIVER:
return (militaryType == MilitaryType::NAVY_FRIGATE || militaryType == MilitaryType::NAVY_SUBMARINE) ? 1.2 :
(militaryType == MilitaryType::CAVALRY) ? 0.6 : 1.0;
default:
return 1.0;
}
}
// 显示国家的兵种信息
void showCountryUnitsInfo(const Country& country) {
printf("【%s 的军队】\n", country.getName().c_str());
for (const auto& unit : country.getUnits()) {
printf(" - %s (%d人)\n", country.getTypeString(unit.getType()).c_str(), unit.getNumber());
}
printf("\n");
}
// 获取兵种相克加成
double getUnitModifier(MilitaryType attackerType, MilitaryType defenderType) {
// 定义兵种相克关系和加成系数
// 步兵克制骑兵,骑兵克制炮兵,炮兵克制步兵
// 空军战斗机克制空军轰炸机和海军单位
// 海军单位克制陆军单位在河流地形
if ((attackerType == MilitaryType::INFANTRY && defenderType == MilitaryType::CAVALRY) ||
(attackerType == MilitaryType::CAVALRY && defenderType == MilitaryType::ARTILLERY) ||
(attackerType == MilitaryType::ARTILLERY && defenderType == MilitaryType::INFANTRY)) {
return 1.3; // 克制时有30%伤害加成
} else if ((attackerType == MilitaryType::AIR_FORCE_FIGHTER && defenderType == MilitaryType::AIR_FORCE_BOMBER) ||
(attackerType == MilitaryType::AIR_FORCE_FIGHTER && defenderType == MilitaryType::NAVY_FRIGATE) ||
(attackerType == MilitaryType::AIR_FORCE_FIGHTER && defenderType == MilitaryType::NAVY_SUBMARINE)) {
return 1.2; // 空军战斗机对轰炸机和海军有20%加成
} else if ((defenderType == MilitaryType::INFANTRY || defenderType == MilitaryType::CAVALRY ||
defenderType == MilitaryType::ARTILLERY) &&
(attackerType == MilitaryType::NAVY_FRIGATE || attackerType == MilitaryType::NAVY_SUBMARINE)) {
return 1.1; // 海军对陆军有10%加成(在河流地形更明显)
} else if ((attackerType == MilitaryType::AIR_FORCE_BOMBER && defenderType == MilitaryType::INFANTRY) ||
(attackerType == MilitaryType::AIR_FORCE_BOMBER && defenderType == MilitaryType::CAVALRY) ||
(attackerType == MilitaryType::AIR_FORCE_BOMBER && defenderType == MilitaryType::ARTILLERY)) {
return 1.4; // 轰炸机对陆军有40%高伤害加成
}
return 1.0; // 无克制关系
}
// 战斗函数 - 支持一个兵种攻击多个目标
void battle(Country& attacker, Country& defender, TerrainType terrain, vector<Territory*>& allTerritories) {
clearScreen();
printf("【战争开始!】%s 进攻 %s\n", attacker.getName().c_str(), defender.getName().c_str());
printf("【地形】");
switch (terrain) {
case TerrainType::PLAINS: printf("平原\n"); break;
case TerrainType::FOREST: printf("森林\n"); break;
case TerrainType::MOUNTAINS: printf("山地\n"); break;
case TerrainType::RIVER: printf("河流\n"); break;
default: printf("未知\n"); break;
}
// 防守方自动招募
defender.autoRecruit();
waitForInput();
printf("\n【双方兵力】\n");
showCountryUnitsInfo(attacker);
showCountryUnitsInfo(defender);
printf("按Enter开始战斗...");
waitForInput();
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> damageDis(1, 10);
// 战斗逻辑
while (!attacker.getUnits().empty() && !defender.getUnits().empty()) {
clearScreen();
printf("【%s 选择攻击部队】\n", attacker.getName().c_str());
const auto& attackerUnits = attacker.getUnits();
if (attackerUnits.empty()) {
printf("没有可用的攻击部队!\n");
break;
}
printf("当前可用部队:\n");
for (size_t i = 0; i < attackerUnits.size(); ++i) {
const auto& unit = attackerUnits[i];
printf("%d. %s - %d人\n", i+1, attacker.getTypeString(unit.getType()).c_str(), unit.getNumber());
}
int attackerIndex = getIntegerInput("请选择攻击部队 (1-%lu): ", 1, attackerUnits.size()) - 1;
// 检查攻击部队是否有效
if (attackerIndex < 0 || attackerIndex >= attackerUnits.size() || attackerUnits[attackerIndex].getNumber() <= 0) {
printf("无效的攻击部队选择!\n");
waitForInput();
continue;
}
clearScreen();
printf("【%s 的 %s (%d人) 选择攻击目标】\n",
attacker.getName().c_str(),
attacker.getTypeString(attackerUnits[attackerIndex].getType()).c_str(),
attackerUnits[attackerIndex].getNumber());
const auto& defenderUnits = defender.getUnits();
if (defenderUnits.empty()) {
printf("没有可用的攻击目标!\n");
waitForInput();
break;
}
printf("当前可攻击目标:\n");
for (size_t i = 0; i < defenderUnits.size(); ++i) {
const auto& unit = defenderUnits[i];
printf("%d. %s - %d人\n", i+1, defender.getTypeString(unit.getType()).c_str(), unit.getNumber());
}
// 允许选择多个目标进行攻击
vector<int> targetIndices;
int maxTargets = min(3, static_cast<int>(defenderUnits.size())); // 最多选择3个目标
printf("请选择攻击目标 (输入0结束选择,最多选择%d个目标):\n", maxTargets);
while (true) {
printf("选择目标 (1-%lu, 0结束): ", defenderUnits.size());
int targetIndex = getIntegerInput("", 0, defenderUnits.size()) - 1;
if (targetIndex == -1) { // 输入0结束选择
if (targetIndices.empty()) {
printf("至少选择一个目标!\n");
continue;
}
break;
}
// 检查目标是否有效且未被选择过
if (targetIndex < 0 || targetIndex >= defenderUnits.size()) {
printf("无效的目标选择!\n");
continue;
}
if (find(targetIndices.begin(), targetIndices.end(), targetIndex) != targetIndices.end()) {
printf("该目标已被选择!\n");
continue;
}
targetIndices.push_back(targetIndex);
// 检查是否达到最大选择数量
if (targetIndices.size() >= maxTargets) {
printf("已达到最大选择数量!\n");
break;
}
}
clearScreen();
printf("【战斗】%s 的 %s 攻击 %s 的以下部队\n",
attacker.getName().c_str(),
attacker.getTypeString(attackerUnits[attackerIndex].getType()).c_str(),
defender.getName().c_str());
// 显示选择的目标
for (int idx : targetIndices) {
printf(" - %s (%d人)\n",
defender.getTypeString(defenderUnits[idx].getType()).c_str(),
defenderUnits[idx].getNumber());
}
// 计算总攻击点数,将根据目标数量分配
int totalAttackPoints = damageDis(gen) * attackerUnits[attackerIndex].getNumber() / 10;
// 计算每个目标分配的攻击点数
vector<int> attackPoints(targetIndices.size());
int remainingPoints = totalAttackPoints;
int remainingTargets = targetIndices.size();
// 随机分配攻击点数
for (size_t i = 0; i < targetIndices.size(); ++i) {
if (remainingTargets == 1) {
attackPoints[i] = remainingPoints;
break;
}
int maxPoints = remainingPoints - (remainingTargets - 1);
uniform_int_distribution<> pointDis(1, maxPoints);
attackPoints[i] = pointDis(gen);
remainingPoints -= attackPoints[i];
remainingTargets--;
}
// 执行攻击
printf("\n【战斗结果】\n");
for (size_t i = 0; i < targetIndices.size(); ++i) {
int defenderIndex = targetIndices[i];
// 计算实际伤害
double terrainMod = getTerrainModifier(terrain, attackerUnits[attackerIndex].getType());
double unitMod = getUnitModifier(attackerUnits[attackerIndex].getType(), defenderUnits[defenderIndex].getType());
double totalMod = terrainMod * unitMod;
int actualDamage = static_cast<int>(attackPoints[i] * totalMod);
// 计算反击伤害
int counterAttackPoints = damageDis(gen) * defenderUnits[defenderIndex].getNumber() / 10;
double defenderTerrainMod = getTerrainModifier(terrain, defenderUnits[defenderIndex].getType());
double defenderUnitMod = getUnitModifier(defenderUnits[defenderIndex].getType(), attackerUnits[attackerIndex].getType());
double defenderTotalMod = defenderTerrainMod * defenderUnitMod;
int counterDamage = static_cast<int>(counterAttackPoints * defenderTotalMod);
// 执行伤害
attacker.getUnits()[attackerIndex].reduceNumber(counterDamage);
defender.getUnits()[defenderIndex].reduceNumber(actualDamage);
printf("%s 的 %s 对 %s 的 %s 造成 %d 点伤害!\n",
attacker.getName().c_str(),
attacker.getTypeString(attackerUnits[attackerIndex].getType()).c_str(),
defender.getName().c_str(),
defender.getTypeString(defenderUnits[defenderIndex].getType()).c_str(),
actualDamage);
printf("%s 的 %s 反击造成 %d 点伤害!\n",
defender.getName().c_str(),
defender.getTypeString(defenderUnits[defenderIndex].getType()).c_str(),
counterDamage);
}
waitForInput();
// 移除被消灭的单位
auto& attackerUnitsRef = attacker.getUnits();
attackerUnitsRef.erase(
remove_if(attackerUnitsRef.begin(), attackerUnitsRef.end(),
[](const MilitaryUnit& unit) { return unit.getNumber() <= 0; }),
attackerUnitsRef.end()
);
auto& defenderUnitsRef = defender.getUnits();
defenderUnitsRef.erase(
remove_if(defenderUnitsRef.begin(), defenderUnitsRef.end(),
[](const MilitaryUnit& unit) { return unit.getNumber() <= 0; }),
defenderUnitsRef.end()
);
// 显示当前战斗状态
clearScreen();
printf("【当前战斗状态】\n");
printf("【%s 军队】\n", attacker.getName().c_str());
if (attacker.getUnits().empty()) {
printf(" 已无剩余部队\n");
} else {
for (const auto& unit : attacker.getUnits()) {
printf(" - %s (%d人)\n", attacker.getTypeString(unit.getType()).c_str(), unit.getNumber());
}
}
printf("\n【%s 军队】\n", defender.getName().c_str());
if (defender.getUnits().empty()) {
printf(" 已无剩余部队\n");
} else {
for (const auto& unit : defender.getUnits()) {
printf(" - %s (%d人)\n", defender.getTypeString(unit.getType()).c_str(), unit.getNumber());
}
}
waitForInput();
}
// 显示战斗结果
clearScreen();
printf("【战斗结束!】\n");
printf("\n【剩余兵力】\n");
showCountryUnitsInfo(attacker);
showCountryUnitsInfo(defender);
if (defender.getUnits().empty()) {
printf("【胜利!】%s 大获全胜!\n", attacker.getName().c_str());
// 胜利方获得领土和金钱奖励
int moneyReward = defender.getMoney() / 2;
attacker.gainMoney(moneyReward);
defender.gainMoney(-moneyReward);
// 获取防守方的领土
auto& defenderTerritories = defender.getTerritories();
if (!defenderTerritories.empty()) {
// 随机夺取1-2个领土
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> countDis(1, min(2, static_cast<int>(defenderTerritories.size())));
int territoriesToCapture = countDis(gen);
printf("\n【领土变更】\n");
vector<Territory*> territoriesToTransfer;
// 选择要转移的领土
for (int i = 0; i < territoriesToCapture && !defenderTerritories.empty(); ++i) {
uniform_int_distribution<> indexDis(0, defenderTerritories.size() - 1);
int index = indexDis(gen);
Territory* capturedTerritory = defenderTerritories[index];
territoriesToTransfer.push_back(capturedTerritory);
}
// 执行领土转移
for (auto territory : territoriesToTransfer) {
// 从防守方移除领土
defender.removeTerritory(territory);
// 添加到攻击方
attacker.addTerritory(territory);
}
}
} else if (attacker.getUnits().empty()) {
printf("【失败!】%s 的进攻被击退!\n", attacker.getName().c_str());
} else {
printf("【僵持!】双方都有剩余兵力,战斗陷入僵持。\n");
}
waitForInput();
}
// 游戏类
class Game {
public:
// 构造函数
Game() : germany("德国", 5000, CountryType::GERMANY),
poland("波兰", 3000, CountryType::POLAND),
sovietUnion("苏联", 7000, CountryType::SOVIET_UNION) {
clearScreen();
printf("【游戏开始】二战欧洲战场\n");
printf("----------------------------------------\n");
// 创建初始领土
createTerritories();
// 初始化各国领土
initializeTerritories();
// 初始化双方军队
printf("【系统】德国开始组建军队...\n");
germany.addUnit(MilitaryUnit(MilitaryType::INFANTRY, 500, 500));
germany.addUnit(MilitaryUnit(MilitaryType::CAVALRY, 200, 400));
germany.addUnit(MilitaryUnit(MilitaryType::ARTILLERY, 150, 450));
germany.addUnit(MilitaryUnit(MilitaryType::AIR_FORCE_FIGHTER, 100, 500));
germany.addUnit(MilitaryUnit(MilitaryType::AIR_FORCE_BOMBER, 80, 640));
germany.addUnit(MilitaryUnit(MilitaryType::NAVY_FRIGATE, 50, 500));
germany.addUnit(MilitaryUnit(MilitaryType::NAVY_SUBMARINE, 30, 450));
printf("\n【系统】波兰开始组建军队...\n");
poland.addUnit(MilitaryUnit(MilitaryType::INFANTRY, 400, 400));
poland.addUnit(MilitaryUnit(MilitaryType::CAVALRY, 150, 300));
poland.addUnit(MilitaryUnit(MilitaryType::ARTILLERY, 100, 300));
poland.addUnit(MilitaryUnit(MilitaryType::AIR_FORCE_FIGHTER, 80, 400));
poland.addUnit(MilitaryUnit(MilitaryType::AIR_FORCE_BOMBER, 60, 480));
poland.addUnit(MilitaryUnit(MilitaryType::NAVY_FRIGATE, 40, 400));
poland.addUnit(MilitaryUnit(MilitaryType::NAVY_SUBMARINE, 20, 300));
printf("\n【系统】苏联开始组建军队...\n");
sovietUnion.addUnit(MilitaryUnit(MilitaryType::INFANTRY, 800, 800));
sovietUnion.addUnit(MilitaryUnit(MilitaryType::CAVALRY, 300, 600));
sovietUnion.addUnit(MilitaryUnit(MilitaryType::ARTILLERY, 250, 750));
sovietUnion.addUnit(MilitaryUnit(MilitaryType::AIR_FORCE_FIGHTER, 150, 750));
sovietUnion.addUnit(MilitaryUnit(MilitaryType::AIR_FORCE_BOMBER, 120, 960));
sovietUnion.addUnit(MilitaryUnit(MilitaryType::NAVY_FRIGATE, 60, 600));
sovietUnion.addUnit(MilitaryUnit(MilitaryType::NAVY_SUBMARINE, 40, 600));
currentTurn = 1;
}
// 游戏主循环
void run() {
while (true) {
clearScreen();
printf("【第%d回合】二战欧洲战场\n", currentTurn);
printf("----------------------------------------\n");
// 显示游戏状态
showGameStatus();
// 玩家选择国家
printf("\n请选择操作的国家:\n");
printf("1. 德国\n");
printf("2. 波兰\n");
printf("3. 苏联\n");
printf("4. 下一回合\n");
printf("5. 保存游戏\n");
printf("6. 退出游戏\n");
int choice = getIntegerInput("请选择 (1-6): ", 1, 6);
switch (choice) {
case 1:
countryMenu(germany);
break;
case 2:
countryMenu(poland);
break;
case 3:
countryMenu(sovietUnion);
break;
case 4:
nextTurn();
break;
case 5:
saveGame();
break;
case 6:
printf("感谢游玩!再见!\n");
return;
}
}
}
private:
// 创建初始领土
void createTerritories() {
// 德国领土
territories.push_back(new Territory("东普鲁士", TerrainType::PLAINS, 350, 200));
territories.push_back(new Territory("西里西亚", TerrainType::PLAINS, 400, 250));
territories.push_back(new Territory("巴伐利亚", TerrainType::FOREST, 300, 150));
territories.push_back(new Territory("莱茵兰", TerrainType::PLAINS, 450, 300));
territories.push_back(new Territory("汉堡", TerrainType::RIVER, 250, 180));
// 波兰领土
territories.push_back(new Territory("华沙", TerrainType::PLAINS, 300, 180));
territories.push_back(new Territory("克拉科夫", TerrainType::MOUNTAINS, 250, 150));
territories.push_back(new Territory("波兹南", TerrainType::PLAINS, 280, 170));
territories.push_back(new Territory("维尔纽斯", TerrainType::FOREST, 220, 130));
territories.push_back(new Territory("格但斯克", TerrainType::RIVER, 200, 150));
// 苏联领土
territories.push_back(new Territory("莫斯科", TerrainType::PLAINS, 500, 350));
territories.push_back(new Territory("列宁格勒", TerrainType::PLAINS, 450, 300));
territories.push_back(new Territory("基辅", TerrainType::PLAINS, 400, 280));
territories.push_back(new Territory("明斯克", TerrainType::FOREST, 300, 200));
territories.push_back(new Territory("斯大林格勒", TerrainType::PLAINS, 350, 250));
}
// 初始化各国领土
void initializeTerritories() {
// 德国领土
for (int i = 0; i < 5; ++i) {
germany.addTerritory(territories[i]);
}
// 波兰领土
for (int i = 5; i < 10; ++i) {
poland.addTerritory(territories[i]);
}
// 苏联领土
for (int i = 10; i < 15; ++i) {
sovietUnion.addTerritory(territories[i]);
}
}
// 显示游戏状态
void showGameStatus() {
printf("【游戏状态】\n");
printf("----------------------------------------\n");
printf("【德国】\n");
printf(" 资金: %d\n", germany.getMoney());
printf(" 军队数量: %lu\n", germany.getUnits().size());
printf(" 领土数量: %lu\n", germany.getTerritories().size());
printf("\n【波兰】\n");
printf(" 资金: %d\n", poland.getMoney());
printf(" 军队数量: %lu\n", poland.getUnits().size());
printf(" 领土数量: %lu\n", poland.getTerritories().size());
printf("\n【苏联】\n");
printf(" 资金: %d\n", sovietUnion.getMoney());
printf(" 军队数量: %lu\n", sovietUnion.getUnits().size());
printf(" 领土数量: %lu\n", sovietUnion.getTerritories().size());
printf("\n");
}
// 国家菜单
void countryMenu(Country& country) {
while (true) {
clearScreen();
printf("【%s 菜单】\n", country.getName().c_str());
printf("----------------------------------------\n");
printf("1. 查看军队\n");
printf("2. 招募军队\n");
printf("3. 调动军队\n");
printf("4. 查看领土\n");
printf("5. 攻击他国\n");
printf("6. 国家发展\n");
printf("7. 返回主菜单\n");
int choice = getIntegerInput("请选择 (1-7): ", 1, 7);
switch (choice) {
case 1:
country.showUnits();
break;
case 2:
recruitMenu(country);
break;
case 3:
moveUnitsMenu(country);
break;
case 4:
country.showTerritories();
break;
case 5:
attackMenu(country);
break;
case 6:
country.develop();
break;
case 7:
return;
}
}
}
// 招募菜单
void recruitMenu(Country& country) {
while (true) {
clearScreen();
printf("【%s 招募军队】\n", country.getName().c_str());
printf("资金: %d\n", country.getMoney());
printf("----------------------------------------\n");
printf("1. 招募步兵 (1人 = 1资金)\n");
printf("2. 招募骑兵 (1人 = 2资金)\n");
printf("3. 招募炮兵 (1人 = 3资金)\n");
printf("4. 招募空军战斗机 (1人 = 5资金)\n");
printf("5. 招募空军轰炸机 (1人 = 8资金)\n");
printf("6. 招募海军护卫舰 (1人 = 10资金)\n");
printf("7. 招募海军潜艇 (1人 = 15资金)\n");
printf("8. 自动招募\n");
printf("9. 返回\n");
int choice = getIntegerInput("请选择 (1-9): ", 1, 9);
if (choice == 9) break;
if (choice == 8) {
country.autoRecruit();
waitForInput();
break;
}
MilitaryType type;
int costPerUnit;
switch (choice) {
case 1: type = MilitaryType::INFANTRY; costPerUnit = 1; break;
case 2: type = MilitaryType::CAVALRY; costPerUnit = 2; break;
case 3: type = MilitaryType::ARTILLERY; costPerUnit = 3; break;
case 4: type = MilitaryType::AIR_FORCE_FIGHTER; costPerUnit = 5; break;
case 5: type = MilitaryType::AIR_FORCE_BOMBER; costPerUnit = 8; break;
case 6: type = MilitaryType::NAVY_FRIGATE; costPerUnit = 10; break;
case 7: type = MilitaryType::NAVY_SUBMARINE; costPerUnit = 15; break;
default: continue;
}
int maxNumber = country.getMoney() / costPerUnit;
if (maxNumber <= 0) {
printf("资金不足,无法招募!\n");
waitForInput();
continue;
}
int number = getIntegerInput(
(string("请输入招募数量 (1-") + to_string(maxNumber) + "): ").c_str(),
1, maxNumber);
country.addUnit(MilitaryUnit(type, number, number * costPerUnit));
waitForInput();
}
}
// 调动军队菜单
void moveUnitsMenu(Country& country) {
while (true) {
clearScreen();
printf("【%s 调动军队】\n", country.getName().c_str());
printf("----------------------------------------\n");
const auto& units = country.getUnits();
if (units.empty()) {
printf("没有军队单位可调动!\n");
waitForInput();
return;
}
printf("当前军队部署:\n");
for (size_t i = 0; i < units.size(); ++i) {
printf("%d. %s - %d人\n", i+1, country.getTypeString(units[i].getType()).c_str(), units[i].getNumber());
}
printf("\n请选择要调动的军队索引 (1-%lu, 0返回): ", units.size());
int fromIndex = getIntegerInput("", 0, units.size()) - 1;
if (fromIndex == -1) break;
printf("请选择目标位置索引 (1-%lu): ", units.size());
int toIndex = getIntegerInput("", 1, units.size()) - 1;
country.moveUnit(fromIndex, toIndex);
}
}
// 攻击菜单
void attackMenu(Country& attacker) {
clearScreen();
printf("【%s 攻击菜单】\n", attacker.getName().c_str());
printf("----------------------------------------\n");
// 显示可选的攻击目标
printf("选择攻击目标:\n");
printf("1. 波兰\n");
printf("2. 苏联\n");
int choice = getIntegerInput("请选择 (1-2): ", 1, 2);
Country* defender;
if (choice == 1) {
defender = &poland;
} else {
defender = &sovietUnion;
}
// 检查是否有军队
if (attacker.getUnits().empty()) {
printf("没有可用的军队进行攻击!\n");
waitForInput();
return;
}
// 选择战斗地点
clearScreen();
printf("【选择战斗地点】\n");
printf("----------------------------------------\n");
printf("选择战场地形:\n");
printf("1. 平原\n");
printf("2. 森林\n");
printf("3. 山地\n");
printf("4. 河流\n");
int terrainChoice = getIntegerInput("请选择 (1-4): ", 1, 4);
TerrainType terrain = static_cast<TerrainType>(terrainChoice - 1);
// 开始战斗
battle(attacker, *defender, terrain, territories);
}
// 下一回合
void nextTurn() {
printf("【系统】进入下一回合...\n");
// 所有国家发展
germany.develop();
poland.develop();
sovietUnion.develop();
// 增加回合数
currentTurn++;
printf("【系统】第%d回合开始!\n", currentTurn);
waitForInput();
}
// 保存游戏(简单实现,实际应用中应使用文件操作)
void saveGame() {
printf("【系统】游戏保存功能尚未完全实现。\n");
printf("当前游戏进度:\n");
printf(" - 回合数: %d\n", currentTurn);
printf(" - 德国资金: %d\n", germany.getMoney());
printf(" - 波兰资金: %d\n", poland.getMoney());
printf(" - 苏联资金: %d\n", sovietUnion.getMoney());
printf("【系统】如需继续游戏,请不要关闭程序。\n");
waitForInput();
}
// 成员变量
Country germany;
Country poland;
Country sovietUnion;
vector<Territory*> territories;
int currentTurn;
};
int main() {
// 设置随机数种子
srand(time(nullptr));
// 创建并运行游戏
Game game;
game.run();
return 0;
}