C++#include <bits/stdc++.h>
#include <cstdio>
#include <ctime>
#include <random>
#include <windows.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int vis[205];
int renshu;
int current_round = 1;
int cli_team = 0;
int oponment;
int zhanshu;
int my_points, you_points;
// 玩家结构体
struct Player {
string name;
int ability;
string position;
bool is_injured;
};
// 城池结构体
struct City {
string name;
int wallLevel; // 城墙等级
int farmLevel; // 农田等级
int populationwuzi; // 农田等级
int population; // 人口
};
// 部队结构体
struct Team {
string name;
int ablity;
int point;
int goal, lose;
int train_remain;
int resource; // 资源(用于招募新兵、购买装备等)
vector<Player> players;
City city; // 城池
} team[205];
// 比较函数
bool cmp(Team a, Team b) {
if (a.point != b.point) return a.point > b.point;
if (a.goal - a.lose != b.goal - b.lose) return a.goal - a.lose > b.goal - b.lose;
if (a.goal != b.goal) return a.goal > b.goal;
return a.ablity > b.ablity;
}
// 打印排行榜
void print_board() {
Team temp_team[45];
for (int i = 1; i <= renshu; i++) {
temp_team[i] = team[i];
}
sort(temp_team + 1, temp_team + 1 + renshu, cmp);
cout << "排名 部队名称 积分 杀敌 死亡" << endl;
for (int i = 1; i <= renshu; i++) {
cout << i << ' ' << temp_team[i].name << ' ' << temp_team[i].point << ' ' << temp_team[i].goal << ' ' << temp_team[i].lose << ' ';
if (temp_team[i].name == team[cli_team].name) {
cout << "(我的部队)";
}
cout << endl;
}
system("pause");
system("cls");
}
// 随机事件
int yuan;
void random_event() {
yuan=team[cli_team].ablity;
int event = rand() % 10; // 增加更多事件
if (event == 0) {
cout << "糟糕!你的主力将领受伤了,战力值暂时下降!" << endl;
team[cli_team].ablity -= 2;
} else if (event == 1) {
cout << "民众们非常支持你的部队,士气高涨!战力值暂时提升!" << endl;
team[cli_team].ablity += 2;
} else if (event == 2) {
cout << "天气恶劣,战争受到影响,双方战力值下降!" << endl;
team[cli_team].ablity -= 1;
team[oponment].ablity -= 1;
} else if (event ==3) {
cout << "天降奇兵!一支友军加入战斗,战力值提升!" << endl;
team[cli_team].ablity += 3;
} else if (event ==4) {
cout << "敌军使用了火攻,战力值下降!" << endl;
team[cli_team].ablity -= 2;
}
}
// 选择战术
void choose_tactic() {
cout << "选择战术:" << endl;
cout << "(1): 进攻" << endl;
cout << "(2): 防守" << endl;
cout << "(3): 平衡" << endl;
cout << "(4): 奇袭" << endl;
cout << "(5): 固守" << endl;
int tactic;
cin >> tactic;
if (tactic == 1) {
cout << "你选择了进攻战术,杀敌概率增加,但死亡风险也增加!" << endl;
zhanshu = 1;
} else if (tactic == 2) {
cout << "你选择了防守战术,死亡概率降低,但杀敌数量减少!" << endl;
zhanshu = 2;
} else if (tactic == 3) {
cout << "你选择了平衡战术,攻守兼备!" << endl;
zhanshu = 3;
} else if (tactic == 4) {
cout << "你选择了奇袭战术,高风险高回报!" << endl;
zhanshu = 4;
} else if (tactic == 5) {
cout << "你选择了固守战术,完全放弃进攻,专注于防守!" << endl;
zhanshu = 5;
}
}
// 战斗动画
void play_battle_animation() {
srand(time(NULL));
cout << "战斗开始!" << endl;
int my = my_points, you = you_points;
cout << "双方开战了!" << endl;
Sleep(1000);
int wowin = 0, youwin = 0;
int i = 0;
while (i < 10) {
if (wowin == my && youwin == you) break;
cout << "第" << i << "回合" << endl;
cout << "比分" << wowin << ":" << youwin << endl;
i++;
if (zhanshu == 1) {
int zs = rand() % 10;
zs++;
if ((zs <= 3 || wowin == my) && youwin != you) {
cout << "这回合你选择防守" << endl;
cout << "他发起了进攻" << endl;
if (youwin < you) {
int u = rand() % 10;
u++;
if (u <= 4) {
cout << "他突破了你的防守!" << endl;
youwin++;
} else {
cout << "他没突破你的防守" << endl;
}
}else{
cout << "他没突破你的防守" << endl;
}
} else {
cout << "这回合你选择进攻" << endl;
cout << "他发起了防守" << endl;
if (youwin < you) {
int u = rand() % 10;
u++;
if (u <= 7) {
cout << "你突破了他的防守!" << endl;
wowin++;
} else {
cout << "你没突破他的防守" << endl;
}
}else{
cout << "你没突破他的防守" << endl;
}
}
}
if (zhanshu == 2) {
int zs = rand() % 10;
zs++;
if ((zs <= 7 || wowin == my) && youwin != you) {
cout << "这回合你选择防守" << endl;
cout << "他发起了进攻" << endl;
if (youwin < you) {
int u = rand() % 10;
u++;
if (u <= 3) {
cout << "他突破了你的防守!" << endl;
youwin++;
} else {
cout << "他没突破你的防守" << endl;
}
}else{
cout << "他没突破你的防守" << endl;
}
} else {
cout << "这回合你选择进攻" << endl;
cout << "他发起了防守" << endl;
if (youwin < you) {
int u = rand() % 10;
u++;
if (u <= 3) {
cout << "你突破了他的防守!" << endl;
wowin++;
} else {
cout << "你没突破他的防守" << endl;
}
}else{
cout << "你没突破他的防守" << endl;
}
}
}
if (zhanshu == 3) {
int zs = rand() % 10;
zs++;
if ((zs <= 5 || wowin == my) && youwin != you) {
cout << "这回合你选择防守" << endl;
cout << "他发起了进攻" << endl;
if (youwin < you) {
int u = rand() % 10;
u++;
if (u <= 5) {
cout << "他突破了你的防守!" << endl;
youwin++;
} else {
cout << "他没突破你的防守" << endl;
}
}
} else {
cout << "这回合你选择进攻" << endl;
cout << "他发起了防守" << endl;
int u = rand() % 10;
u++;
if (u <= 5) {
cout << "你突破了他的防守!" << endl;
wowin++;
} else {
cout << "你没突破他的防守" << endl;
}
}
}
if (zhanshu == 4) {
int zs = rand() % 10;
zs++;
if ((zs <= 1 || wowin == my) && youwin != you) {
cout << "这回合你选择防守" << endl;
cout << "他发起了进攻" << endl;
if (youwin < you) {
int u = rand() % 10;
u++;
if (u <= 3) {
cout << "他突破了你的防守!" << endl;
youwin++;
} else {
cout << "他没突破你的防守" << endl;
}
}else{
cout << "他没突破你的防守" << endl;
}
} else if (zs > 3) {
cout << "这回合你选择进攻" << endl;
cout << "他发起了防守" << endl;
if (wowin < my) {
int u = rand() % 10;
u++;
if (u <= 8) {
cout << "你突破了他的防守!" << endl;
wowin++;
} else {
cout << "你没突破他的防守" << endl;
}
}else{
cout << "你没突破他的防守" << endl;
}
} else {
cout << "你发起了奇袭" << endl;
cout << "他没有发现" << endl;
if (wowin < my) {
int u = rand() % 100;
u++;
if (u <= 93) {
cout << "你突破了他的防守!" << endl;
wowin += 2;
} else {
cout << "你没突破他的防守" << endl;
}
}else{
cout << "你没突破他的防守" << endl;
}
}
}
if (zhanshu == 5) {
int zs = rand() % 10;
zs++;
if ((zs <= 7 || wowin == my) && youwin != you) {
cout << "这回合你选择防守" << endl;
cout << "他发起了进攻" << endl;
if (youwin < you) {
int u = rand() % 10;
u++;
if (u <= 8) {
cout << "他突破了你的防守!" << endl;
youwin++;
} else {
cout << "他没突破你的防守" << endl;
}
}else{
cout << "他没突破你的防守" << endl;
}
} else if (zs <= 8) {
cout << "这回合你选择进攻" << endl;
cout << "他发起了防守" << endl;
if (wowin < my) {
int u = rand() % 10;
u++;
if (u <= 3) {
cout << "你突破了他的防守!" << endl;
wowin++;
} else {
cout << "你没突破他的防守" << endl;
}
}else{
cout << "你没突破他的防守" << endl;
}
} else {
cout << "你发起了固守" << endl;
cout << "他发起了进攻" << endl;
if (youwin < you) {
int u = rand() % 100;
u++;
if (u <= 90) {
cout << "他没突破你的防守" << endl;
} else {
cout << "他突破了你的防守!" << endl;
youwin++;
}
}else{
cout << "他没突破你的防守" << endl;
}
}
}
Sleep(2000);
system("cls");
}
Sleep(1000);
system("cls");
cout << "战斗结束!" << endl;
cout << "最终比分:" << wowin << ":" << youwin << endl;
team[cli_team].goal += wowin;
team[cli_team].lose += youwin;
team[oponment].goal += youwin;
team[oponment].lose += wowin;
if (wowin > youwin) {
cout << "你赢了" << endl;
team[cli_team].point += 3;
} else if (wowin < youwin) {
cout << "你输了" << endl;
team[oponment].point += 3;
} else {
cout << "打平了" << endl;
team[cli_team].point += 1;
team[cli_team].point += 1;
}
team[cli_team].ablity=yuan;
if(youwin>5){
int u=rand()%5;
if(u==1){
cout<<"很遗憾,你的将领战死沙场,战力值--"<<endl;
team[cli_team].ablity-=5;
}
}
if(wowin>5){
int u=rand()%5;
if(u==1){
cout<<"恭喜,对面将领战死沙场,战力值--"<<endl;
team[oponment].ablity-=5;
}
}
}
// 建设城池:升级城墙
void build_wall(Team &t) {
if (t.resource < 45) {
cout << "需要45万资源!" << endl;
return;
}
t.city.wallLevel++;
t.resource -= 45;
cout << "城墙升级至" << t.city.wallLevel << "级!防御力提升!" << endl;
t.ablity += 5; // 城墙等级提升,部队战力增加
system("pause");
system("cls");
}
// 建设城池:升级农田
void build_farm(Team &t) {
if (t.resource < 20) {
cout << "需要20万资源!" << endl;
return;
}
t.city.farmLevel++;
t.resource -= 20;
cout << "农田升级至" << t.city.farmLevel << "级!每回合资源产出增加!" << endl;
system("pause");
system("cls");
}
void build_minju(Team &t) {
if (t.resource < 20) {
cout << "需要20万资源!" << endl;
return;
}
t.city.populationwuzi++;
t.resource -= 20;
cout << "民居升级至" << t.city.populationwuzi << "级!每回合资源产出增加!" << endl;
system("pause");
system("cls");
}
// 更新城池状态(每回合自动调用)
void update_city(Team &t) {
// 每回合根据农田等级产出资源
t.resource += t.city.farmLevel * 5;
// 城墙等级提升部队战力
t.ablity += t.city.wallLevel * 3;
team[cli_team].city.population+=team[cli_team].city.populationwuzi*2;
}
void chengchi(){
cout<<"目前资源:"<<team[cli_team].resource<<"万"<<endl;
cout<<"目前人口:"<<team[cli_team].city.population<<"万"<<endl;
cout << "建设城池:" << endl;
cout << "(1): 升级城墙(消耗 45万资源)" << endl;
cout << "(2): 升级农田(消耗 20万资源)" << endl;
cout << "(3): 升级民居(消耗 20万资源)" << endl;
int build_choice;
cin >> build_choice;
if (build_choice == 1) {
build_wall(team[cli_team]);
} else if (build_choice == 2) {
build_farm(team[cli_team]);
} else if (build_choice == 3) {
build_farm(team[cli_team]);
} else {
cout << "输入错误!" << endl;
}
}
// 互动选项
void interact_options() {
srand(time(NULL));
cout << "互动选项:" << endl;
cout << "(1): 招募新兵(消耗 10万资源 10万人口)" << endl;
cout << "(2): 购买装备(消耗 30万资源)" << endl;
cout << "(3): 外交" << endl;
cout << "(4): 查看资源" << endl;
cout << "(5): 建设城池" << endl;
int option;
cin >> option;
if (option == 1) {
if (team[cli_team].resource >= 5) {
cout << "招募了新兵,战力值 +5!" << endl;
team[cli_team].ablity += 5;
team[cli_team].resource -= 10;
team[cli_team].city.population-= 10;
} else {
cout << "资源不足,无法招募新兵!" << endl;
}
} else if (option == 2) {
if (team[cli_team].resource >= 10) {
cout << "购买了装备,战力值 +10!" << endl;
team[cli_team].ablity += 10;
team[cli_team].resource -= 18;
} else {
cout << "资源不足,无法购买装备!" << endl;
}
} else if (option == 3) {
if (team[cli_team].resource >= 15) {
team[cli_team].resource -= 5;
cout << "请选择外交的势力:" << endl;
int u;
cin >> u;
if (u == cli_team) {
cout << "错误" << endl;
team[cli_team].resource += 15;
return;
} else {
cout << "你要干什么:(1)宣战(消耗 10万资源) (2)贸易(消耗 15万资源) (3)侦察(消耗 5万资源)" << endl;
int my;
cin >> my;
if (my == 1) {
team[cli_team].resource -= 5;
oponment = u;
play_battle_animation();
} else if(my==2){
team[cli_team].resource -= 10;
int u1 = rand() % 25;
team[cli_team].resource += u1;
cout << "你获取了" << u1 << "万点资源" << endl;
return;
}else{
cout<<"他的战力值:"<< team[u].ablity <<" 资源值:"<<team[cli_team].resource<<"万"<<endl;
}
}
} else {
cout << "资源不足,无法进行外交!" << endl;
}
} else if (option == 4) {
cout << "当前资源: " << team[cli_team].resource <<"万"<< endl;
} else if (option == 5) {
chengchi();
}
}
// 历史事件
void historical_events() {
if (current_round == 3) {
cout << "黄巾起义爆发,天下大乱!" << endl;
for (int i = 1; i <= renshu; i++) {
team[i].ablity--;
}
team[14].ablity += 5;
team[15].ablity += 10;
}
if (current_round == 17) {
cout << "赤壁之战即将打响,战火纷飞!" << endl;
team[2].ablity -= 20;
team[1].ablity += 10;
team[3].ablity += 8;
}
if (current_round == 7) {
cout << "董卓进京,开始暴政!十八路诸侯会合。" << endl;
team[4].ablity += 20;
team[5].ablity += 5;
}
if (current_round == 15) {
cout << "官渡之战打响,战火纷飞!" << endl;
team[5].ablity -= 30;
team[2].ablity += 20;
}
if (current_round == 28) {
cout << "夷陵之战打响,战火纷飞!" << endl;
team[1].ablity -= 40;
team[3].ablity -= 5;
}
if (current_round == 33) {
cout << "诸葛亮北伐,屯田,国力加强。" << endl;
team[1].ablity += 10;
}
Sleep(1000);
}
// 游戏主循环
void play_game() {
choose_tactic(); // 选择战术
random_event(); // 随机事件
historical_events(); // 历史事件
if (current_round == renshu) memset(vis, 0, sizeof(vis)); // 重置 vis 数组
oponment = rand() % renshu + 1;
while (vis[oponment] == 1 || oponment == cli_team) {
oponment = rand() % renshu + 1;
}
vis[oponment]++;
cout << "你的对手是: " << team[oponment].name << endl;
system("pause");
// 改进后的胜负判断逻辑
int total_ability = team[cli_team].ablity + team[oponment].ablity;
int result = rand() % total_ability;
if (result < team[cli_team].ablity) {
// 玩家胜利
my_points = rand() % 10 + 1; // 杀敌数至少为 1
you_points = rand() % (my_points); // 敌方杀敌数不超过玩家
} else {
// 玩家失败
you_points = rand() % 10 + 1; // 敌方杀敌数至少为 1
my_points = rand() % (you_points); // 玩家杀敌数不超过敌方
}
play_battle_animation(); // 战斗动画
system("pause");
system("cls");
}
// 其他部队的战斗
void play_others() {
bool local_vis[45] = {false};
for (int i = 1; i <= renshu; i++) {
if (i == cli_team || i == oponment) continue;
if (local_vis[i]) continue;
local_vis[i] = true;
int opn = rand() % renshu + 1;
while (local_vis[opn] || opn == cli_team || opn == oponment) {
opn = rand() % renshu + 1;
}
local_vis[opn] = true;
if (team[i].ablity > team[opn].ablity) {
team[i].point += 3;
int add1 = rand() % 10 + 1;
int add2 = rand() % add1;
team[i].goal += add1, team[i].lose += add2;
team[opn].goal += add2, team[opn].lose += add1;
if(i>5){
int u=rand()%5;
if(u==1){
cout<<"报!"<<team[opn].name<<"的将领战死沙场,战力值--"<<endl;
team[opn].ablity-=5;
}
}
if(opn>5){
int u=rand()%5;
if(u==1){
cout<<"报!"<<team[i].name<<"的将领战死沙场,战力值--"<<endl;
team[i].ablity-=5;
}
}
} else if (team[i].ablity == team[opn].ablity) {
team[i].point += 1;
team[opn].point += 1;
int add1 = rand() % 10 + 1;
int add2 = add1;
team[i].goal += add1, team[i].lose += add2;
team[opn].goal += add2, team[opn].lose += add1;
} else {
team[opn].point += 3;
int add1 = rand() % 10 + 1;
int add2 = rand() % add1;
team[i].goal += add2, team[i].lose += add1;
team[opn].goal += add1, team[opn].lose += add2;
}
}
}
// 检查游戏结束
void check_endgame() {
if (team[cli_team].point >= 100) {
cout << "恭喜你统一天下,成为新的皇帝!" << endl;
} else if (team[cli_team].point <= 20) {
cout << "你被其他势力击败,游戏结束。" << endl;
} else {
cout << "你保持中立,最终成为一方诸侯。" << endl;
}
}
// 主函数
int main() {
srand(time(NULL));
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << "三国模拟器" << endl;
Sleep(200);
system("cls");
team[1].name = "刘备";
team[2].name = "曹操";
team[3].name = "孙权";
team[4].name = "董卓";
team[5].name = "袁绍";
team[6].name = "袁术";
team[7].name = "刘表";
team[8].name = "刘璋";
team[9].name = "公孙瓒";
team[10].name = "吕布";
team[11].name = "山贼";
team[12].name = "草贼部落";
team[13].name = "孟获";
team[14].name = "黄巾军";
team[15].name = "张角";
team[16].name = "青州军";
team[17].name = "白毦军";
team[18].name = "虎豹骑";
team[19].name = "杂牌军";
team[20].name = "藤甲军";
team[1].ablity = rand()%60;
team[2].ablity = rand()%75;
team[3].ablity = rand()%65;
team[4].ablity = rand()%50;
team[5].ablity = rand()%80;
team[6].ablity = rand()%45;
team[7].ablity = rand()%35;
team[8].ablity = rand()%40;
team[9].ablity = rand()%45;
team[10].ablity = rand()%55;
team[11].ablity = rand()%6;
team[12].ablity = rand()%5;
team[13].ablity = rand()%40;
team[14].ablity = rand()%8;
team[15].ablity = rand()%35;
team[16].ablity = rand()%10;
team[17].ablity = rand()%24;
team[18].ablity = rand()%25;
team[19].ablity = rand()%6;
team[20].ablity = rand()%18;
for(int i=1;i<=renshu;i++)team[i].ablity +=1;
team[1].resource = rand()%25;
team[2].resource = rand()%35;
team[3].resource = rand()%25;
team[4].resource = rand()%40;
team[5].resource = rand()%50;
team[6].resource = rand()%30;
team[7].resource = rand()%15;
team[8].resource = rand()%15;
team[9].resource = rand()%20;
team[10].resource = rand()%25;
team[11].resource = rand()%5;
team[12].resource = rand()%5;
team[13].resource = rand()%15;
team[14].resource = rand()%5;
team[15].resource = rand()%20;
team[16].resource = rand()%5;
team[17].resource = rand()%10;
team[18].resource = rand()%10;
team[19].resource = rand()%5;
team[20].resource = rand()%5;
renshu = 20;
for (int i = 1; i <= renshu; i++) {
team[i].train_remain = 5;
int u=rand()%5;
u++;
team[i].city.population = u;
}
for (int i = 1; i <= renshu; i++) {
cout << "(" << i << "):" << team[i].name << endl;
}
cout << "选择你的部队:" << endl;
cin >> cli_team;
system("cls");
if (cli_team <= 0 || cli_team > renshu) {
cout << "输入错误,游戏结束";
system("pause");
return 0;
}
int total_rounds = 50;
while (current_round <= total_rounds) {
team[cli_team].train_remain = rand() % 6;
update_city(team[cli_team]); // 更新城池状态
laodazhouji:
// historical_events();
cout << "建安第 " << current_round << " 年: ";
cout << "你的部队: " << team[cli_team].name << endl;
Sleep(500);
cout << "剩余练兵次数: " << team[cli_team].train_remain << endl;
Sleep(500);
cout << "你的部队战力值: " << team[cli_team].ablity << endl;
Sleep(500);
cout << "当前资源: " << team[cli_team].resource <<"万"<< endl;
Sleep(500);
cout << "开始下一场战争" << endl;
Sleep(500);
cout << "查看排行榜" << endl;
Sleep(1000);
for(int i=1;i<=renshu;i++){
if(i!=cli_team){
int u=rand()%5;
team[i].ablity+=u;
}
}
cout << endl;
cout << "(1): 练兵" << endl;
cout << "(2): 查看排行榜" << endl;
cout << "(3): 开始下一场战争" << endl;
cout << "(4): 互动选项" << endl;
int op;
cin >> op;
while (op < 1 || op > 4) {
cout << "输入错误,请重新输入 ";
Sleep(2000);
system("cls");
goto laodazhouji;
}
system("cls");
if (op == 1) {
if (team[cli_team].train_remain == 0) {
cout << "没有练兵次数了!" << endl;
Sleep(2000);
system("cls");
goto laodazhouji;
}
team[cli_team].train_remain--;
int win = rand() % 3;
if (win == 1 || win == 2) {
cout << "练兵成功!部队战力值 +1!" << endl;
Sleep(2000);
team[cli_team].ablity++;
} else {
cout << "练兵失败!部队战力值 -1!" << endl;
Sleep(2000);
team[cli_team].ablity--;
}
system("cls");
goto laodazhouji;
} else if (op == 2) {
print_board();
goto laodazhouji;
} else if (op == 3) {
play_game();
play_others();
team[cli_team].resource += 5;
current_round++; // 只有选择战争时回合推进
} else if (op == 4) {
interact_options();
goto laodazhouji;
}
}
system("cls");
cout << "战争结束" << endl;
Sleep(2000);
cout << "这是最终排行榜:" << endl;
print_board();
check_endgame();
return 0;
}