news 2026/4/16 1:14:46

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/4/13 16:35:09

c语言结构体操作示例

structs.c 完整代码 #include <stdio.h> #include <string.h>// 结构体定义 struct Student {char name[50];int id;float gpa; };// 嵌套结构体定义 struct Date {int day;int month;int year; };struct Employee {char name[50];int employeeId;struct Date hir…

作者头像 李华
网站建设 2026/4/12 18:11:26

专业的康有利到家理疗小程序哪家好

专业的康有利到家理疗小程序哪家好在当今数字化时代&#xff0c;到家理疗服务借助小程序得到了更广泛的推广与应用。康有利到家理疗小程序为用户提供了便捷的理疗服务预约途径&#xff0c;那么专业的康有利到家理疗小程序哪家好呢&#xff1f;市场现状分析目前市场上的康有利到…

作者头像 李华
网站建设 2026/4/15 12:27:41

大模型RAG技术深度剖析:提升AI回答质量的黑科技

本文详细解读了RAG&#xff08;检索增强生成&#xff09;技术&#xff0c;分析了其解决LLMs幻觉、知识过时等问题的意义&#xff0c;介绍了RAG系统的三大发展范式&#xff08;朴素、高级、模块化&#xff09;&#xff0c;探讨了检索、生成、增强环节的关键技术和评估方法&#…

作者头像 李华
网站建设 2026/4/10 8:05:00

STM32学习——编码器接口测速

1.编码器接口测速1.基本要点1.void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, uint16_t TIM_EncoderMode,uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity);//配置定时器编码器接口模式的核心函数&#xff0c;用于将定时器配置为编码器模式&#xff0c;实现对正交编…

作者头像 李华
网站建设 2026/4/4 3:20:45

学Simulink——基础MPPT控制场景实例:基于Simulink的电导增量法(INC)光伏MPPT仿真

目录 手把手教你学Simulink 一、引言:为什么“扰动观察法在快速变化光照下会‘跑偏’”?——因为P&O仅比较前后功率大小,无法区分MPP左侧还是右侧;当光照突变时,( dP ) 符号可能误判,导致反向远离MPP! 二、电导增量法(INC)原理 1. 光伏功率对电压求导 2. 判断…

作者头像 李华