C++#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <random>
#include <ctime>
#include <algorithm>
#include <limits>
#include <sstream>
using namespace std;
// 前向声明
class General;
class City;
class Force;
class Game;
// 部队类必须在City和Force之后声明,因为它依赖这两个类
class Army;
// 全局随机数生成器
random_device rd;
mt19937 gen(rd());
// 工具函数
int randomInt(int min, int max) {
uniform_int_distribution<> dis(min, max);
return dis(gen);
}
double randomDouble(double min, double max) {
uniform_real_distribution<> dis(min, max);
return dis(gen);
}
string getTimeString(int hour) {
int day = hour / 24 + 1;
int h = hour % 24;
return "第" + to_string(day) + "天 " + (h < 10 ? "0" + to_string(h) : to_string(h)) + ":00";
}
// 武将类
class General {
private:
string name;
int attack; // 攻击力
int defense; // 防御力
int intelligence; // 智力
int leadership; // 领导力
int loyalty; // 忠诚度
bool isRecruited; // 是否已被招募
public:
General(string n, int a, int d, int i, int l)
: name(n), attack(a), defense(d), intelligence(i), leadership(l),
loyalty(randomInt(60, 95)), isRecruited(false) {}
// 获取属性
string getName() const { return name; }
int getAttack() const { return attack; }
int getDefense() const { return defense; }
int getIntelligence() const { return intelligence; }
int getLeadership() const { return leadership; }
int getLoyalty() const { return loyalty; }
bool getIsRecruited() const { return isRecruited; }
// 设置属性
void setLoyalty(int l) { loyalty = max(0, min(100, l)); }
void setRecruited(bool r) { isRecruited = r; }
// 提升属性
void upgrade() {
attack += randomInt(1, 3);
defense += randomInt(1, 3);
intelligence += randomInt(1, 3);
leadership += randomInt(1, 3);
}
// 显示信息
void showInfo() const {
cout << "[" << name << "]" << endl;
cout << " 攻击: " << attack << endl;
cout << " 防御: " << defense << endl;
cout << " 智力: " << intelligence << endl;
cout << " 领导力: " << leadership << endl;
cout << " 忠诚度: " << loyalty << endl;
}
};
// 城池类
class City {
private:
string name;
string location;
Force* owner; // 所属势力
int population; // 人口
int food; // 粮食
int money; // 金钱
int defense; // 防御力
vector<Army*> armies; // 城中部队
bool isCapital; // 是否为首都
public:
City(string n, string loc, bool capital = false)
: name(n), location(loc), owner(nullptr),
population(randomInt(5000, 20000)), food(randomInt(10000, 50000)),
money(randomInt(5000, 20000)), defense(randomInt(50, 150)),
isCapital(capital) {}
// 获取属性
string getName() const { return name; }
string getLocation() const { return location; }
Force* getOwner() const { return owner; }
int getPopulation() const { return population; }
int getFood() const { return food; }
int getMoney() const { return money; }
int getDefense() const { return defense; }
vector<Army*> getArmies() const { return armies; }
bool getIsCapital() const { return isCapital; }
// 设置属性
void setOwner(Force* o) { owner = o; }
void setPopulation(int p) { population = max(0, p); }
void setFood(int f) { food = max(0, f); }
void setMoney(int m) { money = max(0, m); }
void setDefense(int d) { defense = max(0, d); }
// 添加部队
void addArmy(Army* army);
// 移除部队
void removeArmy(Army* army);
// 计算守城战斗力
int calculateDefensePower() const;
// 资源增长
void growResources();
// 显示信息
void showInfo() const;
};
// 势力类
class Force {
private:
string name;
bool isPlayer; // 是否为玩家势力
vector<City*> cities; // 拥有的城池
vector<Army*> armies; // 拥有的部队
vector<General*> generals; // 拥有的武将
vector<Force*> allies; // 盟友
vector<Force*> enemies;// 敌人
int reputation; // 声望
bool isIndependent; // 是否独立
public:
Force(string n, bool player = false)
: name(n), isPlayer(player), reputation(randomInt(30, 70)),
isIndependent(player) {} // 玩家初始为独立
// 获取属性
string getName() const { return name; }
bool getIsPlayer() const { return isPlayer; }
vector<City*> getCities() const { return cities; }
vector<Army*> getArmies() const { return armies; }
vector<General*> getGenerals() const { return generals; }
int getReputation() const { return reputation; }
bool getIsIndependent() const { return isIndependent; }
vector<Force*> getAllies() const { return allies; }
vector<Force*> getEnemies() const { return enemies; }
// 设置属性
void setReputation(int r) { reputation = max(0, min(100, r)); }
void setIsIndependent(bool independent) { isIndependent = independent; }
// 添加城池
void addCity(City* city);
// 移除城池
void removeCity(City* city);
// 添加部队
Army* createArmy(string name, City* location);
// 移除部队
void removeArmy(Army* army);
// 招募武将
bool recruitGeneral(General* general);
// 解雇武将
bool dismissGeneral(General* general);
// 计算总实力
int calculatePower() const;
// 计算总资源
int getTotalFood() const;
int getTotalMoney() const;
int getTotalPopulation() const;
// 结盟
bool makeAlliance(Force* other);
// 宣战
bool declareWar(Force* other);
// 投靠其他势力
bool submitTo(Force* overlord);
// 独立
bool declareIndependence();
// AI行动
void aiAction(Game* game);
// 显示信息
void showInfo() const;
};
// 部队类
class Army {
private:
string name;
Force* owner; // 所属势力
vector<General*> generals; // 包含的武将
int soldierCount; // 士兵数量
int morale; // 士气
City* currentCity; // 当前所在城池
public:
Army(string n, Force* o, City* c)
: name(n), owner(o), soldierCount(randomInt(500, 1500)),
morale(100), currentCity(c) {}
// 获取属性
string getName() const { return name; }
Force* getOwner() const { return owner; }
int getSoldierCount() const { return soldierCount; }
int getMorale() const { return morale; }
City* getCurrentCity() const { return currentCity; }
vector<General*> getGenerals() const { return generals; }
// 设置属性
void setOwner(Force* o) { owner = o; }
void setCurrentCity(City* c) { currentCity = c; }
void setSoldierCount(int count) { soldierCount = max(0, count); }
void setMorale(int m) { morale = max(0, min(100, m)); }
// 添加武将
bool addGeneral(General* g);
// 移除武将
bool removeGeneral(General* g);
// 计算战斗力
int calculateCombatPower() const;
// 部队移动
bool moveTo(City* targetCity);
// 攻击其他部队
bool attackArmy(Army* enemy);
// 补充士兵
void recruitSoldiers(int count);
// 显示信息
void showInfo() const;
};
// 游戏类
class Game {
private:
vector<General*> allGenerals; // 所有武将
vector<City*> allCities; // 所有城池
vector<Force*> allForces; // 所有势力
Force* playerForce; // 玩家势力
int gameHour; // 游戏时间(小时)
bool isRunning; // 游戏是否运行
public:
Game() : gameHour(0), isRunning(true) {
// 初始化随机数种子
gen.seed(time(0));
// 创建武将
createGenerals();
// 创建城池
createCities();
// 创建势力
createForces();
}
~Game() {
// 释放内存
for (auto g : allGenerals) delete g;
for (auto c : allCities) delete c;
for (auto f : allForces) delete f;
}
// 获取属性
vector<General*> getAllGenerals() const { return allGenerals; }
vector<City*> getAllCities() const { return allCities; }
vector<Force*> getAllForces() const { return allForces; }
Force* getPlayerForce() const { return playerForce; }
int getGameHour() const { return gameHour; }
bool getIsRunning() const { return isRunning; }
// 设置属性
void setIsRunning(bool running) { isRunning = running; }
// 创建武将
void createGenerals();
// 创建城池
void createCities();
// 创建势力
void createForces();
// 找到两个城池之间的路径(简化版)
vector<City*> findPath(City* from, City* to);
// 检查游戏是否结束
bool checkGameOver();
// 时间推进
void advanceTime();
// 显示游戏状态
void showStatus() const;
// 显示主菜单
void showMainMenu() const;
// 玩家行动
void playerAction();
// 显示玩家城池
void showPlayerCities() const;
// 显示玩家部队
void showPlayerArmies() const;
// 移动部队菜单
void moveArmyMenu(Army* army) const;
// 管理部队武将
void manageArmyGenerals(Army* army) const;
// 用部队攻击
void attackWithArmy(Army* army) const;
// 显示玩家武将
void showPlayerGenerals() const;
// 招募武将菜单
void recruitGeneralMenu() const;
// 管理部队菜单
void manageArmiesMenu() const;
// 显示其他势力
void showOtherForces() const;
// 显示地图
void showMap() const;
// 外交菜单
void diplomacyMenu() const;
// 运行游戏
void run();
};
// 实现City类的成员函数
void City::addArmy(Army* army) {
armies.push_back(army);
army->setCurrentCity(this);
}
void City::removeArmy(Army* army) {
auto it = find(armies.begin(), armies.end(), army);
if (it != armies.end()) {
armies.erase(it);
}
}
int City::calculateDefensePower() const {
int power = defense * 10;
for (const auto& army : armies) {
power += army->calculateCombatPower() * 1.2; // 守城有加成
}
return power;
}
void City::growResources() {
population += randomInt(50, 200);
food += population / 10;
money += population / 20;
// 随机事件影响
if (randomInt(0, 9) < 2) { // 20%概率发生随机事件
int event = randomInt(0, 3);
switch (event) {
case 0: // 丰收
food += food * 0.2;
cout << name << "获得丰收,粮食增加20%!" << endl;
break;
case 1: // 灾害
food -= food * 0.15;
population -= population * 0.1;
cout << name << "遭遇灾害,粮食和人口减少!" << endl;
break;
case 2: // 商贸繁荣
money += money * 0.25;
cout << name << "商贸繁荣,金钱增加25%!" << endl;
break;
case 3: // 盗贼横行
money -= money * 0.2;
cout << name << "盗贼横行,金钱损失20%!" << endl;
break;
}
}
// 确保资源不为负
setFood(food);
setPopulation(population);
setMoney(money);
}
void City::showInfo() const {
cout << "城池: " << name << " (" << location << ")" << (isCapital ? " [首都]" : "") << endl;
cout << " 所属势力: " << (owner ? owner->getName() : "无") << endl;
cout << " 人口: " << population << endl;
cout << " 粮食: " << food << endl;
cout << " 金钱: " << money << endl;
cout << " 防御力: " << defense << endl;
cout << " 守城战斗力: " << calculateDefensePower() << endl;
cout << " 驻留部队: " << armies.size() << "支" << endl;
}
// 实现Force类的成员函数
void Force::addCity(City* city) {
if (find(cities.begin(), cities.end(), city) == cities.end()) {
cities.push_back(city);
city->setOwner(this);
}
}
void Force::removeCity(City* city) {
auto it = find(cities.begin(), cities.end(), city);
if (it != cities.end()) {
cities.erase(it);
}
}
Army* Force::createArmy(string name, City* location) {
Army* newArmy = new Army(name, this, location);
armies.push_back(newArmy);
location->addArmy(newArmy);
return newArmy;
}
void Force::removeArmy(Army* army) {
auto it = find(armies.begin(), armies.end(), army);
if (it != armies.end()) {
army->getCurrentCity()->removeArmy(army);
armies.erase(it);
delete army;
}
}
bool Force::recruitGeneral(General* general) {
if (general->getIsRecruited()) {
cout << general->getName() << "已被其他势力招募!" << endl;
return false;
}
// 招募成功率基于声望和武将忠诚度
double successRate = (double)reputation / 100.0 * (100.0 - (double)general->getLoyalty() / 2) / 100.0;
successRate = max(0.1, min(0.9, successRate)); // 成功率在10%-90%之间
if (randomDouble(0, 1) <= successRate) {
generals.push_back(general);
general->setRecruited(true);
cout << "成功招募武将: " << general->getName() << "!" << endl;
setReputation(reputation + 5);
return true;
} else {
cout << "招募" << general->getName() << "失败!" << endl;
general->setLoyalty(general->getLoyalty() - 10); // 降低忠诚度,下次更容易招募
return false;
}
}
bool Force::dismissGeneral(General* general) {
auto it = find(generals.begin(), generals.end(), general);
if (it != generals.end()) {
generals.erase(it);
general->setRecruited(false);
general->setLoyalty(50); // 重置忠诚度
cout << "已解雇武将: " << general->getName() << endl;
setReputation(reputation - 3);
return true;
}
return false;
}
int Force::calculatePower() const {
int power = 0;
// 城池贡献
for (const auto& city : cities) {
power += city->getPopulation() / 10;
power += city->getDefense() * 5;
}
// 部队贡献
for (const auto& army : armies) {
power += army->calculateCombatPower() / 5;
}
// 武将贡献
for (const auto& general : generals) {
power += (general->getAttack() + general->getDefense() +
general->getIntelligence() + general->getLeadership()) * 10;
}
return power;
}
int Force::getTotalFood() const {
int total = 0;
for (const auto& city : cities) {
total += city->getFood();
}
return total;
}
int Force::getTotalMoney() const {
int total = 0;
for (const auto& city : cities) {
total += city->getMoney();
}
return total;
}
int Force::getTotalPopulation() const {
int total = 0;
for (const auto& city : cities) {
total += city->getPopulation();
}
return total;
}
bool Force::makeAlliance(Force* other) {
if (other == this) return false;
// 检查是否已是盟友
if (find(allies.begin(), allies.end(), other) != allies.end()) {
return false;
}
// 检查是否是敌人
auto enemyIt = find(enemies.begin(), enemies.end(), other);
if (enemyIt != enemies.end()) {
enemies.erase(enemyIt);
}
allies.push_back(other);
other->allies.push_back(this);
cout << name << "与" << other->name << "结成同盟!" << endl;
return true;
}
bool Force::declareWar(Force* other) {
if (other == this) return false;
// 检查是否已是敌人
if (find(enemies.begin(), enemies.end(), other) != enemies.end()) {
return false;
}
// 检查是否是盟友
auto allyIt = find(allies.begin(), allies.end(), other);
if (allyIt != allies.end()) {
allies.erase(allyIt);
}
enemies.push_back(other);
other->enemies.push_back(this);
cout << name << "向" << other->name << "宣战!" << endl;
return true;
}
bool Force::submitTo(Force* overlord) {
if (!isIndependent || overlord == this) return false;
isIndependent = false;
makeAlliance(overlord);
cout << name << "已投靠" << overlord->name << "!" << endl;
return true;
}
bool Force::declareIndependence() {
if (isIndependent) return false;
isIndependent = true;
cout << name << "宣布独立!" << endl;
return true;
}
void Force::showInfo() const {
cout << "势力: " << name << (isPlayer ? " [玩家]" : "") << (isIndependent ? " [独立]" : " [附属]") << endl;
cout << " 声望: " << reputation << endl;
cout << " 总实力: " << calculatePower() << endl;
cout << " 拥有城池: " << cities.size() << "座" << endl;
cout << " 拥有部队: " << armies.size() << "支" << endl;
cout << " 拥有武将: " << generals.size() << "名" << endl;
cout << " 总资源: 粮食=" << getTotalFood() << ", 金钱=" << getTotalMoney()
<< ", 人口=" << getTotalPopulation() << endl;
cout << " 盟友: " << allies.size() << "个" << endl;
cout << " 敌人: " << enemies.size() << "个" << endl;
}
// 实现Army类的成员函数
bool Army::addGeneral(General* g) {
if (find(generals.begin(), generals.end(), g) != generals.end()) {
return false; // 已存在该武将
}
generals.push_back(g);
return true;
}
bool Army::removeGeneral(General* g) {
auto it = find(generals.begin(), generals.end(), g);
if (it != generals.end()) {
generals.erase(it);
return true;
}
return false;
}
int Army::calculateCombatPower() const {
if (soldierCount <= 0) return 0;
int power = soldierCount;
for (const auto& g : generals) {
power += g->getAttack() * 10;
power += g->getDefense() * 5;
power += g->getLeadership() * 8;
}
// 士气影响
power = power * morale / 100;
return power;
}
bool Army::moveTo(City* targetCity) {
if (currentCity == targetCity) {
cout << "已经在" << targetCity->getName() << "了!" << endl;
return false;
}
cout << name << "开始向" << targetCity->getName() << "进军..." << endl;
// 移动需要消耗粮食
int foodCost = soldierCount / 10;
if (owner->getTotalFood() < foodCost) {
cout << "粮食不足,无法移动!" << endl;
return false;
}
// 从当前城市扣除粮食
currentCity->setFood(currentCity->getFood() - foodCost);
// 移动过程中可能遭遇战斗或损耗
if (randomInt(0, 4) == 0) { // 20%概率发生事件
int event = randomInt(0, 2);
switch (event) {
case 0: // 遭遇小股敌人
{
int enemyPower = randomInt(calculateCombatPower() / 4, calculateCombatPower() / 2);
int loss = soldierCount * randomInt(5, 15) / 100;
setSoldierCount(soldierCount - loss);
setMorale(morale - randomInt(5, 15));
cout << "遭遇小股敌人袭击,损失" << loss << "名士兵!" << endl;
}
break;
case 1: // 物资损耗
{
int loss = soldierCount * randomInt(3, 10) / 100;
setSoldierCount(soldierCount - loss);
cout << "行军途中物资损耗,损失" << loss << "名士兵!" << endl;
}
break;
case 2: // 士气低落
setMorale(morale - randomInt(10, 25));
cout << "行军过久,士气低落!" << endl;
break;
}
}
// 到达目的地
currentCity->removeArmy(this);
targetCity->addArmy(this);
currentCity = targetCity;
cout << name << "已到达" << targetCity->getName() << "!" << endl;
return true;
}
bool Army::attackArmy(Army* enemy) {
if (enemy->getOwner() == owner) {
cout << "不能攻击己方部队!" << endl;
return false;
}
// 检查是否处于战争状态
bool isEnemy = find(owner->getEnemies().begin(), owner->getEnemies().end(), enemy->getOwner()) != owner->getEnemies().end();
if (!isEnemy) {
cout << "与" << enemy->getOwner()->getName() << "并非敌对状态,无法攻击!" << endl;
return false;
}
cout << "\n===== " << name << " 攻击 " << enemy->getName() << " =====" << endl;
int ourPower = calculateCombatPower();
int enemyPower = enemy->calculateCombatPower();
cout << "我方战斗力: " << ourPower << endl;
cout << "敌方战斗力: " << enemyPower << endl;
// 战斗模拟
double ourFactor = ourPower * randomDouble(0.8, 1.2);
double enemyFactor = enemyPower * randomDouble(0.8, 1.2);
int ourLoss, enemyLoss;
if (ourFactor > enemyFactor) {
// 我方胜利
ourLoss = soldierCount * (enemyFactor / (ourFactor + enemyFactor)) * randomDouble(0.3, 0.6);
enemyLoss = enemy->getSoldierCount() * (ourFactor / (ourFactor + enemyFactor)) * randomDouble(0.5, 0.8);
cout << "我方胜利!" << endl;
setMorale(morale + randomInt(5, 20));
} else if (ourFactor < enemyFactor) {
// 敌方胜利
ourLoss = soldierCount * (enemyFactor / (ourFactor + enemyFactor)) * randomDouble(0.5, 0.8);
enemyLoss = enemy->getSoldierCount() * (ourFactor / (ourFactor + enemyFactor)) * randomDouble(0.3, 0.6);
cout << "我方失败!" << endl;
setMorale(morale - randomInt(10, 30));
} else {
// 平局
ourLoss = soldierCount * randomDouble(0.3, 0.5);
enemyLoss = enemy->getSoldierCount() * randomDouble(0.3, 0.5);
cout << "双方打成平局!" << endl;
}
// 应用损失
setSoldierCount(soldierCount - ourLoss);
enemy->setSoldierCount(enemy->getSoldierCount() - enemyLoss);
cout << "我方损失: " << ourLoss << "名士兵" << endl;
cout << "敌方损失: " << enemyLoss << "名士兵" << endl;
// 检查敌方部队是否被消灭
if (enemy->getSoldierCount() <= 0) {
cout << enemy->getName() << "被消灭了!" << endl;
enemy->getOwner()->removeArmy(enemy);
}
// 检查我方部队是否被消灭
if (soldierCount <= 0) {
cout << name << "被消灭了!" << endl;
owner->removeArmy(this);
return false;
}
// 如果在敌方城池且我方胜利,尝试占领城池
if (ourFactor > enemyFactor && currentCity->getOwner() == enemy->getOwner()) {
// 检查城中是否还有其他敌方部队
bool hasEnemy = false;
for (auto army : currentCity->getArmies()) {
if (army->getOwner() == enemy->getOwner()) {
hasEnemy = true;
break;
}
}
if (!hasEnemy) {
// 占领城池
Force* oldOwner = currentCity->getOwner();
if (oldOwner) {
oldOwner->removeCity(currentCity);
}
owner->addCity(currentCity);
cout << "成功占领" << currentCity->getName() << "!" << endl;
}
}
return true;
}
void Army::recruitSoldiers(int count) {
if (currentCity && currentCity->getOwner() == owner) {
int maxRecruit = currentCity->getPopulation() / 10;
count = min(count, maxRecruit);
soldierCount += count;
currentCity->setPopulation(currentCity->getPopulation() - count);
cout << "成功招募" << count << "名士兵!" << endl;
} else {
cout << "无法招募士兵,不在自己的城池中!" << endl;
}
}
void Army::showInfo() const {
cout << "部队: " << name << endl;
cout << " 所属势力: " << owner->getName() << endl;
cout << " 当前位置: " << currentCity->getName() << endl;
cout << " 士兵数量: " << soldierCount << endl;
cout << " 士气: " << morale << endl;
cout << " 战斗力: " << calculateCombatPower() << endl;
cout << " 武将列表: ";
if (generals.empty()) {
cout << "无";
} else {
for (size_t i = 0; i < generals.size(); ++i) {
if (i > 0) cout << ", ";
cout << generals[i]->getName();
}
}
cout << endl;
}
// 实现Game类的成员函数
void Game::createGenerals() {
// 三国著名武将
allGenerals.push_back(new General("曹操", 85, 80, 90, 95));
allGenerals.push_back(new General("刘备", 70, 75, 85, 90));
allGenerals.push_back(new General("孙权", 75, 70, 80, 85));
allGenerals.push_back(new General("诸葛亮", 60, 70, 99, 90));
allGenerals.push_back(new General("关羽", 95, 85, 70, 85));
allGenerals.push_back(new General("张飞", 95, 80, 50, 75));
allGenerals.push_back(new General("赵云", 90, 85, 75, 85));
allGenerals.push_back(new General("吕布", 100, 80, 40, 60));
allGenerals.push_back(new General("周瑜", 80, 75, 90, 85));
allGenerals.push_back(new General("司马懿", 80, 85, 95, 90));
allGenerals.push_back(new General("黄忠", 90, 70, 65, 70));
allGenerals.push_back(new General("马超", 95, 75, 60, 75));
allGenerals.push_back(new General("许褚", 90, 85, 50, 80));
allGenerals.push_back(new General("典韦", 95, 90, 40, 75));
allGenerals.push_back(new General("张辽", 85, 85, 75, 85));
allGenerals.push_back(new General("徐晃", 80, 85, 70, 80));
allGenerals.push_back(new General("甘宁", 85, 75, 65, 75));
allGenerals.push_back(new General("太史慈", 85, 75, 70, 80));
allGenerals.push_back(new General("吕蒙", 75, 80, 85, 80));
allGenerals.push_back(new General("陆逊", 70, 75, 90, 85));
}
void Game::createCities() {
allCities.push_back(new City("洛阳", "中原", true)); // 首都
allCities.push_back(new City("长安", "关中"));
allCities.push_back(new City("许昌", "中原"));
allCities.push_back(new City("邺城", "河北"));
allCities.push_back(new City("成都", "西川", true)); // 首都
allCities.push_back(new City("建业", "江东", true)); // 首都
allCities.push_back(new City("襄阳", "荆州"));
allCities.push_back(new City("江陵", "荆州"));
allCities.push_back(new City("汉中", "汉中"));
allCities.push_back(new City("西凉", "凉州"));
allCities.push_back(new City("蓟城", "幽州"));
allCities.push_back(new City("下邳", "徐州"));
allCities.push_back(new City("寿春", "淮南"));
allCities.push_back(new City("长沙", "荆州"));
allCities.push_back(new City("桂阳", "荆州"));
}
void Game::createForces() {
// 创建玩家势力
playerForce = new Force("玩家势力", true);
allForces.push_back(playerForce);
// 分配一座城池给玩家
playerForce->addCity(allCities[randomInt(0, allCities.size() - 1)]);
// 创建AI势力
allForces.push_back(new Force("魏国"));
allForces.push_back(new Force("蜀国"));
allForces.push_back(new Force("吴国"));
allForces.push_back(new Force("袁绍军"));
allForces.push_back(new Force("袁术军"));
allForces.push_back(new Force("刘表军"));
allForces.push_back(new Force("刘璋军"));
// 为AI势力分配初始城池
for (size_t i = 1; i < allForces.size(); ++i) {
// 寻找无主城池
for (auto city : allCities) {
if (!city->getOwner()) {
allForces[i]->addCity(city);
break;
}
}
// 为AI势力创建初始部队
if (!allForces[i]->getCities().empty()) {
allForces[i]->createArmy(allForces[i]->getName() + "第一军",
allForces[i]->getCities()[0]);
}
// AI势力初始招募1-3名武将
int generalCount = randomInt(1, 3);
for (int j = 0; j < generalCount; ++j) {
// 随机选择未被招募的武将
vector<General*> available;
for (auto g : allGenerals) {
if (!g->getIsRecruited()) {
available.push_back(g);
}
}
if (!available.empty()) {
allForces[i]->recruitGeneral(available[randomInt(0, available.size() - 1)]);
}
}
}
}
vector<City*> Game::findPath(City* from, City* to) {
// 简化实现,直接返回包含目标的路径
vector<City*> path;
path.push_back(to);
return path;
}
bool Game::checkGameOver() {
// 玩家没有城池了
if (playerForce->getCities().empty()) {
cout << "你的所有城池都被占领了,游戏结束!" << endl;
return true;
}
// 玩家统一了全国
if (playerForce->getCities().size() == allCities.size()) {
cout << "恭喜你统一了全国,游戏胜利!" << endl;
return true;
}
return false;
}
void Game::advanceTime() {
gameHour++;
// 每6小时,资源增长一次
if (gameHour % 6 == 0) {
for (auto city : allCities) {
city->growResources();
}
}
// 每天(24小时),AI行动一次,敌人变强
if (gameHour % 48 == 0) {
cout << "\n===== 新的一天开始了 =====" << endl;
// AI势力行动
for (auto force : allForces) {
if (!force->getIsPlayer()) {
force->aiAction(this);
}
}
// 随着时间推移,敌人变强
int days = gameHour / 24;
if (days % 9 == 0 && days > 0) { // 每3天
for (auto force : allForces) {
if (!force->getIsPlayer()) {
// 提升AI势力的武将
for (auto general : force->getGenerals()) {
general->upgrade();
}
// 增加AI势力的声望
force->setReputation(force->getReputation() + randomInt(1, 3));
cout << force->getName() << "的实力增强了!" << endl;
}
}
}
}
}
void Game::showStatus() const {
cout << "\n===== " << getTimeString(gameHour) << " =====" << endl;
playerForce->showInfo();
}
void Game::showMainMenu() const {
cout << "\n===== 主菜单 =====" << endl;
cout << "1. 查看我的城池" << endl;
cout << "2. 查看我的部队" << endl;
cout << "3. 查看我的武将" << endl;
cout << "4. 招募武将" << endl;
cout << "5. 管理部队" << endl;
cout << "6. 查看其他势力" << endl;
cout << "7. 查看地图" << endl;
cout << "8. 外交行动" << endl;
cout << "9. 结束回合" << endl;
cout << "0. 退出游戏" << endl;
cout << "请选择: ";
}
void Game::playerAction() {
int choice;
cin >> choice;
// 清除输入缓冲区
cin.ignore(numeric_limits<streamsize>::max(), '\n');
switch (choice) {
case 1:
showPlayerCities();
break;
case 2:
showPlayerArmies();
break;
case 3:
showPlayerGenerals();
break;
case 4:
recruitGeneralMenu();
break;
case 5:
manageArmiesMenu();
break;
case 6:
showOtherForces();
break;
case 7:
showMap();
break;
case 8:
diplomacyMenu();
break;
case 9:
cout << "结束本回合..." << endl;
advanceTime();
break;
case 0:
cout << "退出游戏..." << endl;
isRunning = false;
break;
default:
cout << "无效的选择,请重试!" << endl;
}
}
void Game::showPlayerCities() const {
cout << "\n===== 我的城池 =====" << endl;
const auto& cities = playerForce->getCities();
if (cities.empty()) {
cout << "你没有任何城池!" << endl;
return;
}
for (size_t i = 0; i < cities.size(); ++i) {
cout << i + 1 << ". ";
cities[i]->showInfo();
cout << endl;
}
cout << "输入城池编号查看详情(0返回): ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)cities.size()) {
cities[choice - 1]->showInfo();
// 城池管理菜单
cout << "\n1. 招募士兵" << endl;
cout << "2. 修筑防御" << endl;
cout << "3. 创建部队" << endl;
cout << "0. 返回" << endl;
cout << "请选择: ";
int action;
cin >> action;
switch (action) {
case 1: {
cout << "输入招募数量: ";
int count;
cin >> count;
if (!playerForce->getArmies().empty()) {
playerForce->getArmies()[0]->recruitSoldiers(count);
} else {
cout << "你没有部队可以招募士兵!" << endl;
}
break;
}
case 2: {
int cost = 500;
if (playerForce->getTotalMoney() >= cost) {
cities[choice - 1]->setDefense(cities[choice - 1]->getDefense() + 10);
// 从城中扣除金钱
cities[choice - 1]->setMoney(cities[choice - 1]->getMoney() - cost);
cout << "成功修筑防御,防御力+10,花费" << cost << "金钱!" << endl;
} else {
cout << "金钱不足,无法修筑防御!" << endl;
}
break;
}
case 3: {
cout << "输入新部队名称: ";
string name;
cin.ignore();
getline(cin, name);
int cost = 1000;
if (playerForce->getTotalMoney() >= cost) {
playerForce->createArmy(name, cities[choice - 1]);
cities[choice - 1]->setMoney(cities[choice - 1]->getMoney() - cost);
cout << "成功创建部队" << name << ",花费" << cost << "金钱!" << endl;
} else {
cout << "金钱不足,无法创建部队!" << endl;
}
break;
}
}
}
}
void Game::showPlayerArmies() const {
cout << "\n===== 我的部队 =====" << endl;
const auto& armies = playerForce->getArmies();
if (armies.empty()) {
cout << "你没有任何部队!" << endl;
return;
}
for (size_t i = 0; i < armies.size(); ++i) {
cout << i + 1 << ". ";
armies[i]->showInfo();
cout << endl;
}
cout << "输入部队编号进行操作(0返回): ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)armies.size()) {
Army* selected = armies[choice - 1];
cout << "\n1. 移动部队" << endl;
cout << "2. 调整武将" << endl;
cout << "3. 攻击目标" << endl;
cout << "4. 解散部队" << endl;
cout << "0. 返回" << endl;
cout << "请选择: ";
int action;
cin >> action;
switch (action) {
case 1:
moveArmyMenu(selected);
break;
case 2:
manageArmyGenerals(selected);
break;
case 3:
attackWithArmy(selected);
break;
case 4:
playerForce->removeArmy(selected);
cout << "部队已解散!" << endl;
break;
}
}
}
void Game::moveArmyMenu(Army* army) const {
cout << "\n当前位置: " << army->getCurrentCity()->getName() << endl;
cout << "可移动到的城池:" << endl;
vector<City*> possibleTargets;
for (auto city : allCities) {
if (city != army->getCurrentCity()) {
possibleTargets.push_back(city);
}
}
for (size_t i = 0; i < possibleTargets.size(); ++i) {
cout << i + 1 << ". " << possibleTargets[i]->getName()
<< " (所有者: " << (possibleTargets[i]->getOwner() ? possibleTargets[i]->getOwner()->getName() : "无") << ")" << endl;
}
cout << "输入目标城池编号(0返回): ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)possibleTargets.size()) {
army->moveTo(possibleTargets[choice - 1]);
}
}
void Game::manageArmyGenerals(Army* army) const {
cout << "\n部队当前武将:" << endl;
const auto& generals = army->getGenerals();
for (size_t i = 0; i < generals.size(); ++i) {
cout << i + 1 << ". " << generals[i]->getName() << endl;
}
cout << "\n1. 添加武将" << endl;
cout << "2. 移除武将" << endl;
cout << "0. 返回" << endl;
cout << "请选择: ";
int action;
cin >> action;
if (action == 1) {
// 添加武将
vector<General*> available;
for (auto g : playerForce->getGenerals()) {
if (find(generals.begin(), generals.end(), g) == generals.end()) {
available.push_back(g);
}
}
if (available.empty()) {
cout << "没有可添加的武将!" << endl;
return;
}
cout << "可选武将:" << endl;
for (size_t i = 0; i < available.size(); ++i) {
cout << i + 1 << ". " << available[i]->getName() << endl;
}
cout << "输入要添加的武将编号: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)available.size()) {
army->addGeneral(available[choice - 1]);
cout << "已添加武将" << available[choice - 1]->getName() << endl;
}
} else if (action == 2) {
// 移除武将
if (generals.empty()) {
cout << "部队中没有武将!" << endl;
return;
}
cout << "输入要移除的武将编号: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)generals.size()) {
army->removeGeneral(generals[choice - 1]);
cout << "已移除武将" << generals[choice - 1]->getName() << endl;
}
}
}
void Game::attackWithArmy(Army* army) const {
City* currentCity = army->getCurrentCity();
// 攻击城中的敌方部队
vector<Army*> enemyArmies;
for (auto a : currentCity->getArmies()) {
if (a->getOwner() != army->getOwner() &&
find(army->getOwner()->getEnemies().begin(),
army->getOwner()->getEnemies().end(),
a->getOwner()) != army->getOwner()->getEnemies().end()) {
enemyArmies.push_back(a);
}
}
// 如果是自己的城池,不能攻击
if (currentCity->getOwner() == army->getOwner()) {
cout << "不能在自己的城池中发动攻击!" << endl;
return;
}
// 如果城中没有敌人,就是攻城
if (enemyArmies.empty() && currentCity->getOwner() != army->getOwner()) {
cout << "是否要攻击" << currentCity->getName() << "? (1=是, 0=否): ";
int choice;
cin >> choice;
if (choice == 1) {
// 攻城战
int attackPower = army->calculateCombatPower();
int defensePower = currentCity->calculateDefensePower();
cout << "开始攻城!" << endl;
cout << army->getName() << " 攻击力: " << attackPower << endl;
cout << currentCity->getName() << " 防御力: " << defensePower << endl;
if (attackPower > defensePower * randomDouble(0.8, 1.2)) {
// 攻城成功
Force* oldOwner = currentCity->getOwner();
if (oldOwner) {
oldOwner->removeCity(currentCity);
}
army->getOwner()->addCity(currentCity);
cout << "攻城成功! " << currentCity->getName() << "已被你占领!" << endl;
// 部队损失
int loss = army->getSoldierCount() * randomInt(20, 40) / 100;
army->setSoldierCount(army->getSoldierCount() - loss);
cout << army->getName() << "损失了" << loss << "名士兵!" << endl;
// 降低士气
army->setMorale(army->getMorale() - randomInt(10, 30));
} else {
// 攻城失败
int loss = army->getSoldierCount() * randomInt(30, 60) / 100;
army->setSoldierCount(army->getSoldierCount() - loss);
cout << "攻城失败! " << army->getName() << "损失了" << loss << "名士兵!" << endl;
// 大幅降低士气
army->setMorale(army->getMorale() - randomInt(30, 50));
}
}
} else if (!enemyArmies.empty()) {
// 选择要攻击的敌方部队
cout << "选择要攻击的敌方部队:" << endl;
for (size_t i = 0; i < enemyArmies.size(); ++i) {
cout << i + 1 << ". " << enemyArmies[i]->getName()
<< " (士兵: " << enemyArmies[i]->getSoldierCount() << ")" << endl;
}
cout << "输入部队编号: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)enemyArmies.size()) {
army->attackArmy(enemyArmies[choice - 1]);
}
} else {
cout << "没有可攻击的目标!" << endl;
}
}
void Game::showPlayerGenerals() const {
cout << "\n===== 我的武将 =====" << endl;
const auto& generals = playerForce->getGenerals();
if (generals.empty()) {
cout << "你没有任何武将!" << endl;
return;
}
for (size_t i = 0; i < generals.size(); ++i) {
cout << i + 1 << ". ";
generals[i]->showInfo();
cout << endl;
}
cout << "输入武将编号查看详情(0返回): ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)generals.size()) {
generals[choice - 1]->showInfo();
cout << "\n1. 解雇武将" << endl;
cout << "0. 返回" << endl;
cout << "请选择: ";
int action;
cin >> action;
if (action == 1) {
playerForce->dismissGeneral(generals[choice - 1]);
}
}
}
void Game::recruitGeneralMenu() const {
cout << "\n===== 招募武将 =====" << endl;
vector<General*> available;
for (auto g : allGenerals) {
if (!g->getIsRecruited()) {
available.push_back(g);
}
}
if (available.empty()) {
cout << "没有可招募的武将了!" << endl;
return;
}
for (size_t i = 0; i < available.size(); ++i) {
cout << i + 1 << ". ";
available[i]->showInfo();
cout << endl;
}
cout << "输入要招募的武将编号(0返回): ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)available.size()) {
int cost = 1000 + (100 - available[choice - 1]->getLoyalty()) * 20;
cout << "招募" << available[choice - 1]->getName() << "需要花费" << cost << "金钱" << endl;
cout << "是否确认招募? (1=是, 0=否): ";
int confirm;
cin >> confirm;
if (confirm == 1) {
if (playerForce->getTotalMoney() >= cost) {
// 从首都扣除金钱
if (!playerForce->getCities().empty()) {
playerForce->getCities()[0]->setMoney(playerForce->getCities()[0]->getMoney() - cost);
playerForce->recruitGeneral(available[choice - 1]);
} else {
cout << "你没有城池,无法招募武将!" << endl;
}
} else {
cout << "金钱不足,无法招募!" << endl;
}
}
}
}
void Game::manageArmiesMenu() const {
cout << "\n===== 部队管理 =====" << endl;
cout << "1. 合并部队" << endl;
cout << "2. 拆分部队" << endl;
cout << "0. 返回" << endl;
cout << "请选择: ";
int choice;
cin >> choice;
if (choice == 1 && playerForce->getArmies().size() >= 2) {
// 合并部队
const auto& armies = playerForce->getArmies();
cout << "选择要合并的第一支部队:" << endl;
for (size_t i = 0; i < armies.size(); ++i) {
cout << i + 1 << ". " << armies[i]->getName() << endl;
}
cout << "输入编号: ";
int a;
cin >> a;
cout << "选择要合并的第二支部队:" << endl;
for (size_t i = 0; i < armies.size(); ++i) {
if (i + 1 != a) {
cout << i + 1 << ". " << armies[i]->getName() << endl;
}
}
cout << "输入编号: ";
int b;
cin >> b;
if (a != b && a > 0 && a <= (int)armies.size() && b > 0 && b <= (int)armies.size()) {
Army* army1 = armies[a - 1];
Army* army2 = armies[b - 1];
// 只有在同一城池才能合并
if (army1->getCurrentCity() == army2->getCurrentCity()) {
// 合并士兵
army1->setSoldierCount(army1->getSoldierCount() + army2->getSoldierCount());
// 合并武将
for (auto g : army2->getGenerals()) {
army1->addGeneral(g);
}
// 解散第二支部队
playerForce->removeArmy(army2);
cout << "部队合并成功!" << endl;
} else {
cout << "只有同一城池的部队才能合并!" << endl;
}
}
} else if (choice == 2 && !playerForce->getArmies().empty()) {
// 拆分部队
const auto& armies = playerForce->getArmies();
cout << "选择要拆分的部队:" << endl;
for (size_t i = 0; i < armies.size(); ++i) {
cout << i + 1 << ". " << armies[i]->getName() << endl;
}
cout << "输入编号: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)armies.size()) {
Army* original = armies[choice - 1];
if (original->getSoldierCount() < 500) {
cout << "部队人数太少,无法拆分!" << endl;
return;
}
cout << "输入新部队名称: ";
string name;
cin.ignore();
getline(cin, name);
// 创建新部队
Army* newArmy = playerForce->createArmy(name, original->getCurrentCity());
// 分配一半士兵
int half = original->getSoldierCount() / 2;
newArmy->setSoldierCount(half);
original->setSoldierCount(original->getSoldierCount() - half);
cout << "部队拆分成功!" << "部队拆分成功!" << endl;
}
}
}
void Game::showOtherForces() const {
cout << "\n===== 其他势力 =====" << endl;
for (size_t i = 0; i < allForces.size(); ++i) {
if (!allForces[i]->getIsPlayer()) {
cout << i + 1 << ". " << allForces[i]->getName()
<< " (实力: " << allForces[i]->calculatePower()
<< ", 城池: " << allForces[i]->getCities().size() << ")" << endl;
}
}
cout << "输入势力编号查看详情(0返回): ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)allForces.size()) {
Force* selected = allForces[choice - 1];
if (!selected->getIsPlayer()) {
selected->showInfo();
}
}
}
void Game::showMap() const {
cout << "\n===== 地图 =====" << endl;
for (auto city : allCities) {
cout << city->getName() << " (" << city->getLocation() << "): "
<< (city->getOwner() ? city->getOwner()->getName() : "无主") << endl;
}
}
void Game::diplomacyMenu() const {
cout << "\n===== 外交行动 =====" << endl;
cout << "选择目标势力:" << endl;
for (size_t i = 0; i < allForces.size(); ++i) {
if (!allForces[i]->getIsPlayer()) {
cout << i + 1 << ". " << allForces[i]->getName() << endl;
}
}
cout << "输入势力编号(0返回): ";
int choice;
cin >> choice;
if (choice > 0 && choice <= (int)allForces.size()) {
Force* target = allForces[choice - 1];
if (!target->getIsPlayer()) {
cout << "\n1. 结盟" << endl;
cout << "2. 宣战" << endl;
cout << "3. 投靠" << endl;
cout << "4. 要求臣服" << endl;
cout << "0. 返回" << endl;
cout << "请选择: ";
int action;
cin >> action;
switch (action) {
case 1:
playerForce->makeAlliance(target);
break;
case 2:
playerForce->declareWar(target);
break;
case 3:
playerForce->submitTo(target);
break;
case 4:
// 成功率基于双方实力差距
if (playerForce->calculatePower() > target->calculatePower() * 2) {
cout << target->getName() << "同意臣服于你!" << endl;
target->submitTo(playerForce);
} else {
cout << target->getName() << "拒绝臣服,两国关系恶化!" << endl;
playerForce->declareWar(target);
}
break;
}
}
}
}
void Game::run() {
cout << "===== 欢迎来到率土之滨简化版 =====" << endl;
cout << "在这个游戏中,你将扮演一方诸侯,招募武将,训练军队,攻占城池," << endl;
cout << "与其他势力周旋,最终统一全国!" << endl;
while (isRunning) {
showStatus();
showMainMenu();
playerAction();
if (checkGameOver()) {
isRunning = false;
}
}
cout << "游戏结束,感谢游玩!" << endl;
}
// 实现Force类的aiAction方法
void Force::aiAction(Game* game) {
// AI行动逻辑
if (cities.empty()) {
return; // 没有城池的势力不行动
}
// 1. 资源管理 - 每3天尝试招募新部队
if (game->getGameHour() % (24 * 3) == 0) {
if (getTotalMoney() > 2000 && armies.size() < cities.size() * 2) {
City* randomCity = cities[randomInt(0, cities.size() - 1)];
createArmy(name + "新编" + to_string(armies.size() + 1) + "军", randomCity);
randomCity->setMoney(randomCity->getMoney() - 1000);
cout << name << "创建了新部队!" << endl;
}
}
// 2. 招募武将 - 每2天尝试一次
if (game->getGameHour() % (24 * 2) == 0) {
vector<General*> available;
for (auto g : game->getAllGenerals()) {
if (!g->getIsRecruited()) {
available.push_back(g);
}
}
if (!available.empty() && getTotalMoney() > 1500 && generals.size() < 5) {
General* target = available[randomInt(0, available.size() - 1)];
int cost = 1000 + (100 - target->getLoyalty()) * 20;
if (getTotalMoney() >= cost) {
cities[0]->setMoney(cities[0]->getMoney() - cost);
recruitGeneral(target);
}
}
}
// 3. 部队调动
if (!armies.empty() && randomInt(0, 1) == 0) { // 50%概率调动一支部队
Army* army = armies[randomInt(0, armies.size() - 1)];
City* currentCity = army->getCurrentCity();
// 寻找目标城池
vector<City*> possibleTargets;
// 优先攻击敌人的城池
for (auto enemy : enemies) {
for (auto city : enemy->getCities()) {
possibleTargets.push_back(city);
}
}
// 如果没有敌人城池,就移动到自己的其他城池
if (possibleTargets.empty()) {
for (auto city : game->getAllCities()) {
if (city->getOwner() == this && city != currentCity) {
possibleTargets.push_back(city);
}
}
}
// 如果没有自己的其他城池,就移动到无主城池
if (possibleTargets.empty()) {
for (auto city : game->getAllCities()) {
if (!city->getOwner() && city != currentCity) {
possibleTargets.push_back(city);
}
}
}
// 如果有可移动的目标
if (!possibleTargets.empty()) {
City* target = possibleTargets[randomInt(0, possibleTargets.size() - 1)];
army->moveTo(target);
// 如果到达敌方城池,尝试攻击
if (target->getOwner() != this &&
find(enemies.begin(), enemies.end(), target->getOwner()) != enemies.end()) {
// 检查城中是否有敌方部队
bool hasEnemyArmy = false;
for (auto a : target->getArmies()) {
if (a->getOwner() == target->getOwner()) {
hasEnemyArmy = true;
army->attackArmy(a);
break;
}
}
// 如果没有敌方部队,就攻城
if (!hasEnemyArmy) {
int attackPower = army->calculateCombatPower();
int defensePower = target->calculateDefensePower();
if (attackPower > defensePower * randomDouble(0.7, 1.1)) {
// 攻城成功
Force* oldOwner = target->getOwner();
if (oldOwner) {
oldOwner->removeCity(target);
}
addCity(target);
cout << name << "的" << army->getName() << "成功占领了" << target->getName() << "!" << endl;
}
}
}
}
}
// 4. 外交行动 - 随机与其他势力结盟或宣战
if (randomInt(0, 9) == 0) { // 10%概率进行外交行动
vector<Force*> otherForces;
for (auto f : game->getAllForces()) {
if (f != this) {
otherForces.push_back(f);
}
}
if (!otherForces.empty()) {
Force* target = otherForces[randomInt(0, otherForces.size() - 1)];
// 基于实力差距决定外交态度
double powerRatio = (double)calculatePower() / target->calculatePower();
if (powerRatio > 1.5 && randomInt(0, 1) == 0) {
// 实力远超对方,尝试让对方臣服
target->submitTo(this);
} else if (powerRatio < 0.7 && randomInt(0, 2) == 0) {
// 实力远逊于对方,尝试投靠
submitTo(target);
} else if (randomInt(0, 3) == 0) {
// 随机结盟
makeAlliance(target);
} else if (randomInt(0, 4) == 0) {
// 随机宣战
declareWar(target);
}
}
}
// 5. 提升城池防御
if (randomInt(0, 4) == 0 && !cities.empty()) { // 20%概率
City* city = cities[randomInt(0, cities.size() - 1)];
if (city->getMoney() >= 300) {
city->setDefense(city->getDefense() + 5);
city->setMoney(city->getMoney() - 300);
cout << name << "加固了" << city->getName() << "的防御!" << endl;
}
}
}
int main() {
// 设置中文字体支持
setlocale(LC_ALL, "zh_CN.UTF-8");
Game game;
game.run();
return 0;
}