news 2026/1/17 4:20:53

c++狼人杀

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
c++狼人杀

#include<bits/stdc++.h>
#include<windows.h>
#define /*白色*/white SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
#define /*初始色*/original SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
#define /*红色*/red SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED);
#define /*绿色*/green SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN);
#define /*蓝色*/blue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_BLUE);
#define /*黄色*/yellow SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED |FOREGROUND_GREEN);
#define /*粉色*/pink SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED |FOREGROUND_BLUE);
#define /*淡蓝*/lightblue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_GREEN |FOREGROUND_BLUE);
using namespace std;
int life[7]={1,1,1,1,1,1};
int potions[5]={-1,1};
int vote[7]={0};
string name[7]={"预言家","女巫","平民","平民","狼人","狼人"};
int seer(int x,int y,int n,int p1,int p2,int l1,int l2)
{
int rescued;
int die;
int die_potions;
bool gameover=false;
while(gameover==false)
{
cout<<endl<<"下一天:----------------------------"<<endl;
for(int i=1;i<=4;i++)
{
if(i==1&&(life[l1]>=0 or life[l2]>=0))
{
cout<<"狼人:"<<endl;
srand((unsigned)time(NULL));
die=rand()%6;
while(name[die]=="狼人"||life[die]==-1) die=rand()%6;
life[die]--;
cout<<"狼人杀了"<<name[die]<<endl;
}
if(i==2&&life[x]>=0)
{
cout<<"预言家:"<<endl;
cout<<"你是预言家,请输入0~5的编号(代表相应的人)(除了"<<x<<"号)来预言"<<endl;
int res;
cin>>res;
if(name[res]=="平民"||name[res]=="女巫"||name[res]=="预言家") cout<<"好人"<<endl;
if(name[res]=="狼人") cout<<"坏人"<<endl;
}
if(i==3&&life[n]>=0)
{
cout<<"女巫:"<<endl;
int dor;
srand((unsigned)time(NULL));
dor=rand()%3;
if(dor==0)
{
die_potions=rand()%6;
while(name[die_potions]=="女巫"||life[die_potions]<=0) die_potions=rand()%6;
life[die_potions]--;
potions[dor]++;
cout<<"女巫杀了"<<name[die_potions]<<endl;
}
if(dor==1)
{
int help;
for(int i=0;i<6;i++) if(life[i]==0) life[i]++,help=i;
potions[dor]--;
cout<<"女巫救了"<<name[help]<<endl;
}
if(dor==2)
{
cout<<"没用药"<<endl;
}
}
if(i==4)
{
int sum_die=0;
cout<<"今晚";
for(int j=0;j<6;j++)
{
if(life[j]==0)
{
sum_die++;
life[j]--;
cout<<j<<"号"<<name[j]<<"死了";
}
}
if(sum_die==0) cout<<"是平安夜";
int fl=0,fp=0;
for(int j=0;j<6;j++)
{
if(life[j]==1)
{
if(name[j]=="狼人") fl++;
else fp++;
}
}
if(fl==0)
{
cout<<endl<<"平民赢了"<<endl;
gameover=true;
continue;
}
if(fp==0)
{
cout<<endl<<"狼人赢了"<<endl;
gameover=true;
continue;
}
cout<<endl<<"投票"<<endl;
int a[7]={0,0,0,0,0,0};
for(int j=0;j<6;j++)
{
if(j==x&&life[x]==1)
{
cout<<"请投0~5号你认为是狼人的";
int ps;
cin>>ps;
a[ps]++;
}
else if(life[j]==1)
{
srand((unsigned)time(NULL));
int cs;
cs=rand()%6;
while(life[cs]!=1||cs==j) cs=rand()%6;
a[cs]++;
}
}
cout<<"投票结果:"<<endl;
cout<<"0 1 2 3 4 5"<<endl;
for(int j=0;j<6;j++) cout<<a[j]<<" ";
cout<<endl;
int max=-1,mp;
for(int j=0;j<6;j++) if(a[j]>=max) max=a[j],mp=j;
cout<<mp<<"号"<<name[mp]<<"出局"<<endl<<endl;
life[mp]=-1;
int good=0,bad=0;
for(int j=0;j<6;j++)
{
if(life[j]==1&&(name[j]=="平民"||name[j]=="女巫"||name[j]=="预言家")) good++;
if(life[j]==1&&name[j]=="狼人") bad++;
}
if(bad==0)
{
cout<<"平民赢了"<<endl;
gameover=true;
}
if(good==0)
{
cout<<"狼人赢了"<<endl;
gameover=true;
}
}
}
system("pause");
}
return 0;
}
int Lycan(int x,int y,int n,int p1,int p2,int l1,int l2)
{
int rescued;
int die;
int die_potions;
bool gameover=false;
while(gameover==false)
{
cout<<endl<<"下一天:----------------------------"<<endl;
for(int i=1;i<=4;i++)
{
if(i==1&&life[x]>=0)
{
cout<<"狼人:"<<endl;
cout<<"你是狼人,请输入0~5的编号(代表相应的人)(除了"<<x<<"和"<<l1+l2-x<<"号)来杀死"<<endl;
cin>>die;
life[die]--;
cout<<"狼人杀了"<<name[die]<<endl;
}
if(i==1&&life[x]==-1&&life[l1+l2-x]>=0)
{
cout<<"狼人:"<<endl;
srand((unsigned)time(NULL));
die=rand()%6;
while(name[die]=="狼人"||life[die]==-1) die=rand()%6;
life[die]--;
cout<<"狼人杀了"<<name[die]<<endl;
}
if(i==2&&life[y]>=0)
{
cout<<"预言家:"<<endl;
cout<<"预言完毕"<<endl;
}
if(i==3&&life[n]>=0)
{
cout<<"女巫:"<<endl;
int dor;
srand((unsigned)time(NULL));
dor=rand()%3;
if(dor==0)
{
die_potions=rand()%6;
while(name[die_potions]=="女巫"||life[die_potions]<=0) die_potions=rand()%6;
life[die_potions]--;
potions[dor]++;
cout<<"女巫杀了"<<name[die_potions]<<endl;
}
if(dor==1)
{
int help;
for(int i=0;i<6;i++) if(life[i]==0) life[i]++,help=i;
potions[dor]--;
cout<<"女巫救了"<<name[help]<<endl;
}
if(dor==2)
{
cout<<"没用药"<<endl;
}
}
if(i==4)
{
int sum_die=0;
cout<<"今晚";
for(int j=0;j<6;j++)
{
if(life[j]==0)
{
sum_die++;
life[j]--;
cout<<j<<"号"<<name[j]<<"死了";
}
}
if(sum_die==0) cout<<"是平安夜";
int fl=0,fp=0;
for(int j=0;j<6;j++)
{
if(life[j]==1)
{
if(name[j]=="狼人") fl++;
else fp++;
}
}
if(fl==0)
{
cout<<endl<<"平民赢了"<<endl;
gameover=true;
continue;
}
if(fp==0)
{
cout<<endl<<"狼人赢了"<<endl;
gameover=true;
continue;
}
cout<<endl<<"投票"<<endl;
int a[7]={0,0,0,0,0,0};
for(int j=0;j<6;j++)
{
if(j==x&&life[x]==1)
{
cout<<"请投0~5号你认为是狼人的";
int ps;
cin>>ps;
a[ps]++;
}
else if(life[j]==1)
{
srand((unsigned)time(NULL));
int cs;
cs=rand()%6;
while(life[cs]!=1||cs==j) cs=rand()%6;
a[cs]++;
}
}
cout<<"投票结果:"<<endl;
cout<<"0 1 2 3 4 5"<<endl;
for(int j=0;j<6;j++) cout<<a[j]<<" ";
cout<<endl;
int max=-1,mp;
for(int j=0;j<6;j++) if(a[j]>=max) max=a[j],mp=j;
cout<<mp<<"号"<<name[mp]<<"出局"<<endl<<endl;
life[mp]=-1;
int good=0,bad=0;
for(int j=0;j<6;j++)
{
if(life[j]==1&&(name[j]=="平民"||name[j]=="女巫"||name[j]=="预言家")) good++;
if(life[j]==1&&name[j]=="狼人") bad++;
}
if(bad==0)
{
cout<<"平民赢了"<<endl;
gameover=true;
}
if(good==0)
{
cout<<"狼人赢了"<<endl;
gameover=true;
}
}
}
system("pause");
}
return 0;
}
int witch(int x,int y,int n,int p1,int p2,int l1,int l2)
{
int rescued;
int die;
int die_potions;
bool gameover=false;
while(gameover==false)
{
cout<<endl<<"下一天:----------------------------"<<endl;
for(int i=1;i<=4;i++)
{
if(i==1&&(life[l1]>=0 or life[l2]>=0))
{
cout<<"狼人:"<<endl;
srand((unsigned)time(NULL));
die=rand()%6;
while(name[die]=="狼人"||life[die]==-1) die=rand()%6;
life[die]--;
cout<<"狼人杀了"<<name[die]<<endl;
}
if(i==2&&life[y]>=0)
{
cout<<"预言家:"<<endl;
cout<<"预言完毕"<<endl;
}
if(i==3&&life[n]>=0)
{
cout<<"女巫:"<<endl;
cout<<"你是女巫,请输入d、r、n。d代表要杀、r代表要救,n代表不用药"<<endl;
char dor;
cin>>dor;
if(dor=='d')
{
cout<<"请输入0~5号你要杀的人(除了"<<n<<"号)"<<endl;
cin>>die_potions;
life[die_potions]--;
potions[0]++;
cout<<"女巫杀了"<<name[die_potions]<<endl;
}
if(dor=='r')
{
int help;
for(int i=0;i<6;i++) if(life[i]==0) life[i]++,help=i;
potions[1]--;
cout<<"女巫救了"<<name[help]<<endl;
}
if(dor=='n')
{
cout<<"没用药"<<endl;
}
}
if(i==4)
{
int sum_die=0;
cout<<"今晚";
for(int j=0;j<6;j++)
{
if(life[j]==0)
{
sum_die++;
life[j]--;
cout<<j<<"号"<<name[j]<<"死了";
}
}
if(sum_die==0) cout<<"是平安夜";
int fl=0,fp=0;
for(int j=0;j<6;j++)
{
if(life[j]==1)
{
if(name[j]=="狼人") fl++;
else fp++;
}
}
if(fl==0)
{
cout<<endl<<"平民赢了"<<endl;
gameover=true;
continue;
}
if(fp==0)
{
cout<<endl<<"狼人赢了"<<endl;
gameover=true;
continue;
}
cout<<endl<<"投票"<<endl;
int a[7]={0,0,0,0,0,0};
for(int j=0;j<6;j++)
{
if(j==x&&life[x]==1)
{
cout<<"请投0~5号你认为是狼人的";
int ps;
cin>>ps;
a[ps]++;
}
else if(life[j]==1)
{
srand((unsigned)time(NULL));
int cs;
cs=rand()%6;
while(life[cs]!=1||cs==j) cs=rand()%6;
a[cs]++;
}
}
cout<<"投票结果:"<<endl;
cout<<"0 1 2 3 4 5"<<endl;
for(int j=0;j<6;j++) cout<<a[j]<<" ";
cout<<endl;
int max=-1,mp;
for(int j=0;j<6;j++) if(a[j]>=max) max=a[j],mp=j;
cout<<mp<<"号"<<name[mp]<<"出局"<<endl<<endl;
life[mp]=-1;
int good=0,bad=0;
for(int j=0;j<6;j++)
{
if(life[j]==1&&(name[j]=="平民"||name[j]=="女巫"||name[j]=="预言家")) good++;
if(life[j]==1&&name[j]=="狼人") bad++;
}
if(bad==0)
{
cout<<"平民赢了"<<endl;
gameover=true;
}
if(good==0)
{
cout<<"狼人赢了"<<endl;
gameover=true;
}
}
}
system("pause");
}
return 0;
}
int commoner(int x,int y,int n,int p1,int p2,int l1,int l2)
{
int rescued;
int die;
int die_potions;
bool gameover=false;
while(gameover==false)
{
cout<<endl<<"下一天:----------------------------"<<endl;
for(int i=1;i<=4;i++)
{
if(i==1&&(life[l1]>=0 or life[l2]>=0))
{
cout<<"狼人:"<<endl;
srand((unsigned)time(NULL));
die=rand()%6;
while(name[die]=="狼人"||life[die]==-1) die=rand()%6;
life[die]--;
cout<<"狼人杀了"<<name[die]<<endl;
}
if(i==2&&life[y]>=0)
{
cout<<"预言家:"<<endl;
cout<<"预言完毕"<<endl;
}
if(i==3&&life[n]>=0)
{
cout<<"女巫:"<<endl;
int dor;
srand((unsigned)time(NULL));
dor=rand()%3;
if(dor==0)
{
die_potions=rand()%6;
while(name[die_potions]=="女巫"||life[die_potions]<=0) die_potions=rand()%6;
life[die_potions]--;
potions[dor]++;
cout<<"女巫杀了"<<name[die_potions]<<endl;
}
if(dor==1)
{
int help;
for(int i=0;i<6;i++) if(life[i]==0) life[i]++,help=i;
potions[dor]--;
cout<<"女巫救了"<<name[help]<<endl;
}
if(dor==2)
{
cout<<"没用药"<<endl;
}
}
if(i==4)
{
int sum_die=0;
cout<<"今晚";
for(int j=0;j<6;j++)
{
if(life[j]==0)
{
sum_die++;
life[j]--;
cout<<j<<"号"<<name[j]<<"死了";
}
}
if(sum_die==0) cout<<"是平安夜";
int fl=0,fp=0;
for(int j=0;j<6;j++)
{
if(life[j]==1)
{
if(name[j]=="狼人") fl++;
else fp++;
}
}
if(fl==0)
{
cout<<endl<<"平民赢了"<<endl;
gameover=true;
continue;
}
if(fp==0)
{
cout<<endl<<"狼人赢了"<<endl;
gameover=true;
continue;
}
cout<<endl<<"投票"<<endl;
int a[7]={0,0,0,0,0,0};
for(int j=0;j<6;j++)
{
if(j==x&&life[x]==1)
{
cout<<"请投0~5号你认为是狼人的";
int ps;
cin>>ps;
a[ps]++;
}
else if(life[j]==1)
{
srand((unsigned)time(NULL));
int cs;
cs=rand()%6;
while(life[cs]!=1||cs==j) cs=rand()%6;
a[cs]++;
}
}
cout<<"投票结果:"<<endl;
cout<<"0 1 2 3 4 5"<<endl;
for(int j=0;j<6;j++) cout<<a[j]<<" ";
cout<<endl;
int max=-1,mp;
for(int j=0;j<6;j++) if(a[j]>=max) max=a[j],mp=j;
cout<<mp<<"号"<<name[mp]<<"出局"<<endl<<endl;
life[mp]=-1;
int good=0,bad=0;
for(int j=0;j<6;j++)
{
if(life[j]==1&&(name[j]=="平民"||name[j]=="女巫"||name[j]=="预言家")) good++;
if(life[j]==1&&name[j]=="狼人") bad++;
}
if(bad==0)
{
cout<<"平民赢了"<<endl;
gameover=true;
}
if(good==0)
{
cout<<"狼人赢了"<<endl;
gameover=true;
}
}
}
system("pause");
}
return 0;
}
int main()
{
begin:
white
cout<<" 狼人杀"<<endl;
yellow
cout<<"1个预言家 ";
yellow
cout<<"1个女巫 " ;
yellow
cout<<"2个平民 ";
green
cout<<" 2个狼人"<<endl;
original
Sleep(1000);
srand((unsigned)time(NULL));
for(int i=1;i<=1000;i++)
{
int x=rand()%6;
int y=rand()%6;
swap(name[x],name[y]);
}
int player=rand()%6;
cout<<"你是";
red
cout<<name[player]<<endl;
original
cout<<"您的编号是"<<player<<endl<<endl;
Sleep(2000);
int l1,l2,l=1;
int y;
int n;
int p1,p2,p=1;
for(int i=0;i<6;i++)
{
if(name[i]=="狼人"&&l==1)
{
l1=i;
l++;
continue;
}
if(name[i]=="狼人"&&l==2) l2=i,l++;
if(name[i]=="预言家") y=i;
if(name[i]=="女巫") n=i;
if(name[i]=="平民"&&p==1)
{
p1=i;
p++;
continue;
}
if(name[i]=="平民"&&p==2) p2=i,p++;
}
if(player==y) seer(player,y,n,p1,p2,l1,l2);
if(player==l1 or player==l2) Lycan(player,y,n,p1,p2,l1,l2);
if(player==n) witch(player,y,n,p1,p2,l1,l2);
if(player==p1 or player==p2) commoner(player,y,n,p1,p2,l1,l2);
else return 0;
}

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/16 2:24:59

性价比高的老房换新实用门窗品牌精选指南排名

性价比高的老房换新实用门窗品牌精选指南排名在老房换新的过程中&#xff0c;门窗的更换是至关重要的一环。选择一款性价比高的门窗&#xff0c;不仅能提升居住的舒适度&#xff0c;还能为家居增添美观。以下为大家带来一份实用的门窗品牌精选指南。工厂直营模式&#xff1a;性…

作者头像 李华
网站建设 2026/1/5 12:30:41

好用做老房换新实用门窗品牌精选指南的机构

做老房换新实用门窗的品牌精选指南引言老房换新门窗是提升居住品质的重要工程&#xff0c;然而面对众多的门窗品牌&#xff0c;消费者往往不知如何选择。在众多选择中&#xff0c;工厂直营模式的品牌有着独特的优势。专业评估能力像采用工厂直营模式的这类品牌&#xff0c;具备…

作者头像 李华
网站建设 2025/12/31 16:14:28

灵活用工平台,我的实践复盘

灵活用工平台技术实践复盘&#xff1a;从行业挑战到解决方案的演进行业痛点分析当前&#xff0c;灵活用工平台领域正面临一系列深刻的技术挑战&#xff0c;这些挑战直接关系到平台的稳定性、合规性及用户体验。首要挑战在于海量并发处理与数据精准性。随着灵活用工模式渗透率的…

作者头像 李华
网站建设 2026/1/1 19:27:51

在duckdb 递归CTE中实现深度优先搜索DFS

原帖地址 https://github.com/duckdb/duckdb/discussions/15386 通常的递归CTE都是广度优先搜索&#xff08;BFS&#xff09; WITH RECURSIVE edges(a, b) as( VALUES(1, 2),(1, 3),(2, 4),(4, 5),(4, 6) ), bfs(node, path) AS (SELECT 1 AS node, [] :: STRUCT("from&…

作者头像 李华
网站建设 2025/12/20 20:46:12

基于记忆增强网络的语言模型推理优化

基于记忆增强网络的语言模型推理优化 关键词:记忆增强网络、语言模型、推理优化、注意力机制、深度学习 摘要:本文聚焦于基于记忆增强网络的语言模型推理优化。首先介绍了相关背景,包括研究目的、预期读者、文档结构和术语定义。接着阐述了核心概念,如记忆增强网络和语言模…

作者头像 李华
网站建设 2025/12/20 20:45:47

分类管理与分类统计 UI -Cordova 与 OpenHarmony 混合开发实战

欢迎大家加入[开源鸿蒙跨平台开发者社区](https://openharmonycross平台开发者社区](https://openharmonycrossplatform.csdn.net)&#xff0c;一起共建开源鸿蒙跨平台生态。 本文对应模块&#xff1a;pages.js 中“分类统计”页面以及分类管理相关的 UI 结构&#xff0c;重点是…

作者头像 李华