线上博弈游戏C++代码 原创
2026-08-06 13:39:05
发布于:浙江
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <vector>
using namespace std;
const int N =1e5+10;
struct Node {
string name;
int hp;
int dm;
int fy;
bool state=true;
int kill=0;
int score=0;
};
bool ck;
int n;
vector <Node > v(N+1);
int survive;
int random_(int mn, int mx) {
return (rand() % (mx - mn + 1)) + mn;
}
void color(int c) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, c);
}
string talot[]= {"无意义","愚者","女祭司","力量","倒吊人","高塔","太阳","月亮"};
void check(int tlp,int t) {
if (tlp == 1) {
v[t].hp -= v[t].dm/2;
cout << "|" << v[t].name << "|被愚者反噬 受到了" << "[" << v[t].dm/2 << "]" << "的伤害\n";
if (v[t].hp <= 0) {
cout << "|" << v[t].name << "| 已死亡";
survive -= 1;
v[t].state = false;
}
} else if (tlp == 2) {
cout << "无事发生\n";
} else if (tlp == 3) {
int x = random_(1,100);
cout << "|" << v[t].name << "|的攻击 增加了[" << x << "]点伤害\n";
v[t].dm += x;
} else if(tlp == 4) {
survive -= 1;
cout << "|" << v[t].name << "|抽到了";
color(14);
cout << "倒吊人";
color(11);
cout << "[" << v[t].name << "]已死亡\n";
v[t].state=false;
} else if(tlp == 5) {
int x = random_(1,50);
cout << "|" << v[t].name << "|的防御 增加了[" << x << "]点\n";
v[t].fy += x;
} else if (tlp == 6) {
int x = random_(1,80);
cout << "|" << v[t].name << "|的血量 增加了[" << x << "]点\n";
v[t].hp += x;
} else if (tlp == 7) {
int x = random_(1,80);
cout << "|" << v[t].name << "|的血量 减少了[" << x << "]点\n";
v[t].hp -= x;
if (v[t].hp <= 0) {
cout << "|" << v[t].name << "| 已死亡";
survive -= 1;
v[t].state = false;
}
}
color(7);
}
int main() {
srand(time(0));
cout << "请输入玩家人数:";
cin >> n;
Sleep(500);
for (int i = 1; i <= n; i ++) {
cout << "请输入玩家名称" << i << ":";
cin >> v[i].name;
v[i].hp = random_(150,400);
v[i].dm = random_(50,150);
v[i].fy = random_(50,100);
Sleep(500);
}
cout << "---------------------------------------\n";
for (int i = 1; i <= n; i ++) {
cout << v[i].name << " HP" << v[i].hp << " 攻" << v[i].dm << " 防" << v[i].fy << "\n";
}
cout << "---------------------------------------\n";
survive=n;
while (survive > 1) {
ck=false;
for (int i = 1; i <= n; i ++) {
int t = random_(1,n);
if (v[i].state == false || v[t].state == false) continue;
if (t == i) {
Sleep(500);
cout << "|" <<v[i].name << "|" << "还是太善良了 没有攻击任何人\n";
} else {
cout << "|" << v[i].name << "|" << "向" << "|" << v[t].name << "|" << " 起了攻击\n";
v[i].score += v[i].dm;
Sleep(500);
if (v[t].fy / 2 > 35) {
v[t].fy /= 2;
cout << "|" << v[t].name << "|" << "回避了本次攻击\n";
ck=true;
} else {
cout << "|" << v[t].name << "|受到了";
color(4);
cout<< "[" <<v[i].dm << "]";
color(7);
cout << "的伤害\n";
v[t].hp -= v[i].dm;
cout << v[t].name << "还剩|" << v[t].hp << "|点血";
}
if (v[t].hp <= 0) {
survive -= 1;
Sleep(1500);
color(10);
cout << "|" << v[t].name << "|被|" << v[i].name << "|击败了\n";
v[i].kill ++;
v[t].state=false;
int tlp = random_(1,7);
color(11);
cout << "|" << v[t].name << "|抽到的塔罗牌为:" ;
if (tlp == 4 || tlp == 2) color(14);
cout << talot[tlp] << "\n";
color(7);
int joker = random_(1,10);
if (joker >= 8) {
color(14);
cout << "小丑牌 [" << talot[tlp] << "] 牌不奏效\n";
}
if (tlp == 2 && joker < 8) {
Sleep(500);
color(10);
cout << "很幸运|" << v[t].name << "| 复活了\n";
color(7);
v[t].hp = random_(150,400);
v[t].dm = random_(50,150);
v[t].fy = random_(50,100);
survive += 1;
v[t].state=true;
} else {
Sleep(500);
color(10);
cout << "很不幸 | " << v[t].name << "| 已死亡\n";
color(7);
}
}else {
if (ck == true) {
int tlp = random_(1,7);
color(11);
cout << "|" << v[t].name << "|抽到的塔罗牌为:" ;
if (tlp == 4) color(14);
cout << talot[tlp] << "\n";
Sleep(500);
int joker = random_(1,10);
if (joker >= 8) {
color(14) ;
cout << "小丑牌 [" << talot[tlp] << "]不奏效\n";
}else check(tlp,t);
color(7);
}
}
}
}
}
for (int i = 1; i <= n; i ++) {
if (v[i].state == true) {
cout << "|" << v[i].name << "|为胜者\n";
break;
}
}
cout << "---------------------------------------\n";
int id,ans=-INT_MAX;
for (int i = 1; i <= n; i ++) {
cout << "|" << v[i].name << "|" << "击败了[" << v[i].kill << "]个人 ," << "造成了[" << v[i].score << "]伤害\n";
if (v[i].score > ans) {
ans = v[i].score;
id=i;
} else if (v[i].score == ans) {
if (v[i].kill > v[id].kill) {
id=i;
ans = v[i].score;
}
}
}
cout << "---------------------------------------\n";
cout << "|" << v[id].name << "|为MVP获得了:[" << ans << "]的分数,击败了[" << v[id].kill << "]个人";
return 0;
}
喜欢点个赞吧QWQ
全部评论 3
QWQ
2026-08-06 来自 浙江
0代码能运行就行 团队火热招募中!
立即点击链接加入我们:https://www.acgo.cn/application/1966730825962467328
我们致力于打造高质量的C++模板库,汇聚各路编程爱好者。
加入团队,你可以:贡献和使用优质模板,提升效率;与同好交流技术,共同进步;享受团队专属福利与资源支持。如有任何疑问,欢迎私信我,我会尽快解答。
2026-08-06 来自 浙江
0加油
2026-08-06 来自 广东
0


























有帮助,赞一个