- 分享
狼人杀
- @ 2025-11-23 20:52:29
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
const int daytime=0,night=1;
int day=0, during_time=daytime, player_number, my_number;
HWND hwnd=GetForegroundWindow();
const int blue=0,yellow=1,red=2,green=3,purple=4,white=5;
void color(int c){
switch(c){
case red:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);break;
case green:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);break;
case yellow:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN);break;
case blue:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);break;
case white:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN | FOREGROUND_BLUE);break;
case purple:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_BLUE);break;
}
}
int idx_police=-1;
void gotoxy(int x,int y){
COORD position;
position.X=x;
position.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}
void init_Show_Window(){
system("mode con lines=60 cols=188");
ShowWindow(hwnd,SW_MAXIMIZE);//窗口最大化
DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND);
DrawMenuBar(GetConsoleWindow());//删除×字符
}
const int nvwu=0,cunmin=1,yuyanjia=2,langren=3,lieren=4,shouwei=5,good=6,die=1,life=2;
class player{
public:
int type;
int die_or_life;
int how(){
return die_or_life;
}
int is_light;
int killer;
};
player players[1000];
void change_daytime(){
during_time=daytime;
day++;
}
void change_night(){
during_time=night;
}
int nnvwu=0,ncunmin=0,nyuyanjia=0,nlangren=0,nlieren=0,nshouwei=0;
int idxnvwu,idxshouwei,idxyuyanjia,idxlieren,idxlangren[4]={-1,-1,-1,-1};
bool is_include(int arr[],int b,int l){
for(int i=0;i<l;i++){
if(arr[i]==b)
return true;
}
return false;
}
void init_players(){
my_number=rand()%player_number;
if(player_number==12) nlangren=4;
else if(player_number>=10) nlangren=3;
else nlangren=2;
for(int i=0;i<player_number;i++)
{
players[i].die_or_life=life;
players[i].is_light=0;
players[i].type=-1;
players[i].killer=2147483647;
}
for(int i=0;i<nlangren;i++)
{
int p=rand()%player_number;
if(!is_include(idxlangren,p,4))
idxlangren[i]=p,players[p].type=langren;
else
i--;
Sleep(rand()%80+100);
}
if(player_number==12)
{
do{
idxshouwei=rand()%player_number;
}while(players[idxshouwei].type!=-1);
players[idxshouwei].type=shouwei;
}
do{
idxnvwu=rand()%player_number;
}while(players[idxnvwu].type!=-1);
players[idxnvwu].type=nvwu;
if(player_number>=10)
{
do{
idxlieren=rand()%player_number;
}while(players[idxlieren].type!=-1);
players[idxlieren].type=lieren;
}
do{
idxyuyanjia=rand()%player_number;
}while(players[idxyuyanjia].type!=-1);
players[idxyuyanjia].type=yuyanjia;
for(int i=0;i<player_number;i++)
if(players[i].type==-1)
players[i].type=cunmin,
ncunmin++;
if(players[my_number].type==langren)
{
for(int i=0;i<nlangren;i++)
{
players[idxlangren[i]].is_light=1;
}
}
players[my_number].is_light=1;
}
void print(){
gotoxy(0,0);
cout<<"作者:洛谷393864";
gotoxy(90,0);
if(during_time==night)
color(red);
else
color(blue);
printf("第%d天 | ",day);
if(during_time==night) cout<<"黑夜";
else cout<<"白天";
gotoxy(0,3);
color(blue);
cout<<" 我的号位:"<<my_number+1;
for(int i=0;i<player_number;i++){
gotoxy(i*8+1,4);
if(i==idx_police) color(yellow);
else color(blue);
cout<<i+1<<"号位";
gotoxy(i*8+1,5);
if(players[i].how()==die){
color(red);
cout<<"死 亡";
}else{
color(green);
cout<<"存 活";
}
gotoxy(i*8+1,6);
color(blue);
if(players[i].is_light){
if(players[i].is_light==1){
switch(players[i].type){
case nvwu: cout<<"女 巫";break;
case yuyanjia: cout<<"\b预言家";break;
case cunmin: cout<<"村 民";break;
case langren:cout<<"狼 人"; break;
case lieren:cout<<"猎 人"; break;
case shouwei:cout<<"守 卫"; break;
}
}else{
cout<<"好人";
}
}else{
cout<<"未知";
}
}
}
int is_end(){
int die_bad=0;
int die_people=0;
int die_god=0;
for(int i=0;i<player_number;i++){
if((players[i].type == nvwu || players[i].type == yuyanjia || players[i].type == shouwei)&&players[i].die_or_life==die)
die_god++;
else if(players[i].type == langren && players[i].die_or_life==die)
die_bad++;
else if(players[i].type == cunmin && players[i].die_or_life==die)
die_people++;
}
if((die_bad==die_people || die_bad==die_god)&&(die_bad>=nlangren))
return 3;
if(die_bad>=nlangren)
return 1;
if(die_people>=ncunmin||die_god>=(player_number>=10 ? 3:2))
return 2;
return 0;
}
void before_game(){
srand(time(NULL));
init_Show_Window();
color(green);
cout<<"欢迎来到狼人杀游戏\t\t\t注:10 11 12人局开设猎人 12人局开设守卫警长\n______________________\n";
cout<<"请输入玩家人数(8-12人):";
cin>>player_number;
while(player_number<8||player_number>12)
{ cout<<"请重新输入!\n"; cin>>player_number; }
system("cls");
cout<<"初始化身份中,请稍等.";
for(int i=0;i<6;i++){
for(int j=0;j<12;j++){
cout<<".";
Sleep(50);
}
cout<<"\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b";
}
system("cls");
init_players();
cout<<"我的号位:"<<my_number+1<<endl
<<"我的身份:";
switch(players[my_number].type){
case nvwu: cout<<"女巫\n";break;
case yuyanjia: cout<<"预言家\n";break;
case cunmin: cout<<"村民\n";break;
case langren:cout<<"狼人\n";break;
case lieren:cout<<"猎人\n"; break;
case shouwei:cout<<"守卫\n";break;
}
change_daytime();
system("pause");
system("cls");
cout<<"游戏加载中.";int ppppp=rand()%3+2;
for(int i=0;i<ppppp;i++){
for(int j=0;j<6;j++){
cout<<".";
Sleep(rand()%100+150);
}
cout<<"\b\b\b\b\b\b \b\b\b\b\b\b";
}
print();
}
void something_before_everyday(){
change_night();
system("cls");
print();
gotoxy(0,7);
cout<<"________________________";
gotoxy(0,8);
color(white);
cout<<"天黑~请闭眼~~~\n";
}
int shouweishou=0;
int ShouWei(){
color(blue);
cout<<"守卫~请睁眼~~\n";
Sleep(1500);
cout<<"你要守护的是?\n";
if(players[my_number].type==shouwei&&players[my_number].die_or_life == life){
cin>>shouweishou;
while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){
cout<<"请重新输入!\n";
cin>>shouweishou;
}
cout<<"你今晚要守护的是"<<shouweishou<<"号\n";
Sleep(1500);
shouweishou--;
}else{
if(players[idxshouwei].die_or_life == life){
shouweishou=rand()%10;
while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){
shouweishou=rand()%10;
}
}
}
Sleep(2000);
cout<<"守卫请闭眼"<<endl<<endl;
return shouweishou;
}
int LangRen(){
int langrensha=-1;
color(red);
cout<<"狼人~请睁眼~~~\n";
Sleep(1500);
cout<<"你们今晚要杀~谁~~??\n";
if(players[my_number].type==langren&&players[my_number].die_or_life == life){
cin>>langrensha;
while(!(langrensha>=1&&langrensha<=player_number&&players[langrensha-1].die_or_life==life)){
cout<<"请重新输入!\n";
cin>>langrensha;
}
cout<<"你们今晚要杀的是"<<langrensha--<<"号\n";
Sleep(3500);
}else{
while(langrensha==-1 || players[langrensha].die_or_life == die || players[langrensha].type==langren){
langrensha=rand()%player_number;
}
Sleep(3000);
}
cout<<"狼人请~闭眼~~\n\n";
return langrensha;
}
int nvwujiu=0,nvwudu=0,is_nvwujiu=0,is_nvwudu=0;
int NvWu(int langrensha){
color(purple);
cout<<"女巫~~请睁眼~~\n";
Sleep(2000);
if(players[my_number].type==nvwu&&players[my_number].die_or_life == life){
if(is_nvwujiu)
cout<<"你已经用过解药\n",Sleep(1500);
else
{
cout<<"今晚"<<langrensha+1<<"号死了,你想用解药吗?(1想 / 2不想)\n";
int is_nvwujie=0;
cin>>is_nvwujie;
while(is_nvwujie!=1&&is_nvwujie!=2){
cout<<"请重新输入\n";
cin>>is_nvwujie;
}
if(is_nvwujie==1) {
Sleep(1000);
cout<<"已经解救"<<langrensha+1<<"号\n";
nvwujiu=langrensha;
}
is_nvwujiu=1;
}
Sleep(1500);
if(::is_nvwudu) cout<<"你已经用过解药\n",Sleep(1500);
else
{
cout<<"你想用毒药吗?(1想 / 2不想)\n";
Sleep(1500);
int is_nvwudu=0;
cin>>is_nvwudu;
while(is_nvwudu!=1&&is_nvwudu!=2){
cout<<"请重新输入\n";
cin>>is_nvwudu;
}
if(is_nvwudu==1){
Sleep(1500);
cout<<"你想毒谁?\n";
cin>>nvwudu;
while(!(nvwudu>=1&&nvwudu<=player_number&&players[nvwudu].die_or_life==life)){
cout<<"请重新输入\n";
cin>>nvwudu;
}
nvwudu--;
Sleep(1500);
cout<<"已经毒死了"<<nvwudu+1<<"号\n";
}
::is_nvwudu=1;
}
}else{
if(players[idxnvwu].die_or_life == life){
if(!is_nvwujiu)
{
int is_jiu=rand()%8;
if(is_jiu==0){
nvwujiu=langrensha;
is_nvwujiu=1;
}
}
if(!is_nvwudu)
{
int is_du=rand()%4;
if(is_du==0){
int num=rand()%player_number;
nvwudu=num;
is_nvwudu=1;
}
}
}
}
cout<<"女巫~请闭眼~~\n\n";
return nvwujiu*10000+nvwudu;
}
int yuyanjiabixutoupiao=-1;
void YuYanJia(){
color(green);
cout<<"预言家~请睁眼~~\n";
Sleep(2000);
if(players[my_number].type==yuyanjia&&players[my_number].die_or_life == life){
cout<<"请问你想查验谁的身份\n";
int p;
cin>>p;
while(!(p>=1&&p<=player_number)){
cout<<"请重新输入!\n";
cin>>p;
}
Sleep(2000);
cout<<p<<"号的身份是——";
Sleep(1000);
if(players[p-1].type == langren){
cout<<"狼人\n";
players[p-1].is_light = 1;
}else{
cout<<"好人\n";
players[p-1].is_light = 2;
}
}else{
int p=-1;
while(p==-1||players[p].die_or_life==die||p==idxlieren)
p=rand()%player_number;
if(players[p].type==langren)
{
yuyanjiabixutoupiao=p;
}
}
cout<<"预言家~~请闭眼~~\n";
}
int LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1;
void Night(){
LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1;
if(player_number==12){
SHOUWEISHOU=ShouWei();
Sleep(2000);
}
LANGRENSHA=LangRen();
Sleep(3500);
int nvwu=NvWu(LANGRENSHA);
NVWUDU=nvwu%10+nvwu/10%10;
NVWUJIU=nvwu/10000%10+nvwu/100000%10;
Sleep(3000);
YuYanJia();
Sleep(2000);
}
void Lieren(){
int lierendai=-1;
cout<<idxlieren+1<<"号是猎人\n";
players[idxlieren].is_light = 1;
Sleep(1000);
if(idxlieren==my_number){
cout<<"你想带走几号?\n";
cin>>lierendai;
while(lierendai<1||lierendai>player_number||players[lierendai].die_or_life==die){
cout<<"请重新输入!\n";
cin>>lierendai;
}
lierendai--;
}else{
lierendai=rand()%player_number;
while(players[lierendai].die_or_life == die){
lierendai=rand()%player_number;
}
}
Sleep(2000);
cout<<"猎人选择带走"<<lierendai+1<<"号\n";
Sleep(2000);
players[lierendai].die_or_life = die;
}
void police_die();
void panduansiwang(){
system("cls");
print();
gotoxy(0,7);
cout<<"________________________\n";
Sleep(3000);
color(white);
cout<<"天亮了\n";
Sleep(2000);
gotoxy(0,9);
cout<<"昨晚";
bool is_die[15]={false},is_die_lieren=false,flag=false;
for(int i=0;i<player_number;i++)
{
if(players[i].die_or_life==life)
{
if(i==LANGRENSHA||i==NVWUDU) {
if(players[i].type==lieren) is_die_lieren=true;
players[i].killer= (i==LANGRENSHA ? langren:nvwu);
players[i].die_or_life=die;
is_die[i]=true;
}
if(i==SHOUWEISHOU||i==NVWUJIU) {
if(players[i].type==lieren) is_die_lieren=false;
players[i].killer=-1;
players[i].die_or_life=life;
is_die[i]=false;
}
}
}
bool is_police_die=false;
for(int i=0;i<player_number;i++)
{
if(is_die[i])
{
if(flag) cout<<"和"<<i+1<<"号";
else cout<<i+1<<"号",flag=true;
if(i==idx_police)
is_police_die=true;
}
}
if(flag) cout<<"死了\n";
else cout<<"是平安夜\n";
if(is_die_lieren) Lieren();
if(is_police_die) police_die();
}
void choose_police(){
system("cls");
print();
color(blue);
gotoxy(0,7);
cout<<"________________________\n";
color(yellow);
cout<<"下面开始选举警长,各位不能选举自己~\n";
int tong[100]={0},cannot[100],must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
memset(cannot,-1,sizeof(cannot));
CHOOSE:
color(yellow);
Sleep(1500);
for(int i=0;i<player_number;i++)
{
if(players[i].die_or_life==life&&!is_include(cannot,i,player_number))
{
if(i==my_number)
{
cout<<"你要选举几号?\n";
int n;
cin>>n;
while(n<1||n>player_number||n==i+1||players[n-1].die_or_life==die||!is_include(must,n-1,player_number))
{
cout<<"请重新输入!\n";
cin>>n;
}
cout<<i+1<<"号选举"<<n--<<"号\n";
tong[n]++;
}
else
{
int n=rand()%player_number;
while(n==i||players[n].die_or_life==die||!is_include(must,n,player_number))
n=rand()%player_number;
cout<<i+1<<"号选举"<<n+1<<"号\n";
tong[n]++;
}
Sleep(1500);
}
}
int idx_max=-1,maxn=-1,len=0;
for(int i=0;i<player_number;i++)
if(maxn<tong[i])
{
maxn=tong[i];
idx_max=i;
}
int maxn_arr[15]={0};
for(int i=0;i<player_number;i++)
{
if(tong[i]==maxn)
{
maxn_arr[len++]=i;
}
}
color(blue);
if(len>1)
{
for(int i=0;i<len;i++)
{
if(i==len-1)
{
cout<<maxn_arr[i]+1<<"号平票\n";
}
else
{
cout<<maxn_arr[i]+1<<"号,";
}
}
for(int i=0;i<len;i++)
cannot[i]=maxn_arr[i];
for(int i=0;i<player_number;i++)
{
if(is_include(cannot,i,len))
must[i]=i;
else
must[i]=-1;
}
color(white);
goto CHOOSE;
}
cout<<"恭喜"<<idx_max+1<<"号当选警长\n";
Sleep(3000);
idx_police=idx_max;
return;
}
int toupiao(){
int tong[100]={0},cannot[100]={},must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
memset(cannot,-1,sizeof(cannot));
gotoxy(0,7);
color(blue);
cout<<"________________________\n";
color(white);
cout<<"下面进入投票环节\n";
memset(tong,0,sizeof(tong));
Sleep(2000);
TOUPIAO:
for(int i=0;i<player_number;i++){
if(players[i].die_or_life == life&&!is_include(cannot,i,player_number)){
if(i==my_number){
color(white);
cout<<"你要投几号?\n";
int n;
cin>>n;
while(!(n>=1&&n<=player_number&&is_include(must,n-1,player_number))){
cout<<"请重新输入!\n";
cin>>n;
}
Sleep(2000);
cout<<setw(2)<<my_number+1<<"号投了"<<setw(2)<<n<<"号";
if(my_number==n-1) color(red),cout<<"快来看!这有个疯子投自己!";
if(i==idx_police) color(yellow),cout<<"(警长)\n"; else cout<<"\n";
if(i==idx_police) tong[n-1]++;
tong[n-1]++;
}else{
color(white);
int t=-1;
while(t==-1 || players[t].die_or_life == die || t==i || !is_include(must,t,player_number)){
if(i==idxyuyanjia&&yuyanjiabixutoupiao!=-1)
{
t=yuyanjiabixutoupiao;
yuyanjiabixutoupiao=-1;
continue;
}
t=rand()%player_number;
if(is_include(idxlangren,i,nlangren))
{
if(players[t].type == langren)
t=-1;
}
}
cout<<setw(2)<<i+1<<"号"<<"投了"<<setw(2)<<t+1<<"号";
if(i==idx_police) cout<<"(警长2票)\n"; else cout<<"\n";
if(i==idx_police) tong[t]++;
tong[t]++;
}
Sleep(rand()%1000+1000);
}
}
int idx_max=-1,maxn=-1,len=0;
for(int i=0;i<player_number;i++)
if(maxn<tong[i])
{
maxn=tong[i];
idx_max=i;
}
int maxn_arr[15]={0};
for(int i=0;i<player_number;i++)
{
if(tong[i]==maxn)
{
maxn_arr[len++]=i;
}
}
color(blue);
if(len>1)
{
for(int i=0;i<len;i++)
{
if(i==len-1)
{
cout<<maxn_arr[i]+1<<"号平票\n";
}
else
{
cout<<maxn_arr[i]+1<<"号,";
}
}
for(int i=0;i<len;i++)
cannot[i]=maxn_arr[i];
for(int i=0;i<player_number;i++)
{
if(is_include(cannot,i,len))
must[i]=i;
else
must[i]=-1;
}
color(white);
goto TOUPIAO;
}
cout<<idx_max+1<<"号"<<"出局\n";
Sleep(4000);
players[idx_max].die_or_life = die;
players[idx_max].killer = good;
return idx_max;
}
void police_die(){
color(yellow);
int type;
if(idx_police==my_number)
{
Sleep(1550);
cout<<"你是想撕毁警徽还是移交警徽?(撕毁输入1,移交输入2)";
cin>>type;
while(!(type==1||type==2))
{
cout<<"请重新输入!\n";
cin>>type;
}
}
else{
type=rand()%3+1;
}
if(type==1)
{
cout<<"警长选择撕毁警徽\n";
Sleep(1000);
idx_police=-1;
}
else
{
int lucky=-1;
while(lucky==-1||players[lucky].die_or_life==die)
lucky=rand()%player_number;
cout<<"警长选择把警徽移交给"<<lucky+1<<"号\n";
Sleep(1500);
idx_police=lucky;
}
}
void the_end(){
system("cls");
switch(is_end()){
case 1:cout<<"好人胜利\n\n"; break;
case 2:cout<<"狼人胜利\n\n"; break;
case 3:cout<<"本局平局\n\n"; break;
}
for(int i=0;i<player_number;i++){
cout<<i+1<<"号位:\t";
switch(players[i].type){
case nvwu: cout<<"女巫\t";break;
case yuyanjia: cout<<"预言家\t";break;
case cunmin: cout<<"村民\t";break;
case langren:cout<<"狼人\t";break;
case lieren:cout<<"猎人\t"; break;
case shouwei:cout<<"守卫\t";break;
}
cout<<"最终";
switch(players[i].killer){
case nvwu:cout<<"被女巫毒死\n"; break;
case langren:cout<<"被狼人杀死\n"; break;
case good:cout<<"被投票出局\n"; break;
case lieren:cout<<"被猎人带走\n";break;
default :cout<<"存活\n";
}
cout<<endl;
}
}
int main(){
int wheel=0;
before_game();
while(!is_end()){
something_before_everyday();
Sleep(1500);
Night();
change_daytime();
panduansiwang();
Sleep(2000);
system("cls");
print();
if(is_end()) break;
if(!wheel&&player_number==12)
{
choose_police();
system("cls");
print();
}
int idx_max=toupiao();
if(idx_max==idx_police){
police_die();
}
if(players[idx_max].type==lieren){
Lieren();
if(is_end()) break;
}
system("cls");
print();
wheel++;
}
the_end();
system("pause");
return 0;
}
9 条评论
-
-
#include<bits/stdc++.h> #include<windows.h> #include<conio.h> using namespace std; // 1. 补充缺失的枚举定义(核心修复) enum Phase { DAY = 0, NIGHT = 1 }; // 昼夜阶段 enum Role { VILLAGER = 0, // 村民 WEREWOLF = 1, // 狼人 SEER = 2, // 预言家 WITCH = 3, // 女巫 HUNTER = 4, // 猎人 GUARD = 5, // 守卫 GOOD = 6 // 好人投票(用于标记死因) }; enum Color { RED, GREEN, YELLOW, BLUE, WHITE, PURPLE // 控制台颜色 }; enum Status { DEAD = 1, ALIVE = 2 }; // 玩家状态 // 全局变量 int wolf_kill = -1; // 狼人击杀目标 int day = 0; // 当前天数 Phase current_phase = DAY; // 当前阶段(修复:用定义的枚举) int player_count; // 玩家总数 int my_seat; // 自己的座位号 HWND hwnd = GetForegroundWindow(); int police_seat = -1; // 警长座位号 int wolf_count = 0; // 狼人数量 int witch_seat = -1, guard_seat = -1, seer_seat = -1, hunter_seat = -1; int wolf_seats[4] = {-1, -1, -1, -1}; // 狼人座位号 bool witch_healed = false; // 女巫是否用过解药 bool witch_poisoned = false;// 女巫是否用过毒药 int heal_target = -1; // 解药目标 int poison_target = -1; // 毒药目标 int guard_protect = -1; // 守卫守护目标 int seer_check_target = -1;// 预言家查验目标 // 玩家结构体(修复:Role枚举已定义) struct Player { Role role = (Role)-1; Status status = ALIVE; bool known = false; int killer = INT_MAX; }; Player players[100]; // 控制台颜色设置(修复:Color枚举已定义) void set_color(Color c) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); switch(c) { case RED: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED); break; case GREEN: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_GREEN); break; case YELLOW: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); break; case BLUE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_BLUE); break; case WHITE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; case PURPLE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); break; } } // 控制台光标定位 void gotoxy(int x, int y) { COORD pos = { (SHORT)x, (SHORT)y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } // 初始化窗口 void init_window() { system("mode con lines=60 cols=188"); ShowWindow(hwnd, SW_MAXIMIZE); DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(GetConsoleWindow()); } // 检查数组是否包含指定值 bool array_contains(int arr[], int val, int len) { for(int i = 0; i < len; i++) { if(arr[i] == val) return true; } return false; } // 随机选一个存活玩家(可排除指定身份) int rand_alive_player(Role exclude_role = (Role)-1) { int target = -1; while(true) { target = rand() % player_count; if(players[target].status == ALIVE && players[target].role != exclude_role) { break; } } return target; } // 初始化玩家身份 void init_players() { // 确定狼人数量 if(player_count == 12) wolf_count = 4; else if(player_count >= 10) wolf_count = 3; else wolf_count = 2; // 初始化所有玩家 for(int i = 0; i < player_count; i++) { players[i].status = ALIVE; players[i].known = false; players[i].killer = INT_MAX; players[i].role = (Role)-1; } // 分配狼人身份 int wolf_idx = 0; while(wolf_idx < wolf_count) { int seat = rand() % player_count; if(!array_contains(wolf_seats, seat, 4)) { wolf_seats[wolf_idx++] = seat; players[seat].role = WEREWOLF; } Sleep(rand() % 80 + 100); } // 分配守卫(仅12人局) if(player_count == 12) { do { guard_seat = rand() % player_count; } while(players[guard_seat].role != (Role)-1); players[guard_seat].role = GUARD; } // 分配女巫 do { witch_seat = rand() % player_count; } while(players[witch_seat].role != (Role)-1); players[witch_seat].role = WITCH; // 分配猎人(≥10人局) if(player_count >= 10) { do { hunter_seat = rand() % player_count; } while(players[hunter_seat].role != (Role)-1); players[hunter_seat].role = HUNTER; } // 分配预言家 do { seer_seat = rand() % player_count; } while(players[seer_seat].role != (Role)-1); players[seer_seat].role = SEER; // 剩余玩家为村民 for(int i = 0; i < player_count; i++) { if(players[i].role == (Role)-1) { players[i].role = VILLAGER; } } // 随机分配自己的座位,并标记已知身份 my_seat = rand() % player_count; players[my_seat].known = true; if(players[my_seat].role == WEREWOLF) { // 狼人可见所有狼队友 for(int i = 0; i < wolf_count; i++) { players[wolf_seats[i]].known = true; } } } // 绘制游戏UI void print_ui() { gotoxy(0, 0); set_color(WHITE); cout << "作者:chenzhan"; gotoxy(90, 0); if(current_phase == NIGHT) set_color(RED); else set_color(BLUE); printf("第%d天 | %s\n", day, current_phase == NIGHT ? "黑夜" : "白天"); gotoxy(0, 3); set_color(BLUE); cout << " 我的号位:" << my_seat + 1; // 绘制所有玩家状态 for(int i = 0; i < player_count; i++) { gotoxy(i * 8 + 1, 4); if(i == police_seat) set_color(YELLOW); else set_color(BLUE); cout << i + 1 << "号位"; gotoxy(i * 8 + 1, 5); if(players[i].status == DEAD) { set_color(RED); cout << "死 亡"; } else { set_color(GREEN); cout << "存 活"; } gotoxy(i * 8 + 1, 6); set_color(BLUE); if(players[i].known) { switch(players[i].role) { case WITCH: cout << "女 巫"; break; case SEER: cout << "预言家"; break; case VILLAGER: cout << "村 民"; break; case WEREWOLF: cout << "狼 人"; break; case HUNTER: cout << "猎 人"; break; case GUARD: cout << "守 卫"; break; default: cout << "好人"; } } else { cout << "未知"; } } set_color(WHITE); } // 检查游戏是否结束 int check_game_end() { int dead_wolf = 0, dead_villager = 0, dead_god = 0; int alive_wolf = 0, alive_good = 0; for(int i = 0; i < player_count; i++) { if(players[i].status == DEAD) { if(players[i].role == WEREWOLF) dead_wolf++; else if(players[i].role == VILLAGER) dead_villager++; else dead_god++; // 神牌(女巫/预言家/猎人/守卫) } else { if(players[i].role == WEREWOLF) alive_wolf++; else alive_good++; } } // 狼人全灭 → 好人胜利 if(alive_wolf == 0) return 1; // 计算神牌总数和村民总数 int god_total = (witch_seat != -1) + (seer_seat != -1) + (hunter_seat != -1) + (guard_seat != -1); int villager_total = player_count - god_total - wolf_count; // 神牌全灭 或 村民全灭 → 狼人胜利 if(dead_god >= god_total || dead_villager >= villager_total) return 2; // 狼人数 = 好人数 → 平局 if(alive_wolf == alive_good) return 3; // 游戏继续 return 0; } // 游戏初始化阶段 void pre_game() { srand(time(NULL)); init_window(); set_color(GREEN); cout << "欢迎来到狼人杀游戏\t\t\t注:10/11/12人局开设猎人 12人局开设守卫/警长\n"; cout << "----------------------------------------\n"; cout << "请输入玩家人数(8-12人): "; cin >> player_count; while(player_count < 8 || player_count > 12) { cout << "人数不符合要求,请重新输入(8-12): "; cin >> player_count; } system("cls"); cout << "初始化身份中,请稍等."; for(int i = 0; i < 6; i++) { for(int j = 0; j < 12; j++) { cout << "."; Sleep(50); } cout << "\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b"; } system("cls"); init_players(); cout << "我的号位: " << my_seat + 1 << endl; cout << "我的身份: "; switch(players[my_seat].role) { case WITCH: cout << "女巫\n"; break; case SEER: cout << "预言家\n"; break; case VILLAGER: cout << "村民\n"; break; case WEREWOLF: cout << "狼人\n"; break; case HUNTER: cout << "猎人\n"; break; case GUARD: cout << "守卫\n"; break; } current_phase = DAY; day = 1; system("pause"); system("cls"); cout << "游戏加载中."; int load_times = rand() % 3 + 2; for(int i = 0; i < load_times; i++) { for(int j = 0; j < 6; j++) { cout << "."; Sleep(rand() % 100 + 150); } cout << "\b\b\b\b\b\b \b\b\b\b\b\b"; } system("cls"); print_ui(); } // 守卫行动阶段 void guard_action() { set_color(BLUE); cout << "守卫~请睁眼~~\n"; Sleep(1500); guard_protect = -1; if(players[my_seat].role == GUARD && players[my_seat].status == ALIVE) { cout << "你要守护的是? (输入座位号1-" << player_count << "): "; cin >> guard_protect; guard_protect--; // 转成0索引 while(guard_protect < 0 || guard_protect >= player_count || players[guard_protect].status == DEAD) { cout << "输入无效,请重新输入存活玩家的座位号: "; cin >> guard_protect; guard_protect--; } cout << "你今晚要守护的是" << guard_protect + 1 << "号\n"; } else if(guard_seat != -1 && players[guard_seat].status == ALIVE) { // AI守卫随机守护 guard_protect = rand_alive_player(); } Sleep(2000); cout << "守卫请闭眼\n\n"; } // 狼人行动阶段(返回击杀目标) int wolf_action() { set_color(RED); cout << "狼人~请睁眼~~~\n"; Sleep(1500); int kill_target = -1; if(players[my_seat].role == WEREWOLF && players[my_seat].status == ALIVE) { cout << "你们今晚要杀谁?(输入座位号1-" << player_count << "): "; cin >> kill_target; kill_target--; while(kill_target < 0 || kill_target >= player_count || players[kill_target].status == DEAD || players[kill_target].role == WEREWOLF) { cout << "输入无效,请重新输入非狼人存活玩家的座位号: "; cin >> kill_target; kill_target--; } cout << "你们今晚要杀的是" << kill_target + 1 << "号\n"; } else { // AI狼人随机击杀(排除狼人) kill_target = rand_alive_player(WEREWOLF); } Sleep(3500); cout << "狼人请~闭眼~~\n\n"; return kill_target; } // 女巫行动阶段 void witch_action(int wolf_kill) { set_color(PURPLE); cout << "女巫~~请睁眼~~\n"; Sleep(2000); heal_target = -1; poison_target = -1; if(players[my_seat].role == WITCH && players[my_seat].status == ALIVE) { // 解药环节 if(!witch_healed) { cout << "今晚" << wolf_kill + 1 << "号被狼人杀死,你想用解药吗?(1=想 / 2=不想): "; int choice; cin >> choice; while(choice != 1 && choice != 2) { cout << "输入无效,请输入1或2: "; cin >> choice; } if(choice == 1) { heal_target = wolf_kill; witch_healed = true; cout << "已解救" << wolf_kill + 1 << "号\n"; } } else { cout << "你已经用过解药\n"; Sleep(1500); } Sleep(1500); // 毒药环节 if(!witch_poisoned) { cout << "你想用毒药吗?(1=想 / 2=不想): "; int choice; cin >> choice; while(choice != 1 && choice != 2) { cout << "输入无效,请输入1或2: "; cin >> choice; } if(choice == 1) { cout << "你想毒谁?(输入座位号1-" << player_count << "): "; cin >> poison_target; poison_target--; while(poison_target < 0 || poison_target >= player_count || players[poison_target].status == DEAD) { cout << "输入无效,请重新输入存活玩家的座位号: "; cin >> poison_target; poison_target--; } witch_poisoned = true; cout << "已毒死" << poison_target + 1 << "号\n"; } } else { cout << "你已经用过毒药\n"; Sleep(1500); } } else if(players[witch_seat].status == ALIVE) { // AI女巫随机使用解药/毒药 if(!witch_healed && rand() % 10 == 0) { // 10%概率用解药 heal_target = wolf_kill; witch_healed = true; } if(!witch_poisoned && rand() % 4 == 0) { // 25%概率用毒药 poison_target = rand_alive_player(); witch_poisoned = true; } } Sleep(1500); cout << "女巫~请闭眼~~\n\n"; } // 预言家行动阶段 void seer_action() { set_color(GREEN); cout << "预言家~请睁眼~~\n"; Sleep(2000); seer_check_target = -1; if(players[my_seat].role == SEER && players[my_seat].status == ALIVE) { cout << "你想查验谁的身份?(输入座位号1-" << player_count << "): "; int check; cin >> check; check--; while(check < 0 || check >= player_count || players[check].status == DEAD) { cout << "输入无效,请重新输入存活玩家的座位号: "; cin >> check; check--; } Sleep(2000); cout << check + 1 << "号的身份是——"; Sleep(1000); if(players[check].role == WEREWOLF) { cout << "狼人\n"; players[check].known = true; } else { cout << "好人\n"; players[check].known = true; } } else if(players[seer_seat].status == ALIVE) { // AI预言家随机查验 seer_check_target = rand_alive_player(); } Sleep(1000); cout << "预言家~~请闭眼~~\n\n"; } // 黑夜阶段总流程 void night_phase() { current_phase = NIGHT; system("cls"); print_ui(); gotoxy(0, 7); cout << "----------------------------------------\n"; set_color(WHITE); cout << "天黑~请闭眼~~~\n"; Sleep(1500); // 守卫行动(仅12人局) if(player_count == 12) { guard_action(); Sleep(2000); } // 狼人行动 wolf_kill = wolf_action(); Sleep(3500); // 女巫行动 witch_action(wolf_kill); Sleep(3000); // 预言家行动 seer_action(); Sleep(2000); current_phase = DAY; } // 猎人技能触发 void hunter_skill(int dead_seat) { if(players[dead_seat].role != HUNTER || players[dead_seat].status == ALIVE) return; set_color(RED); cout << dead_seat + 1 << "号是猎人,触发技能!\n"; Sleep(1000); int take_seat = -1; if(dead_seat == my_seat) { cout << "你想带走几号?(输入座位号1-" << player_count << "): "; cin >> take_seat; take_seat--; while(take_seat < 0 || take_seat >= player_count || players[take_seat].status == DEAD) { cout << "输入无效,请重新输入存活玩家的座位号: "; cin >> take_seat; take_seat--; } } else { // AI猎人随机带走 take_seat = rand_alive_player(); } Sleep(2000); cout << "猎人选择带走" << take_seat + 1 << "号\n"; players[take_seat].status = DEAD; players[take_seat].killer = HUNTER; Sleep(3000); } // 公布夜晚结果 void announce_night_result() { system("cls"); print_ui(); gotoxy(0, 7); cout << "----------------------------------------\n"; Sleep(3000); set_color(WHITE); cout << "天亮了\n"; Sleep(2000); vector<int> dead_players; // 处理狼人击杀(未被守卫守护且未被女巫救) if(wolf_kill != -1 && wolf_kill != guard_protect && wolf_kill != heal_target) { players[wolf_kill].status = DEAD; players[wolf_kill].killer = WEREWOLF; dead_players.push_back(wolf_kill); } // 处理女巫毒药 if(poison_target != -1) { players[poison_target].status = DEAD; players[poison_target].killer = WITCH; dead_players.push_back(poison_target); } // 公布死亡信息 cout << "昨晚"; if(dead_players.empty()) { cout << "是平安夜\n"; } else { for(int i = 0; i < dead_players.size(); i++) { if(i > 0) cout << "和"; cout << dead_players[i] + 1 << "号"; if(dead_players[i] == police_seat) { cout << "(警长)"; } } cout << "死了\n"; // 触发猎人技能 for(int seat : dead_players) { hunter_skill(seat); } } Sleep(3000); } // 选举警长(仅12人局第一天) void elect_police() { system("cls"); print_ui(); gotoxy(0, 7); cout << "----------------------------------------\n"; set_color(YELLOW); cout << "开始选举警长,不能选举自己!\n"; Sleep(2000); int votes[100] = {0}; int cannot_vote[100] = {-1}; int cannot_len = 0; while(true) { memset(votes, 0, sizeof(votes)); // 投票流程 for(int i = 0; i < player_count; i++) { if(players[i].status == DEAD || array_contains(cannot_vote, i, cannot_len)) { continue; } int vote_target = -1; if(i == my_seat) { cout << "你要选举谁当警长?(输入座位号1-" << player_count << "): "; cin >> vote_target; vote_target--; while(vote_target < 0 || vote_target >= player_count || vote_target == i || players[vote_target].status == DEAD || array_contains(cannot_vote, vote_target, cannot_len)) { cout << "输入无效,请重新输入: "; cin >> vote_target; vote_target--; } cout << i + 1 << "号选举" << vote_target + 1 << "号\n"; } else { // AI投票(排除自己、死亡、平票者) do { vote_target = rand() % player_count; } while(vote_target == i || players[vote_target].status == DEAD || array_contains(cannot_vote, vote_target, cannot_len)); cout << i + 1 << "号选举" << vote_target + 1 << "号\n"; } // 警长票权重2倍(首次选举无警长,此逻辑为后续重选) votes[vote_target] += (i == police_seat) ? 2 : 1; Sleep(1500); } // 统计最高票 int max_vote = -1; vector<int> max_seats; for(int i = 0; i < player_count; i++) { if(votes[i] > max_vote) { max_vote = votes[i]; max_seats.clear(); max_seats.push_back(i); } else if(votes[i] == max_vote) { max_seats.push_back(i); } } // 唯一最高票 → 当选警长 if(max_seats.size() == 1) { police_seat = max_seats[0]; cout << "恭喜" << police_seat + 1 << "号当选警长!\n"; Sleep(3000); break; } else { // 平票 → 重选(平票者不能参与) cout << "平票:"; cannot_len = 0; for(int seat : max_seats) { cout << seat + 1 << "号 "; cannot_vote[cannot_len++] = seat; } cout << "进入重选,平票者不能参与\n"; Sleep(2000); } } } // 警长死亡处理(撕毁/移交) void police_death() { set_color(YELLOW); cout << "警长死亡,处理警徽!\n"; Sleep(1500); int choice = 1; // 1=撕毁,2=移交 if(police_seat == my_seat) { cout << "你想撕毁警徽还是移交警徽?(1=撕毁 / 2=移交): "; cin >> choice; while(choice != 1 && choice != 2) { cout << "输入无效,请输入1或2: "; cin >> choice; } } else { // AI随机选择 choice = rand() % 2 + 1; } if(choice == 1) { cout << "警长选择撕毁警徽\n"; police_seat = -1; } else { // 移交给随机存活玩家 int new_police = rand_alive_player(); cout << "警长选择把警徽移交给" << new_police + 1 << "号\n"; police_seat = new_police; } Sleep(2000); } // 白天投票放逐阶段 int vote_phase() { system("cls"); print_ui(); gotoxy(0, 7); cout << "----------------------------------------\n"; set_color(WHITE); cout << "进入投票环节,选出要放逐的玩家!\n"; Sleep(2000); int votes[100] = {0}; int cannot_vote[100] = {-1}; int cannot_len = 0; while(true) { memset(votes, 0, sizeof(votes)); // 投票流程 for(int i = 0; i < player_count; i++) { if(players[i].status == DEAD || array_contains(cannot_vote, i, cannot_len)) { continue; } int vote_target = -1; if(i == my_seat) { cout << "你要投谁出局?(输入座位号1-" << player_count << "): "; cin >> vote_target; vote_target--; while(vote_target < 0 || vote_target >= player_count || players[vote_target].status == DEAD) { cout << "输入无效,请重新输入: "; cin >> vote_target; vote_target--; } cout << i + 1 << "号投了" << vote_target + 1 << "号"; if(i == police_seat) cout << "(警长,2票)"; cout << endl; } else { // AI投票逻辑 do { vote_target = rand() % player_count; // 预言家会投自己查验出的狼人 if(i == seer_seat && seer_check_target != -1 && players[seer_check_target].role == WEREWOLF) { vote_target = seer_check_target; seer_check_target = -1; break; } // 狼人不会投狼队友 if(array_contains(wolf_seats, i, wolf_count) && players[vote_target].role == WEREWOLF) { vote_target = -1; continue; } } while(vote_target == -1 || players[vote_target].status == DEAD); cout << i + 1 << "号投了" << vote_target + 1 << "号"; if(i == police_seat) cout << "(警长,2票)"; cout << endl; } // 警长票权重2倍 votes[vote_target] += (i == police_seat) ? 2 : 1; Sleep(rand() % 1000 + 1000); } // 统计最高票 int max_vote = -1; vector<int> max_seats; for(int i = 0; i < player_count; i++) { if(votes[i] > max_vote) { max_vote = votes[i]; max_seats.clear(); max_seats.push_back(i); } else if(votes[i] == max_vote) { max_seats.push_back(i); } } // 唯一最高票 → 出局 if(max_seats.size() == 1) { int out_seat = max_seats[0]; cout << out_seat + 1 << "号获得最高票,出局!\n"; players[out_seat].status = DEAD; players[out_seat].killer = GOOD; Sleep(4000); // 处理警长出局 if(out_seat == police_seat) { police_death(); } // 触发猎人技能 hunter_skill(out_seat); return out_seat; } else { // 平票 → 重选(平票者不能参与) cout << "平票:"; cannot_len = 0; for(int seat : max_seats) { cout << seat + 1 << "号 "; cannot_vote[cannot_len++] = seat; } cout << "进入重选!\n"; Sleep(3000); } } } // 游戏结束结算 void game_over() { system("cls"); set_color(WHITE); int result = check_game_end(); switch(result) { case 1: cout << "🎉 好人阵营胜利!🎉\n\n"; break; case 2: cout << "🐺 狼人阵营胜利!🐺\n\n"; break; case 3: cout << "🤝 本局平局!🤝\n\n"; break; } cout << "===== 最终结算 =====\n"; for(int i = 0; i < player_count; i++) { cout << i + 1 << "号位: \t"; switch(players[i].role) { case WITCH: cout << "女巫\t"; break; case SEER: cout << "预言家\t"; break; case VILLAGER: cout << "村民\t"; break; case WEREWOLF: cout << "狼人\t"; break; case HUNTER: cout << "猎人\t"; break; case GUARD: cout << "守卫\t"; break; } cout << "最终: "; if(players[i].status == ALIVE) { cout << "存活\n"; } else { switch(players[i].killer) { case WEREWOLF: cout << "被狼人杀死\n"; break; case WITCH: cout << "被女巫毒死\n"; break; case GOOD: cout << "被投票出局\n"; break; case HUNTER: cout << "被猎人带走\n"; break; default: cout << "死亡(未知原因)\n"; } } } set_color(WHITE); } // 主函数 int main() { // 循环2局游戏(可修改次数) for(int i=0; i<=1; i++) { // 重置全局变量(避免多局数据污染) wolf_kill = -1; day = 0; current_phase = DAY; police_seat = -1; wolf_count = 0; witch_seat = -1; guard_seat = -1; seer_seat = -1; hunter_seat = -1; memset(wolf_seats, -1, sizeof(wolf_seats)); witch_healed = false; witch_poisoned = false; heal_target = -1; poison_target = -1; guard_protect = -1; seer_check_target = -1; memset(players, 0, sizeof(players)); pre_game(); int round = 0; while(!check_game_end()) { night_phase(); // 黑夜行动 announce_night_result();// 公布夜晚结果 if(check_game_end()) break; system("cls"); print_ui(); // 12人局第一天选举警长 if(round == 0 && player_count == 12 && police_seat == -1) { elect_police(); system("cls"); print_ui(); } vote_phase(); // 白天投票 if(check_game_end()) break; day++; round++; system("cls"); print_ui(); } game_over(); system("pause"); system("cls"); } return 0; } -
打开项目 → 工具 → 编译选项 → 切换到 “连接器” 标签页 → 在 “连接器命令行” 输入框中:-static-libgcc -lwinmm -std=c++11
#include<bits/stdc++.h> #include<windows.h> #include<bits/stdc++.h> using namespace std; enum TimeState { DAY = 0, NIGHT = 1 }; enum Color { BLUE = 0, YELLOW = 1, RED = 2, GREEN = 3, PURPLE = 4, WHITE = 5 }; enum Role { WITCH = 0, // 女巫 VILLAGER = 1, // 村民 SEER = 2, // 预言家 WEREWOLF = 3, // 狼人 HUNTER = 4, // 猎人 GUARD = 5, // 守卫 GOOD = 6 // 好人(投票) }; enum Status { DEAD = 1, ALIVE = 2 }; int day = 0; // 当前天数 TimeState currentTime = DAY;// 当前昼夜状态 int playerCount = 0; // 玩家总数 int myId = -1; // 自己的编号(0开始) HWND hwnd = GetForegroundWindow(); int policeId = -1; // 警长编号 int witchCnt = 0, villagerCnt = 0, seerCnt = 0, wolfCnt = 0, hunterCnt = 0, guardCnt = 0; int witchId = -1, guardId = -1, seerId = -1, hunterId = -1; int wolfIds[4] = {-1, -1, -1, -1}; // 狼人编号 int wolfKill = -1; // 狼人击杀目标 int guardProtect = -1;// 守卫守护目标 int witchSave = -1; // 女巫解药目标 int witchPoison = -1; // 女巫毒药目标 bool witchHasSave = false; // 女巫是否用过解药 bool witchHasPoison = false;// 女巫是否用过毒药 int seerCheck = -1; // 预言家查验目标 struct Player { Role role = (Role)-1; // 角色类型 Status status = ALIVE; // 生存状态 bool isKnown = false; // 是否已知身份(自己视角) int killer = INT_MAX; // 击杀者(角色类型) }; Player players[1000]; void setColor(Color c) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); switch(c) { case RED: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED); break; case GREEN: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_GREEN); break; case YELLOW: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); break; case BLUE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_BLUE); break; case WHITE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; case PURPLE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); break; } } void gotoXY(int x, int y) { COORD pos = { (SHORT)x, (SHORT)y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } bool isInArray(int arr[], int val, int len) { for(int i = 0; i < len; i++) { if(arr[i] == val) return true; } return false; } int getRandomPlayer(Role excludeRole = (Role)-1) { int p = -1; while(true) { p = rand() % playerCount; if(players[p].status == ALIVE && (excludeRole == (Role)-1 || players[p].role != excludeRole)) { break; } } return p; } int inputValidPlayer(const string& prompt) { int p; cout << prompt; while(true) { cin >> p; p--; // 转为0开始 if(p >= 0 && p < playerCount && players[p].status == ALIVE) { break; } cout << "输入无效!请输入存活玩家编号(1~" << playerCount << "):"; } return p; } void initWindow() { system("mode con lines=60 cols=188"); ShowWindow(hwnd, SW_MAXIMIZE); DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(GetConsoleWindow()); } void initPlayers() { myId = rand() % playerCount; if(playerCount == 12) wolfCnt = 4; else if(playerCount >= 10) wolfCnt = 3; else wolfCnt = 2; for(int i = 0; i < playerCount; i++) { players[i].status = ALIVE; players[i].isKnown = false; players[i].killer = INT_MAX; } int wolfIdx = 0; while(wolfIdx < wolfCnt) { int p = rand() % playerCount; if(!isInArray(wolfIds, p, 4)) { wolfIds[wolfIdx++] = p; players[p].role = WEREWOLF; } } if(playerCount == 12) { do { guardId = rand() % playerCount; } while(players[guardId].role != (Role)-1); players[guardId].role = GUARD; guardCnt = 1; } do { witchId = rand() % playerCount; } while(players[witchId].role != (Role)-1); players[witchId].role = WITCH; witchCnt = 1; if(playerCount >= 10) { do { hunterId = rand() % playerCount; } while(players[hunterId].role != (Role)-1); players[hunterId].role = HUNTER; hunterCnt = 1; } do { seerId = rand() % playerCount; } while(players[seerId].role != (Role)-1); players[seerId].role = SEER; seerCnt = 1; for(int i = 0; i < playerCount; i++) { if(players[i].role == (Role)-1) { players[i].role = VILLAGER; villagerCnt++; } } players[myId].isKnown = true; if(players[myId].role == WEREWOLF) { for(int i = 0; i < wolfCnt; i++) { players[wolfIds[i]].isKnown = true; } } } void printGameUI() { system("cls"); gotoXY(0, 0); cout << "作者:chenzhan"; gotoXY(90, 0); currentTime == NIGHT ? setColor(RED) : setColor(BLUE); printf("第%d天 | %s\n", day, currentTime == NIGHT ? "黑夜" : "白天"); gotoXY(0, 3); setColor(BLUE); cout << " 我的号位:" << myId + 1; for(int i = 0; i < playerCount; i++) { gotoXY(i * 8 + 1, 4); i == policeId ? setColor(YELLOW) : setColor(BLUE); cout << i + 1 << "号位"; gotoXY(i * 8 + 1, 5); players[i].status == DEAD ? setColor(RED) : setColor(GREEN); cout << (players[i].status == DEAD ? "死 亡" : "存 活"); gotoXY(i * 8 + 1, 6); setColor(BLUE); if(players[i].isKnown) { switch(players[i].role) { case WITCH: cout << "女 巫"; break; case SEER: cout << "预言家"; break; case VILLAGER: cout << "村 民"; break; case WEREWOLF: cout << "狼 人"; break; case HUNTER: cout << "猎 人"; break; case GUARD: cout << "守 卫"; break; default: cout << "好人"; } } else { cout << "未知"; } } setColor(WHITE); // 重置颜色 } int checkGameEnd() { int deadWolf = 0, deadVillager = 0, deadGod = 0; for(int i = 0; i < playerCount; i++) { if(players[i].status == DEAD) { if(players[i].role == WEREWOLF) deadWolf++; else if(players[i].role == VILLAGER) deadVillager++; else deadGod++; // 女巫/预言家/猎人/守卫视为神 } } if(deadWolf >= wolfCnt) return 1; int godTotal = witchCnt + seerCnt + hunterCnt + guardCnt; if(deadGod >= godTotal || deadVillager >= villagerCnt) return 2; if((deadWolf == deadVillager || deadWolf == deadGod) && deadWolf >= wolfCnt) return 3; return 0; } void preGame() { srand(time(NULL)); initWindow(); setColor(GREEN); cout << "欢迎来到狼人杀游戏\t\t\t注:10/11/12人局开设猎人 12人局开设守卫/警长\n"; cout << "====================\n请输入玩家人数(8-12人):"; cin >> playerCount; while(playerCount < 8 || playerCount > 12) { cout << "人数无效!请重新输入(8-12):"; cin >> playerCount; } system("cls"); cout << "初始化身份中,请稍等."; for(int i = 0; i < 3; i++) { for(int j = 0; j < 10; j++) { cout << "."; Sleep(50); } cout << "\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b"; } initPlayers(); system("cls"); cout << "你的身份信息:\n"; cout << "我的号位:" << myId + 1 << "\n我的身份:"; switch(players[myId].role) { case WITCH: cout << "女巫\n"; break; case SEER: cout << "预言家\n"; break; case VILLAGER: cout << "村民\n"; break; case WEREWOLF: cout << "狼人\n"; break; case HUNTER: cout << "猎人\n"; break; case GUARD: cout << "守卫\n"; break; } day = 1; // 第一天 system("pause"); printGameUI(); } void nightPhase() { currentTime = NIGHT; printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(WHITE); cout << "天黑~请闭眼~~~\n"; Sleep(1500); guardProtect = -1; if(playerCount == 12 && players[guardId].status == ALIVE) { setColor(BLUE); cout << "守卫~请睁眼~~\n"; Sleep(1500); if(players[myId].role == GUARD && players[myId].status == ALIVE) { guardProtect = inputValidPlayer("你要守护的玩家编号:"); cout << "你选择守护" << guardProtect + 1 << "号\n"; } else { guardProtect = getRandomPlayer(); // AI随机守护 } Sleep(1500); cout << "守卫请闭眼~~\n\n"; Sleep(1000); } wolfKill = -1; setColor(RED); cout << "狼人~请睁眼~~~\n"; Sleep(1500); cout << "选择猎杀目标:\n"; if(players[myId].role == WEREWOLF && players[myId].status == ALIVE) { wolfKill = inputValidPlayer("你要击杀的玩家编号:"); cout << "狼人选择猎杀" << wolfKill + 1 << "号\n"; } else { wolfKill = getRandomPlayer(WEREWOLF); // AI随机击杀(排除狼人) } Sleep(2000); cout << "狼人请闭眼~~\n\n"; Sleep(1000); witchSave = -1; witchPoison = -1; setColor(PURPLE); cout << "女巫~请睁眼~~\n"; Sleep(2000); if(players[myId].role == WITCH && players[myId].status == ALIVE) { if(!witchHasSave) { cout << "今晚" << wolfKill + 1 << "号被击杀,是否使用解药?(1=是/2=否):"; int choice; cin >> choice; if(choice == 1) { witchSave = wolfKill; witchHasSave = true; cout << "已使用解药解救" << wolfKill + 1 << "号\n"; } } else { cout << "你已使用解药\n"; } Sleep(1500); if(!witchHasPoison) { cout << "是否使用毒药?(1=是/2=否):"; int choice; cin >> choice; if(choice == 1) { witchPoison = inputValidPlayer("你要毒杀的玩家编号:"); witchHasPoison = true; cout << "已使用毒药毒杀" << witchPoison + 1 << "号\n"; } } else { cout << "你已使用毒药\n"; } } else { if(!witchHasSave && rand() % 3 == 0) { // 30%概率救 witchSave = wolfKill; witchHasSave = true; } if(!witchHasPoison && rand() % 5 == 0) { // 20%概率毒 witchPoison = getRandomPlayer(); witchHasPoison = true; } } Sleep(1500); cout << "女巫请闭眼~~\n\n"; Sleep(1000); seerCheck = -1; setColor(GREEN); cout << "预言家~请睁眼~~\n"; Sleep(2000); if(players[myId].role == SEER && players[myId].status == ALIVE) { seerCheck = inputValidPlayer("你要查验的玩家编号:"); cout << seerCheck + 1 << "号的身份是——"; Sleep(1000); if(players[seerCheck].role == WEREWOLF) { cout << "狼人\n"; } else { cout << "好人\n"; players[seerCheck].isKnown = true; // 标记为好人 } players[seerCheck].isKnown = true; } else { seerCheck = getRandomPlayer(); // AI随机查验 } Sleep(1500); cout << "预言家请闭眼~~\n"; Sleep(1000); currentTime = DAY; } void hunterSkill(int hunterId, bool isPoisoned) { if(isPoisoned) { cout << hunterId + 1 << "号猎人被毒死,无法开枪!\n"; return; } cout << hunterId + 1 << "号猎人发动技能!\n"; int target = -1; if(hunterId == myId) { target = inputValidPlayer("你要带走的玩家编号:"); } else { target = getRandomPlayer(); // AI随机带走 } players[target].status = DEAD; players[target].killer = HUNTER; cout << "猎人带走了" << target + 1 << "号!\n"; Sleep(2000); } void announceNightResult() { printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(WHITE); cout << "天亮了!\n"; Sleep(2000); vector<int> deadPlayers; if(wolfKill != -1 && wolfKill != guardProtect && wolfKill != witchSave) { players[wolfKill].status = DEAD; players[wolfKill].killer = WEREWOLF; deadPlayers.push_back(wolfKill); } if(witchPoison != -1) { players[witchPoison].status = DEAD; players[witchPoison].killer = WITCH; deadPlayers.push_back(witchPoison); } if(deadPlayers.empty()) { cout << "昨晚是平安夜!\n"; } else { cout << "昨晚死亡的玩家:"; for(int i = 0; i < deadPlayers.size(); i++) { if(i > 0) cout << "、"; cout << deadPlayers[i] + 1 << "号"; if(deadPlayers[i] == policeId) { cout << "(警长)"; } } cout << "\n"; } Sleep(2000); for(int p : deadPlayers) { if(players[p].role == HUNTER) { bool isPoisoned = (p == witchPoison); hunterSkill(p, isPoisoned); } } for(int p : deadPlayers) { if(p == policeId) { cout << "警长死亡,"; int choice = (p == myId) ? inputValidPlayer("撕毁警徽(1) / 移交警徽(2):") : (rand() % 2 + 1); if(choice == 1) { cout << "警徽已撕毁!\n"; policeId = -1; } else { int newPolice = getRandomPlayer(); cout << "警徽移交给" << newPolice + 1 << "号!\n"; policeId = newPolice; } Sleep(2000); } } } void electPolice() { printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(YELLOW); cout << "开始选举警长!(不能选自己)\n"; Sleep(1500); int votes[100] = {0}; vector<int> cannotVote; // 平票者(重投时排除) vector<int> mustVote; // 重投时仅能投这些人 while(true) { memset(votes, 0, sizeof(votes)); for(int i = 0; i < playerCount; i++) { if(players[i].status == ALIVE && !isInArray(&cannotVote[0], i, cannotVote.size())) { int target = -1; if(i == myId) { target = inputValidPlayer("你要选举的警长编号:"); while(target == myId) { // 不能选自己 cout << "不能选自己!重新输入:"; cin >> target; target--; } } else { do { target = rand() % playerCount; } while(target == i || players[target].status != ALIVE || (!mustVote.empty() && !isInArray(&mustVote[0], target, mustVote.size()))); } votes[target]++; cout << i + 1 << "号投票给" << target + 1 << "号\n"; Sleep(500); } } int maxVote = -1; vector<int> maxPlayers; for(int i = 0; i < playerCount; i++) { if(votes[i] > maxVote) { maxVote = votes[i]; maxPlayers.clear(); maxPlayers.push_back(i); } else if(votes[i] == maxVote) { maxPlayers.push_back(i); } } if(maxPlayers.size() == 1) { policeId = maxPlayers[0]; cout << "恭喜" << policeId + 1 << "号当选警长!\n"; Sleep(2000); break; } cout << "平票!重投(仅" << maxPlayers.size() << "人可选):\n"; cannotVote = maxPlayers; mustVote = maxPlayers; Sleep(1500); } } int voteOut() { printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(WHITE); cout << "开始投票出局!\n"; Sleep(1000); int votes[100] = {0}; vector<int> cannotVote; vector<int> mustVote; while(true) { memset(votes, 0, sizeof(votes)); for(int i = 0; i < playerCount; i++) { if(players[i].status == ALIVE && !isInArray(&cannotVote[0], i, cannotVote.size())) { int target = -1; if(i == myId) { target = inputValidPlayer("你要投出局的玩家编号:"); } else { do { target = rand() % playerCount; } while(players[target].status != ALIVE || (players[i].role == WEREWOLF && players[target].role == WEREWOLF) || (!mustVote.empty() && !isInArray(&mustVote[0], target, mustVote.size()))); if(i == seerId && seerCheck != -1 && players[seerCheck].role == WEREWOLF) { target = seerCheck; } } votes[target] += (i == policeId) ? 2 : 1; cout << i + 1 << "号投票给" << target + 1 << "号" << (i == policeId ? "(警长2票)" : "") << "\n"; Sleep(300); } } int maxVote = -1; vector<int> maxPlayers; for(int i = 0; i < playerCount; i++) { if(votes[i] > maxVote) { maxVote = votes[i]; maxPlayers.clear(); maxPlayers.push_back(i); } else if(votes[i] == maxVote) { maxPlayers.push_back(i); } } if(maxPlayers.size() == 1) { int outId = maxPlayers[0]; players[outId].status = DEAD; players[outId].killer = GOOD; cout << outId + 1 << "号被投票出局!\n"; Sleep(2000); return outId; } cout << "平票!重投(仅" << maxPlayers.size() << "人可选):\n"; cannotVote = maxPlayers; mustVote = maxPlayers; Sleep(1500); } } int main() { preGame(); bool isFirstDay = true; while(true) { int endFlag = checkGameEnd(); if(endFlag != 0) { system("cls"); setColor(GREEN); switch(endFlag) { case 1: cout << "?? 好人阵营胜利!??\n"; break; case 2: cout << "?? 狼人阵营胜利!??\n"; break; case 3: cout << "?? 本局平局!??\n"; break; } setColor(WHITE); cout << "\n===== 最终身份一览 =====\n"; for(int i = 0; i < playerCount; i++) { cout << i + 1 << "号位:"; switch(players[i].role) { case WITCH: cout << "女巫"; break; case SEER: cout << "预言家"; break; case VILLAGER: cout << "村民"; break; case WEREWOLF: cout << "狼人"; break; case HUNTER: cout << "猎人"; break; case GUARD: cout << "守卫"; break; } cout << " | " << (players[i].status == DEAD ? "死亡" : "存活"); if(players[i].status == DEAD) { cout << " | 死因:"; switch(players[i].killer) { case WEREWOLF: cout << "狼人击杀"; break; case WITCH: cout << "女巫毒死"; break; case GOOD: cout << "投票出局"; break; case HUNTER: cout << "猎人带走"; break; default: cout << "未知"; } } cout << "\n"; } break; } nightPhase(); announceNightResult(); if(isFirstDay && playerCount == 12) { electPolice(); isFirstDay = false; } int outId = voteOut(); if(outId == policeId) { cout << "警长被投票出局,"; int choice = (outId == myId) ? inputValidPlayer("撕毁警徽(1) / 移交警徽(2):") : (rand() % 2 + 1); if(choice == 1) { cout << "警徽已撕毁!\n"; policeId = -1; } else { int newPolice = getRandomPlayer(); cout << "警徽移交给" << newPolice + 1 << "号!\n"; policeId = newPolice; } Sleep(2000); } if(players[outId].role == HUNTER) { hunterSkill(outId, false); } day++; } setColor(WHITE); system("pause"); return 0; } -
#include <iostream> #include <vector> #include <string> #include <cstdlib> #include <ctime> #include <algorithm> // 用于find_if #include <windows.h> // Sleep函数(Windows平台) enum Role { WEREWOLF, // 狼人 VILLAGER, // 村民 POLICE, // 警察/预言家 WITCH, // 女巫 HUNTER // 猎人 }; enum Status { ALIVE, // 存活 DEAD // 死亡 }; struct Player { int id; // 玩家编号 Role role; // 角色 Status status; bool hasBadge; Player(int id_, Role role_) { id = id_; role = role_; status = ALIVE; hasBadge = false; } }; std::vector<Player> players; // 所有玩家 std::vector<int> deadPlayers; // 死亡玩家ID int witchPoison = -1; // 女巫毒药目标(-1未使用) int witchAntidote = -1; // 女巫解药目标(-1未使用) int getRandomPlayer() { std::vector<int> aliveIds; for (size_t i = 0; i < players.size(); ++i) { const Player& p = players[i]; if (p.status == ALIVE) { aliveIds.push_back(p.id); } } if (aliveIds.empty()) { std::cerr << "错误:无存活玩家!" << std::endl; return -1; } srand(time(0)); // 初始化随机数种子 return aliveIds[rand() % aliveIds.size()]; } void initGame(int playerCount) { if (playerCount < 6) { // 狼人杀最低人数限制 std::cerr << "错误:玩家人数需≥6!" << std::endl; return; } players.clear(); deadPlayers.clear(); witchPoison = -1; witchAntidote = -1; players.push_back(Player(1, WEREWOLF)); players.push_back(Player(2, POLICE)); players.push_back(Player(3, WITCH)); players.push_back(Player(4, HUNTER)); for (int i = 5; i <= playerCount; ++i) { players.push_back(Player(i, VILLAGER)); } std::cout << "游戏初始化完成,共" << playerCount << "名玩家!" << std::endl; } void recordDeadPlayer(int playerId) { std::vector<Player>::iterator it; for (it = players.begin(); it != players.end(); ++it) { if (it->id == playerId) { break; } } if (it == players.end()) { std::cerr << "错误:玩家ID" << playerId << "不存在!" << std::endl; return; } if (it->status == DEAD) { std::cout << "提示:玩家" << playerId << "已死亡,无需重复记录!" << std::endl; return; } it->status = DEAD; deadPlayers.push_back(playerId); std::cout << "玩家" << playerId << "已死亡,当前死亡列表:"; for (size_t i = 0; i < deadPlayers.size(); ++i) { std::cout << deadPlayers[i] << " "; } std::cout << std::endl; if (it->role == HUNTER) { std::cout << "猎人" << playerId << "触发技能!随机选择开枪目标:" << std::endl; int target = getRandomPlayer(); if (target != -1 && target != playerId) { std::cout << "猎人开枪带走玩家" << target << "!" << std::endl; recordDeadPlayer(target); } } } void policeCheckAndTransferBadge(int policeId) { std::vector<Player>::iterator policeIt; for (policeIt = players.begin(); policeIt != players.end(); ++policeIt) { if (policeIt->id == policeId && policeIt->role == POLICE) { break; } } if (policeIt == players.end() || policeIt->status == DEAD) { std::cerr << "错误:警察" << policeId << "不存在或已死亡!" << std::endl; return; } int checkTarget = getRandomPlayer(); if (checkTarget == -1) return; std::vector<Player>::iterator targetIt; for (targetIt = players.begin(); targetIt != players.end(); ++targetIt) { if (targetIt->id == checkTarget) { break; } } std::string roleStr; switch (targetIt->role) { case WEREWOLF: roleStr = "狼人"; break; case POLICE: roleStr = "警察"; break; case WITCH: roleStr = "女巫"; break; case HUNTER: roleStr = "猎人"; break; default: roleStr = "村民"; break; } std::cout << "警察" << policeId << "查验玩家" << checkTarget << "身份:" << roleStr << std::endl; if (policeIt->hasBadge) { std::cout << "警察" << policeId << "准备移交警徽,随机选择目标:" << std::endl; int badgeTarget = getRandomPlayer(); if (badgeTarget != -1) { std::vector<Player>::iterator badgeIt; for (badgeIt = players.begin(); badgeIt != players.end(); ++badgeIt) { if (badgeIt->id == badgeTarget) { break; } } badgeIt->hasBadge = true; policeIt->hasBadge = false; std::cout << "警徽已移交至玩家" << badgeTarget << "!" << std::endl; } } } int main() { initGame(8); std::cout << "\n=== 夜间阶段 ===" << std::endl; Sleep(2000); // 延迟2秒 int deadId = getRandomPlayer(); if (deadId != -1) { recordDeadPlayer(deadId); } std::cout << "\n=== 警察阶段 ===" << std::endl; Sleep(2000); for (size_t i = 0; i < players.size(); ++i) { if (players[i].id == 2) { players[i].hasBadge = true; break; } } policeCheckAndTransferBadge(2); std::cout << "\n=== 最终玩家状态 ===" << std::endl; for (size_t i = 0; i < players.size(); ++i) { const Player& p = players[i]; std::string statusStr = (p.status == ALIVE) ? "存活" : "死亡"; std::string badgeStr = (p.hasBadge) ? "(持有警徽)" : ""; std::cout << "玩家" << p.id << ":" << statusStr << badgeStr << std::endl; } return 0; } -
#include <iostream> #include <vector> #include <string> #include <cstdlib> #include <ctime> #include <windows.h> // 用于Sleep函数(仅Windows平台) // 角色枚举(全大写+下划线,符合C++命名规范) enum Role { WEREWOLF, // 狼人 VILLAGER, // 村民 POLICE, // 警察/预言家 WITCH, // 女巫 HUNTER // 猎人 }; // 玩家状态枚举 enum Status { ALIVE, // 存活 DEAD // 死亡 }; // 玩家结构体(封装属性) struct Player { int id; // 玩家编号 Role role; // 角色 Status status; // 状态 bool hasBadge; // 是否持有警徽(仅警察相关) // 构造函数(初始化成员,避免C++11初始化警告兼容问题) Player(int id_, Role role_) : id(id_), role(role_), status(ALIVE), hasBadge(false) {} }; // 全局游戏数据(简化示例,实际可封装为类) std::vector<Player> players; // 所有玩家 std::vector<int> deadPlayers; // 死亡玩家ID列表 int witchPoison = -1; // 女巫毒药使用目标(-1未使用) int witchAntidote = -1; // 女巫解药使用目标(-1未使用) // 工具函数:获取随机存活玩家ID(带边界判断) int getRandomPlayer() { std::vector<int> aliveIds; for (const auto& p : players) { if (p.status == ALIVE) { aliveIds.push_back(p.id); } } if (aliveIds.empty()) { std::cerr << "错误:无存活玩家!" << std::endl; return -1; } srand(time(0)); // 初始化随机数种子 return aliveIds[rand() % aliveIds.size()]; } // 模块1:初始化游戏(创建玩家) void initGame(int playerCount) { if (playerCount < 6) { // 狼人杀最低人数限制 std::cerr << "错误:玩家人数需≥6!" << std::endl; return; } // 初始化角色分配(简化示例:1狼人+1警察+1女巫+1猎人+剩余村民) players.clear(); players.emplace_back(1, WEREWOLF); players.emplace_back(2, POLICE); players.emplace_back(3, WITCH); players.emplace_back(4, HUNTER); for (int i = 5; i <= playerCount; ++i) { players.emplace_back(i, VILLAGER); } std::cout << "游戏初始化完成,共" << playerCount << "名玩家!" << std::endl; } // 模块2:记录死亡玩家(带重复判断) void recordDeadPlayer(int playerId) { // 检查玩家是否存活 auto it = std::find_if(players.begin(), players.end(), [playerId](const Player& p) { return p.id == playerId; }); if (it == players.end()) { std::cerr << "错误:玩家ID" << playerId << "不存在!" << std::endl; return; } if (it->status == DEAD) { std::cout << "提示:玩家" << playerId << "已死亡,无需重复记录!" << std::endl; return; } // 标记死亡并加入死亡列表 it->status = DEAD; deadPlayers.push_back(playerId); std::cout << "玩家" << playerId << "已死亡,当前死亡列表:"; for (int id : deadPlayers) { std::cout << id << " "; } std::cout << std::endl; // 猎人死亡触发技能(简化逻辑) if (it->role == HUNTER) { std::cout << "猎人" << playerId << "触发技能!请选择开枪目标:" << std::endl; int target = getRandomPlayer(); // 示例:随机选择目标 if (target != -1) { std::cout << "猎人开枪带走玩家" << target << "!" << std::endl; recordDeadPlayer(target); // 递归标记目标死亡 } } } // 模块3:警察查验身份+移交警徽逻辑 void policeCheckAndTransferBadge(int policeId) { // 检查警察是否存活 auto policeIt = std::find_if(players.begin(), players.end(), [policeId](const Player& p) { return p.id == policeId && p.role == POLICE; }); if (policeIt == players.end() || policeIt->status == DEAD) { std::cerr << "错误:警察" << policeId << "不存在或已死亡!" << std::endl; return; } // 警察查验随机玩家身份 int checkTarget = getRandomPlayer(); if (checkTarget == -1) return; auto targetIt = std::find_if(players.begin(), players.end(), [checkTarget](const Player& p) { return p.id == checkTarget; }); std::string roleStr; switch (targetIt->role) { case WEREWOLF: roleStr = "狼人"; break; case POLICE: roleStr = "警察"; break; case WITCH: roleStr = "女巫"; break; case HUNTER: roleStr = "猎人"; break; default: roleStr = "村民"; break; } std::cout << "警察" << policeId << "查验玩家" << checkTarget << "身份:" << roleStr << std::endl; // 警察移交警徽(仅存活且持有警徽时) if (policeIt->hasBadge) { std::cout << "警察" << policeId << "准备移交警徽,请选择目标:" << std::endl; int badgeTarget = getRandomPlayer(); if (badgeTarget != -1) { auto badgeIt = std::find_if(players.begin(), players.end(), [badgeTarget](const Player& p) { return p.id == badgeTarget; }); badgeIt->hasBadge = true; policeIt->hasBadge = false; std::cout << "警徽已移交至玩家" << badgeTarget << "!" << std::endl; } } } // 主函数:演示核心流程 int main() { // 1. 初始化游戏(8名玩家) initGame(8); // 2. 延迟模拟游戏流程(原Sleep逻辑保留) std::cout << "\n游戏开始,夜间阶段...(延迟2秒)" << std::endl; Sleep(2000); // 3. 狼人刀人(示例:随机死亡1名玩家) int deadId = getRandomPlayer(); if (deadId != -1) { recordDeadPlayer(deadId); } // 4. 警察查验+移交警徽(示例:警察2操作) std::cout << "\n警察阶段...(延迟2秒)" << std::endl; Sleep(2000); players[1].hasBadge = true; // 给警察2分配初始警徽 policeCheckAndTransferBadge(2); // 5. 输出最终状态 std::cout << "\n当前所有玩家状态:" << std::endl; for (const auto& p : players) { std::string statusStr = (p.status == ALIVE) ? "存活" : "死亡"; std::string badgeStr = (p.hasBadge) ? "(持有警徽)" : ""; std::cout << "玩家" << p.id << ":" << statusStr << badgeStr << std::endl; } return 0; } -
#include<bits/stdc++.h> #include<windows.h> #include<bits/stdc++.h> using namespace std; enum TimeState { DAY = 0, NIGHT = 1 }; enum Color { BLUE = 0, YELLOW = 1, RED = 2, GREEN = 3, PURPLE = 4, WHITE = 5 }; enum Role { WITCH = 0, // 女巫 VILLAGER = 1, // 村民 SEER = 2, // 预言家 WEREWOLF = 3, // 狼人 HUNTER = 4, // 猎人 GUARD = 5, // 守卫 GOOD = 6 // 好人(投票) }; enum Status { DEAD = 1, ALIVE = 2 }; int day = 0; // 当前天数 TimeState currentTime = DAY;// 当前昼夜状态 int playerCount = 0; // 玩家总数 int myId = -1; // 自己的编号(0开始) HWND hwnd = GetForegroundWindow(); int policeId = -1; // 警长编号 int witchCnt = 0, villagerCnt = 0, seerCnt = 0, wolfCnt = 0, hunterCnt = 0, guardCnt = 0; int witchId = -1, guardId = -1, seerId = -1, hunterId = -1; int wolfIds[4] = {-1, -1, -1, -1}; // 狼人编号 int wolfKill = -1; // 狼人击杀目标 int guardProtect = -1;// 守卫守护目标 int witchSave = -1; // 女巫解药目标 int witchPoison = -1; // 女巫毒药目标 bool witchHasSave = false; // 女巫是否用过解药 bool witchHasPoison = false;// 女巫是否用过毒药 int seerCheck = -1; // 预言家查验目标 struct Player { Role role = (Role)-1; // 角色类型 Status status = ALIVE; // 生存状态 bool isKnown = false; // 是否已知身份(自己视角) int killer = INT_MAX; // 击杀者(角色类型) }; Player players[1000]; void setColor(Color c) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); switch(c) { case RED: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED); break; case GREEN: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_GREEN); break; case YELLOW: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); break; case BLUE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_BLUE); break; case WHITE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; case PURPLE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); break; } } void gotoXY(int x, int y) { COORD pos = { (SHORT)x, (SHORT)y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } bool isInArray(int arr[], int val, int len) { for(int i = 0; i < len; i++) { if(arr[i] == val) return true; } return false; } int getRandomPlayer(Role excludeRole = (Role)-1) { int p = -1; while(true) { p = rand() % playerCount; if(players[p].status == ALIVE && (excludeRole == (Role)-1 || players[p].role != excludeRole)) { break; } } return p; } int inputValidPlayer(const string& prompt) { int p; cout << prompt; while(true) { cin >> p; p--; // 转为0开始 if(p >= 0 && p < playerCount && players[p].status == ALIVE) { break; } cout << "输入无效!请输入存活玩家编号(1~" << playerCount << "):"; } return p; } void initWindow() { system("mode con lines=60 cols=188"); ShowWindow(hwnd, SW_MAXIMIZE); DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(GetConsoleWindow()); } void initPlayers() { myId = rand() % playerCount; if(playerCount == 12) wolfCnt = 4; else if(playerCount >= 10) wolfCnt = 3; else wolfCnt = 2; for(int i = 0; i < playerCount; i++) { players[i].status = ALIVE; players[i].isKnown = false; players[i].killer = INT_MAX; } int wolfIdx = 0; while(wolfIdx < wolfCnt) { int p = rand() % playerCount; if(!isInArray(wolfIds, p, 4)) { wolfIds[wolfIdx++] = p; players[p].role = WEREWOLF; } } if(playerCount == 12) { do { guardId = rand() % playerCount; } while(players[guardId].role != (Role)-1); players[guardId].role = GUARD; guardCnt = 1; } do { witchId = rand() % playerCount; } while(players[witchId].role != (Role)-1); players[witchId].role = WITCH; witchCnt = 1; if(playerCount >= 10) { do { hunterId = rand() % playerCount; } while(players[hunterId].role != (Role)-1); players[hunterId].role = HUNTER; hunterCnt = 1; } do { seerId = rand() % playerCount; } while(players[seerId].role != (Role)-1); players[seerId].role = SEER; seerCnt = 1; for(int i = 0; i < playerCount; i++) { if(players[i].role == (Role)-1) { players[i].role = VILLAGER; villagerCnt++; } } players[myId].isKnown = true; if(players[myId].role == WEREWOLF) { for(int i = 0; i < wolfCnt; i++) { players[wolfIds[i]].isKnown = true; } } } void printGameUI() { system("cls"); gotoXY(0, 0); cout << "作者:chenzhan"; gotoXY(90, 0); currentTime == NIGHT ? setColor(RED) : setColor(BLUE); printf("第%d天 | %s\n", day, currentTime == NIGHT ? "黑夜" : "白天"); gotoXY(0, 3); setColor(BLUE); cout << " 我的号位:" << myId + 1; for(int i = 0; i < playerCount; i++) { gotoXY(i * 8 + 1, 4); i == policeId ? setColor(YELLOW) : setColor(BLUE); cout << i + 1 << "号位"; gotoXY(i * 8 + 1, 5); players[i].status == DEAD ? setColor(RED) : setColor(GREEN); cout << (players[i].status == DEAD ? "死 亡" : "存 活"); gotoXY(i * 8 + 1, 6); setColor(BLUE); if(players[i].isKnown) { switch(players[i].role) { case WITCH: cout << "女 巫"; break; case SEER: cout << "预言家"; break; case VILLAGER: cout << "村 民"; break; case WEREWOLF: cout << "狼 人"; break; case HUNTER: cout << "猎 人"; break; case GUARD: cout << "守 卫"; break; default: cout << "好人"; } } else { cout << "未知"; } } setColor(WHITE); // 重置颜色 } int checkGameEnd() { int deadWolf = 0, deadVillager = 0, deadGod = 0; for(int i = 0; i < playerCount; i++) { if(players[i].status == DEAD) { if(players[i].role == WEREWOLF) deadWolf++; else if(players[i].role == VILLAGER) deadVillager++; else deadGod++; // 女巫/预言家/猎人/守卫视为神 } } if(deadWolf >= wolfCnt) return 1; int godTotal = witchCnt + seerCnt + hunterCnt + guardCnt; if(deadGod >= godTotal || deadVillager >= villagerCnt) return 2; if((deadWolf == deadVillager || deadWolf == deadGod) && deadWolf >= wolfCnt) return 3; return 0; } void preGame() { srand(time(NULL)); initWindow(); setColor(GREEN); cout << "欢迎来到狼人杀游戏\t\t\t注:10/11/12人局开设猎人 12人局开设守卫/警长\n"; cout << "====================\n请输入玩家人数(8-12人):"; cin >> playerCount; while(playerCount < 8 || playerCount > 12) { cout << "人数无效!请重新输入(8-12):"; cin >> playerCount; } system("cls"); cout << "初始化身份中,请稍等."; for(int i = 0; i < 3; i++) { for(int j = 0; j < 10; j++) { cout << "."; Sleep(50); } cout << "\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b"; } initPlayers(); system("cls"); cout << "你的身份信息:\n"; cout << "我的号位:" << myId + 1 << "\n我的身份:"; switch(players[myId].role) { case WITCH: cout << "女巫\n"; break; case SEER: cout << "预言家\n"; break; case VILLAGER: cout << "村民\n"; break; case WEREWOLF: cout << "狼人\n"; break; case HUNTER: cout << "猎人\n"; break; case GUARD: cout << "守卫\n"; break; } day = 1; // 第一天 system("pause"); printGameUI(); } void nightPhase() { currentTime = NIGHT; printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(WHITE); cout << "天黑~请闭眼~~~\n"; Sleep(1500); guardProtect = -1; if(playerCount == 12 && players[guardId].status == ALIVE) { setColor(BLUE); cout << "守卫~请睁眼~~\n"; Sleep(1500); if(players[myId].role == GUARD && players[myId].status == ALIVE) { guardProtect = inputValidPlayer("你要守护的玩家编号:"); cout << "你选择守护" << guardProtect + 1 << "号\n"; } else { guardProtect = getRandomPlayer(); // AI随机守护 } Sleep(1500); cout << "守卫请闭眼~~\n\n"; Sleep(1000); } wolfKill = -1; setColor(RED); cout << "狼人~请睁眼~~~\n"; Sleep(1500); cout << "选择猎杀目标:\n"; if(players[myId].role == WEREWOLF && players[myId].status == ALIVE) { wolfKill = inputValidPlayer("你要击杀的玩家编号:"); cout << "狼人选择猎杀" << wolfKill + 1 << "号\n"; } else { wolfKill = getRandomPlayer(WEREWOLF); // AI随机击杀(排除狼人) } Sleep(2000); cout << "狼人请闭眼~~\n\n"; Sleep(1000); witchSave = -1; witchPoison = -1; setColor(PURPLE); cout << "女巫~请睁眼~~\n"; Sleep(2000); if(players[myId].role == WITCH && players[myId].status == ALIVE) { if(!witchHasSave) { cout << "今晚" << wolfKill + 1 << "号被击杀,是否使用解药?(1=是/2=否):"; int choice; cin >> choice; if(choice == 1) { witchSave = wolfKill; witchHasSave = true; cout << "已使用解药解救" << wolfKill + 1 << "号\n"; } } else { cout << "你已使用解药\n"; } Sleep(1500); if(!witchHasPoison) { cout << "是否使用毒药?(1=是/2=否):"; int choice; cin >> choice; if(choice == 1) { witchPoison = inputValidPlayer("你要毒杀的玩家编号:"); witchHasPoison = true; cout << "已使用毒药毒杀" << witchPoison + 1 << "号\n"; } } else { cout << "你已使用毒药\n"; } } else { if(!witchHasSave && rand() % 3 == 0) { // 30%概率救 witchSave = wolfKill; witchHasSave = true; } if(!witchHasPoison && rand() % 5 == 0) { // 20%概率毒 witchPoison = getRandomPlayer(); witchHasPoison = true; } } Sleep(1500); cout << "女巫请闭眼~~\n\n"; Sleep(1000); seerCheck = -1; setColor(GREEN); cout << "预言家~请睁眼~~\n"; Sleep(2000); if(players[myId].role == SEER && players[myId].status == ALIVE) { seerCheck = inputValidPlayer("你要查验的玩家编号:"); cout << seerCheck + 1 << "号的身份是——"; Sleep(1000); if(players[seerCheck].role == WEREWOLF) { cout << "狼人\n"; } else { cout << "好人\n"; players[seerCheck].isKnown = true; // 标记为好人 } players[seerCheck].isKnown = true; } else { seerCheck = getRandomPlayer(); // AI随机查验 } Sleep(1500); cout << "预言家请闭眼~~\n"; Sleep(1000); currentTime = DAY; } void hunterSkill(int hunterId, bool isPoisoned) { if(isPoisoned) { cout << hunterId + 1 << "号猎人被毒死,无法开枪!\n"; return; } cout << hunterId + 1 << "号猎人发动技能!\n"; int target = -1; if(hunterId == myId) { target = inputValidPlayer("你要带走的玩家编号:"); } else { target = getRandomPlayer(); // AI随机带走 } players[target].status = DEAD; players[target].killer = HUNTER; cout << "猎人带走了" << target + 1 << "号!\n"; Sleep(2000); } void announceNightResult() { printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(WHITE); cout << "天亮了!\n"; Sleep(2000); vector<int> deadPlayers; if(wolfKill != -1 && wolfKill != guardProtect && wolfKill != witchSave) { players[wolfKill].status = DEAD; players[wolfKill].killer = WEREWOLF; deadPlayers.push_back(wolfKill); } if(witchPoison != -1) { players[witchPoison].status = DEAD; players[witchPoison].killer = WITCH; deadPlayers.push_back(witchPoison); } if(deadPlayers.empty()) { cout << "昨晚是平安夜!\n"; } else { cout << "昨晚死亡的玩家:"; for(int i = 0; i < deadPlayers.size(); i++) { if(i > 0) cout << "、"; cout << deadPlayers[i] + 1 << "号"; if(deadPlayers[i] == policeId) { cout << "(警长)"; } } cout << "\n"; } Sleep(2000); for(int p : deadPlayers) { if(players[p].role == HUNTER) { bool isPoisoned = (p == witchPoison); hunterSkill(p, isPoisoned); } } for(int p : deadPlayers) { if(p == policeId) { cout << "警长死亡,"; int choice = (p == myId) ? inputValidPlayer("撕毁警徽(1) / 移交警徽(2):") : (rand() % 2 + 1); if(choice == 1) { cout << "警徽已撕毁!\n"; policeId = -1; } else { int newPolice = getRandomPlayer(); cout << "警徽移交给" << newPolice + 1 << "号!\n"; policeId = newPolice; } Sleep(2000); } } } void electPolice() { printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(YELLOW); cout << "开始选举警长!(不能选自己)\n"; Sleep(1500); int votes[100] = {0}; vector<int> cannotVote; // 平票者(重投时排除) vector<int> mustVote; // 重投时仅能投这些人 while(true) { memset(votes, 0, sizeof(votes)); for(int i = 0; i < playerCount; i++) { if(players[i].status == ALIVE && !isInArray(&cannotVote[0], i, cannotVote.size())) { int target = -1; if(i == myId) { target = inputValidPlayer("你要选举的警长编号:"); while(target == myId) { // 不能选自己 cout << "不能选自己!重新输入:"; cin >> target; target--; } } else { do { target = rand() % playerCount; } while(target == i || players[target].status != ALIVE || (!mustVote.empty() && !isInArray(&mustVote[0], target, mustVote.size()))); } votes[target]++; cout << i + 1 << "号投票给" << target + 1 << "号\n"; Sleep(500); } } int maxVote = -1; vector<int> maxPlayers; for(int i = 0; i < playerCount; i++) { if(votes[i] > maxVote) { maxVote = votes[i]; maxPlayers.clear(); maxPlayers.push_back(i); } else if(votes[i] == maxVote) { maxPlayers.push_back(i); } } if(maxPlayers.size() == 1) { policeId = maxPlayers[0]; cout << "恭喜" << policeId + 1 << "号当选警长!\n"; Sleep(2000); break; } cout << "平票!重投(仅" << maxPlayers.size() << "人可选):\n"; cannotVote = maxPlayers; mustVote = maxPlayers; Sleep(1500); } } int voteOut() { printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(WHITE); cout << "开始投票出局!\n"; Sleep(1000); int votes[100] = {0}; vector<int> cannotVote; vector<int> mustVote; while(true) { memset(votes, 0, sizeof(votes)); for(int i = 0; i < playerCount; i++) { if(players[i].status == ALIVE && !isInArray(&cannotVote[0], i, cannotVote.size())) { int target = -1; if(i == myId) { target = inputValidPlayer("你要投出局的玩家编号:"); } else { do { target = rand() % playerCount; } while(players[target].status != ALIVE || (players[i].role == WEREWOLF && players[target].role == WEREWOLF) || (!mustVote.empty() && !isInArray(&mustVote[0], target, mustVote.size()))); if(i == seerId && seerCheck != -1 && players[seerCheck].role == WEREWOLF) { target = seerCheck; } } votes[target] += (i == policeId) ? 2 : 1; cout << i + 1 << "号投票给" << target + 1 << "号" << (i == policeId ? "(警长2票)" : "") << "\n"; Sleep(300); } } int maxVote = -1; vector<int> maxPlayers; for(int i = 0; i < playerCount; i++) { if(votes[i] > maxVote) { maxVote = votes[i]; maxPlayers.clear(); maxPlayers.push_back(i); } else if(votes[i] == maxVote) { maxPlayers.push_back(i); } } if(maxPlayers.size() == 1) { int outId = maxPlayers[0]; players[outId].status = DEAD; players[outId].killer = GOOD; cout << outId + 1 << "号被投票出局!\n"; Sleep(2000); return outId; } cout << "平票!重投(仅" << maxPlayers.size() << "人可选):\n"; cannotVote = maxPlayers; mustVote = maxPlayers; Sleep(1500); } } int main() { preGame(); bool isFirstDay = true; while(true) { int endFlag = checkGameEnd(); if(endFlag != 0) { system("cls"); setColor(GREEN); switch(endFlag) { case 1: cout << "?? 好人阵营胜利!??\n"; break; case 2: cout << "?? 狼人阵营胜利!??\n"; break; case 3: cout << "?? 本局平局!??\n"; break; } setColor(WHITE); cout << "\n===== 最终身份一览 =====\n"; for(int i = 0; i < playerCount; i++) { cout << i + 1 << "号位:"; switch(players[i].role) { case WITCH: cout << "女巫"; break; case SEER: cout << "预言家"; break; case VILLAGER: cout << "村民"; break; case WEREWOLF: cout << "狼人"; break; case HUNTER: cout << "猎人"; break; case GUARD: cout << "守卫"; break; } cout << " | " << (players[i].status == DEAD ? "死亡" : "存活"); if(players[i].status == DEAD) { cout << " | 死因:"; switch(players[i].killer) { case WEREWOLF: cout << "狼人击杀"; break; case WITCH: cout << "女巫毒死"; break; case GOOD: cout << "投票出局"; break; case HUNTER: cout << "猎人带走"; break; default: cout << "未知"; } } cout << "\n"; } break; } nightPhase(); announceNightResult(); if(isFirstDay && playerCount == 12) { electPolice(); isFirstDay = false; } int outId = voteOut(); if(outId == policeId) { cout << "警长被投票出局,"; int choice = (outId == myId) ? inputValidPlayer("撕毁警徽(1) / 移交警徽(2):") : (rand() % 2 + 1); if(choice == 1) { cout << "警徽已撕毁!\n"; policeId = -1; } else { int newPolice = getRandomPlayer(); cout << "警徽移交给" << newPolice + 1 << "号!\n"; policeId = newPolice; } Sleep(2000); } if(players[outId].role == HUNTER) { hunterSkill(outId, false); } day++; } setColor(WHITE); system("pause"); return 0; } -
打开项目 → 工具 → 编译选项 → 切换到 “连接器” 标签页 → 在 “连接器命令行” 输入框中:
-static-libgcc -lwinmm -std=c++11#include<bits/stdc++.h> #include<windows.h> #include<bits/stdc++.h> using namespace std; enum TimeState { DAY = 0, NIGHT = 1 }; enum Color { BLUE = 0, YELLOW = 1, RED = 2, GREEN = 3, PURPLE = 4, WHITE = 5 }; enum Role { WITCH = 0, // 女巫 VILLAGER = 1, // 村民 SEER = 2, // 预言家 WEREWOLF = 3, // 狼人 HUNTER = 4, // 猎人 GUARD = 5, // 守卫 GOOD = 6 // 好人(投票) }; enum Status { DEAD = 1, ALIVE = 2 }; int day = 0; // 当前天数 TimeState currentTime = DAY;// 当前昼夜状态 int playerCount = 0; // 玩家总数 int myId = -1; // 自己的编号(0开始) HWND hwnd = GetForegroundWindow(); int policeId = -1; // 警长编号 int witchCnt = 0, villagerCnt = 0, seerCnt = 0, wolfCnt = 0, hunterCnt = 0, guardCnt = 0; int witchId = -1, guardId = -1, seerId = -1, hunterId = -1; int wolfIds[4] = {-1, -1, -1, -1}; // 狼人编号 int wolfKill = -1; // 狼人击杀目标 int guardProtect = -1;// 守卫守护目标 int witchSave = -1; // 女巫解药目标 int witchPoison = -1; // 女巫毒药目标 bool witchHasSave = false; // 女巫是否用过解药 bool witchHasPoison = false;// 女巫是否用过毒药 int seerCheck = -1; // 预言家查验目标 struct Player { Role role = (Role)-1; // 角色类型 Status status = ALIVE; // 生存状态 bool isKnown = false; // 是否已知身份(自己视角) int killer = INT_MAX; // 击杀者(角色类型) }; Player players[1000]; void setColor(Color c) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); switch(c) { case RED: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED); break; case GREEN: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_GREEN); break; case YELLOW: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); break; case BLUE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_BLUE); break; case WHITE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; case PURPLE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); break; } } void gotoXY(int x, int y) { COORD pos = { (SHORT)x, (SHORT)y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } bool isInArray(int arr[], int val, int len) { for(int i = 0; i < len; i++) { if(arr[i] == val) return true; } return false; } int getRandomPlayer(Role excludeRole = (Role)-1) { int p = -1; while(true) { p = rand() % playerCount; if(players[p].status == ALIVE && (excludeRole == (Role)-1 || players[p].role != excludeRole)) { break; } } return p; } int inputValidPlayer(const string& prompt) { int p; cout << prompt; while(true) { cin >> p; p--; // 转为0开始 if(p >= 0 && p < playerCount && players[p].status == ALIVE) { break; } cout << "输入无效!请输入存活玩家编号(1~" << playerCount << "):"; } return p; } void initWindow() { system("mode con lines=60 cols=188"); ShowWindow(hwnd, SW_MAXIMIZE); DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(GetConsoleWindow()); } void initPlayers() { myId = rand() % playerCount; if(playerCount == 12) wolfCnt = 4; else if(playerCount >= 10) wolfCnt = 3; else wolfCnt = 2; for(int i = 0; i < playerCount; i++) { players[i].status = ALIVE; players[i].isKnown = false; players[i].killer = INT_MAX; } int wolfIdx = 0; while(wolfIdx < wolfCnt) { int p = rand() % playerCount; if(!isInArray(wolfIds, p, 4)) { wolfIds[wolfIdx++] = p; players[p].role = WEREWOLF; } } if(playerCount == 12) { do { guardId = rand() % playerCount; } while(players[guardId].role != (Role)-1); players[guardId].role = GUARD; guardCnt = 1; } do { witchId = rand() % playerCount; } while(players[witchId].role != (Role)-1); players[witchId].role = WITCH; witchCnt = 1; if(playerCount >= 10) { do { hunterId = rand() % playerCount; } while(players[hunterId].role != (Role)-1); players[hunterId].role = HUNTER; hunterCnt = 1; } do { seerId = rand() % playerCount; } while(players[seerId].role != (Role)-1); players[seerId].role = SEER; seerCnt = 1; for(int i = 0; i < playerCount; i++) { if(players[i].role == (Role)-1) { players[i].role = VILLAGER; villagerCnt++; } } players[myId].isKnown = true; if(players[myId].role == WEREWOLF) { for(int i = 0; i < wolfCnt; i++) { players[wolfIds[i]].isKnown = true; } } } void printGameUI() { system("cls"); gotoXY(0, 0); cout << "作者:chenzhan"; gotoXY(90, 0); currentTime == NIGHT ? setColor(RED) : setColor(BLUE); printf("第%d天 | %s\n", day, currentTime == NIGHT ? "黑夜" : "白天"); gotoXY(0, 3); setColor(BLUE); cout << " 我的号位:" << myId + 1; for(int i = 0; i < playerCount; i++) { gotoXY(i * 8 + 1, 4); i == policeId ? setColor(YELLOW) : setColor(BLUE); cout << i + 1 << "号位"; gotoXY(i * 8 + 1, 5); players[i].status == DEAD ? setColor(RED) : setColor(GREEN); cout << (players[i].status == DEAD ? "死 亡" : "存 活"); gotoXY(i * 8 + 1, 6); setColor(BLUE); if(players[i].isKnown) { switch(players[i].role) { case WITCH: cout << "女 巫"; break; case SEER: cout << "预言家"; break; case VILLAGER: cout << "村 民"; break; case WEREWOLF: cout << "狼 人"; break; case HUNTER: cout << "猎 人"; break; case GUARD: cout << "守 卫"; break; default: cout << "好人"; } } else { cout << "未知"; } } setColor(WHITE); // 重置颜色 } int checkGameEnd() { int deadWolf = 0, deadVillager = 0, deadGod = 0; for(int i = 0; i < playerCount; i++) { if(players[i].status == DEAD) { if(players[i].role == WEREWOLF) deadWolf++; else if(players[i].role == VILLAGER) deadVillager++; else deadGod++; // 女巫/预言家/猎人/守卫视为神 } } if(deadWolf >= wolfCnt) return 1; int godTotal = witchCnt + seerCnt + hunterCnt + guardCnt; if(deadGod >= godTotal || deadVillager >= villagerCnt) return 2; if((deadWolf == deadVillager || deadWolf == deadGod) && deadWolf >= wolfCnt) return 3; return 0; } void preGame() { srand(time(NULL)); initWindow(); setColor(GREEN); cout << "欢迎来到狼人杀游戏\t\t\t注:10/11/12人局开设猎人 12人局开设守卫/警长\n"; cout << "====================\n请输入玩家人数(8-12人):"; cin >> playerCount; while(playerCount < 8 || playerCount > 12) { cout << "人数无效!请重新输入(8-12):"; cin >> playerCount; } system("cls"); cout << "初始化身份中,请稍等."; for(int i = 0; i < 3; i++) { for(int j = 0; j < 10; j++) { cout << "."; Sleep(50); } cout << "\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b"; } initPlayers(); system("cls"); cout << "你的身份信息:\n"; cout << "我的号位:" << myId + 1 << "\n我的身份:"; switch(players[myId].role) { case WITCH: cout << "女巫\n"; break; case SEER: cout << "预言家\n"; break; case VILLAGER: cout << "村民\n"; break; case WEREWOLF: cout << "狼人\n"; break; case HUNTER: cout << "猎人\n"; break; case GUARD: cout << "守卫\n"; break; } day = 1; // 第一天 system("pause"); printGameUI(); } void nightPhase() { currentTime = NIGHT; printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(WHITE); cout << "天黑~请闭眼~~~\n"; Sleep(1500); guardProtect = -1; if(playerCount == 12 && players[guardId].status == ALIVE) { setColor(BLUE); cout << "守卫~请睁眼~~\n"; Sleep(1500); if(players[myId].role == GUARD && players[myId].status == ALIVE) { guardProtect = inputValidPlayer("你要守护的玩家编号:"); cout << "你选择守护" << guardProtect + 1 << "号\n"; } else { guardProtect = getRandomPlayer(); // AI随机守护 } Sleep(1500); cout << "守卫请闭眼~~\n\n"; Sleep(1000); } wolfKill = -1; setColor(RED); cout << "狼人~请睁眼~~~\n"; Sleep(1500); cout << "选择击杀目标:\n"; if(players[myId].role == WEREWOLF && players[myId].status == ALIVE) { wolfKill = inputValidPlayer("你要击杀的玩家编号:"); cout << "狼人选择击杀" << wolfKill + 1 << "号\n"; } else { wolfKill = getRandomPlayer(WEREWOLF); // AI随机击杀(排除狼人) } Sleep(2000); cout << "狼人请闭眼~~\n\n"; Sleep(1000); witchSave = -1; witchPoison = -1; setColor(PURPLE); cout << "女巫~请睁眼~~\n"; Sleep(2000); if(players[myId].role == WITCH && players[myId].status == ALIVE) { if(!witchHasSave) { cout << "今晚" << wolfKill + 1 << "号被击杀,是否使用解药?(1=是/2=否):"; int choice; cin >> choice; if(choice == 1) { witchSave = wolfKill; witchHasSave = true; cout << "已使用解药解救" << wolfKill + 1 << "号\n"; } } else { cout << "你已使用解药\n"; } Sleep(1500); if(!witchHasPoison) { cout << "是否使用毒药?(1=是/2=否):"; int choice; cin >> choice; if(choice == 1) { witchPoison = inputValidPlayer("你要毒杀的玩家编号:"); witchHasPoison = true; cout << "已使用毒药毒杀" << witchPoison + 1 << "号\n"; } } else { cout << "你已使用毒药\n"; } } else { if(!witchHasSave && rand() % 3 == 0) { // 30%概率救 witchSave = wolfKill; witchHasSave = true; } if(!witchHasPoison && rand() % 5 == 0) { // 20%概率毒 witchPoison = getRandomPlayer(); witchHasPoison = true; } } Sleep(1500); cout << "女巫请闭眼~~\n\n"; Sleep(1000); seerCheck = -1; setColor(GREEN); cout << "预言家~请睁眼~~\n"; Sleep(2000); if(players[myId].role == SEER && players[myId].status == ALIVE) { seerCheck = inputValidPlayer("你要查验的玩家编号:"); cout << seerCheck + 1 << "号的身份是——"; Sleep(1000); if(players[seerCheck].role == WEREWOLF) { cout << "狼人\n"; } else { cout << "好人\n"; players[seerCheck].isKnown = true; // 标记为好人 } players[seerCheck].isKnown = true; } else { seerCheck = getRandomPlayer(); // AI随机查验 } Sleep(1500); cout << "预言家请闭眼~~\n"; Sleep(1000); currentTime = DAY; } void hunterSkill(int hunterId, bool isPoisoned) { if(isPoisoned) { cout << hunterId + 1 << "号猎人被毒死,无法开枪!\n"; return; } cout << hunterId + 1 << "号猎人发动技能!\n"; int target = -1; if(hunterId == myId) { target = inputValidPlayer("你要带走的玩家编号:"); } else { target = getRandomPlayer(); // AI随机带走 } players[target].status = DEAD; players[target].killer = HUNTER; cout << "猎人带走了" << target + 1 << "号!\n"; Sleep(2000); } void announceNightResult() { printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(WHITE); cout << "天亮了!\n"; Sleep(2000); vector<int> deadPlayers; if(wolfKill != -1 && wolfKill != guardProtect && wolfKill != witchSave) { players[wolfKill].status = DEAD; players[wolfKill].killer = WEREWOLF; deadPlayers.push_back(wolfKill); } if(witchPoison != -1) { players[witchPoison].status = DEAD; players[witchPoison].killer = WITCH; deadPlayers.push_back(witchPoison); } if(deadPlayers.empty()) { cout << "昨晚是平安夜!\n"; } else { cout << "昨晚死亡的玩家:"; for(int i = 0; i < deadPlayers.size(); i++) { if(i > 0) cout << "、"; cout << deadPlayers[i] + 1 << "号"; if(deadPlayers[i] == policeId) { cout << "(警长)"; } } cout << "\n"; } Sleep(2000); for(int p : deadPlayers) { if(players[p].role == HUNTER) { bool isPoisoned = (p == witchPoison); hunterSkill(p, isPoisoned); } } for(int p : deadPlayers) { if(p == policeId) { cout << "警长死亡,"; int choice = (p == myId) ? inputValidPlayer("撕毁警徽(1) / 移交警徽(2):") : (rand() % 2 + 1); if(choice == 1) { cout << "警徽已撕毁!\n"; policeId = -1; } else { int newPolice = getRandomPlayer(); cout << "警徽移交给" << newPolice + 1 << "号!\n"; policeId = newPolice; } Sleep(2000); } } } void electPolice() { printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(YELLOW); cout << "开始选举警长!(不能选自己)\n"; Sleep(1500); int votes[100] = {0}; vector<int> cannotVote; // 平票者(重投时排除) vector<int> mustVote; // 重投时仅能投这些人 while(true) { memset(votes, 0, sizeof(votes)); for(int i = 0; i < playerCount; i++) { if(players[i].status == ALIVE && !isInArray(&cannotVote[0], i, cannotVote.size())) { int target = -1; if(i == myId) { target = inputValidPlayer("你要选举的警长编号:"); while(target == myId) { // 不能选自己 cout << "不能选自己!重新输入:"; cin >> target; target--; } } else { do { target = rand() % playerCount; } while(target == i || players[target].status != ALIVE || (!mustVote.empty() && !isInArray(&mustVote[0], target, mustVote.size()))); } votes[target]++; cout << i + 1 << "号投票给" << target + 1 << "号\n"; Sleep(500); } } int maxVote = -1; vector<int> maxPlayers; for(int i = 0; i < playerCount; i++) { if(votes[i] > maxVote) { maxVote = votes[i]; maxPlayers.clear(); maxPlayers.push_back(i); } else if(votes[i] == maxVote) { maxPlayers.push_back(i); } } if(maxPlayers.size() == 1) { policeId = maxPlayers[0]; cout << "恭喜" << policeId + 1 << "号当选警长!\n"; Sleep(2000); break; } cout << "平票!重投(仅" << maxPlayers.size() << "人可选):\n"; cannotVote = maxPlayers; mustVote = maxPlayers; Sleep(1500); } } int voteOut() { printGameUI(); gotoXY(0, 7); cout << "====================\n"; setColor(WHITE); cout << "开始投票出局!\n"; Sleep(1000); int votes[100] = {0}; vector<int> cannotVote; vector<int> mustVote; while(true) { memset(votes, 0, sizeof(votes)); for(int i = 0; i < playerCount; i++) { if(players[i].status == ALIVE && !isInArray(&cannotVote[0], i, cannotVote.size())) { int target = -1; if(i == myId) { target = inputValidPlayer("你要投出局的玩家编号:"); } else { do { target = rand() % playerCount; } while(players[target].status != ALIVE || (players[i].role == WEREWOLF && players[target].role == WEREWOLF) || (!mustVote.empty() && !isInArray(&mustVote[0], target, mustVote.size()))); if(i == seerId && seerCheck != -1 && players[seerCheck].role == WEREWOLF) { target = seerCheck; } } votes[target] += (i == policeId) ? 2 : 1; cout << i + 1 << "号投票给" << target + 1 << "号" << (i == policeId ? "(警长2票)" : "") << "\n"; Sleep(300); } } int maxVote = -1; vector<int> maxPlayers; for(int i = 0; i < playerCount; i++) { if(votes[i] > maxVote) { maxVote = votes[i]; maxPlayers.clear(); maxPlayers.push_back(i); } else if(votes[i] == maxVote) { maxPlayers.push_back(i); } } if(maxPlayers.size() == 1) { int outId = maxPlayers[0]; players[outId].status = DEAD; players[outId].killer = GOOD; cout << outId + 1 << "号被投票出局!\n"; Sleep(2000); return outId; } cout << "平票!重投(仅" << maxPlayers.size() << "人可选):\n"; cannotVote = maxPlayers; mustVote = maxPlayers; Sleep(1500); } } int main() { preGame(); bool isFirstDay = true; while(true) { int endFlag = checkGameEnd(); if(endFlag != 0) { system("cls"); setColor(GREEN); switch(endFlag) { case 1: cout << "?? 好人阵营胜利!??\n"; break; case 2: cout << "?? 狼人阵营胜利!??\n"; break; case 3: cout << "?? 本局平局!??\n"; break; } setColor(WHITE); cout << "\n===== 最终身份一览 =====\n"; for(int i = 0; i < playerCount; i++) { cout << i + 1 << "号位:"; switch(players[i].role) { case WITCH: cout << "女巫"; break; case SEER: cout << "预言家"; break; case VILLAGER: cout << "村民"; break; case WEREWOLF: cout << "狼人"; break; case HUNTER: cout << "猎人"; break; case GUARD: cout << "守卫"; break; } cout << " | " << (players[i].status == DEAD ? "死亡" : "存活"); if(players[i].status == DEAD) { cout << " | 死因:"; switch(players[i].killer) { case WEREWOLF: cout << "狼人击杀"; break; case WITCH: cout << "女巫毒死"; break; case GOOD: cout << "投票出局"; break; case HUNTER: cout << "猎人带走"; break; default: cout << "未知"; } } cout << "\n"; } break; } nightPhase(); announceNightResult(); if(isFirstDay && playerCount == 12) { electPolice(); isFirstDay = false; } int outId = voteOut(); if(outId == policeId) { cout << "警长被投票出局,"; int choice = (outId == myId) ? inputValidPlayer("撕毁警徽(1) / 移交警徽(2):") : (rand() % 2 + 1); if(choice == 1) { cout << "警徽已撕毁!\n"; policeId = -1; } else { int newPolice = getRandomPlayer(); cout << "警徽移交给" << newPolice + 1 << "号!\n"; policeId = newPolice; } Sleep(2000); } if(players[outId].role == HUNTER) { hunterSkill(outId, false); } day++; } setColor(WHITE); system("pause"); return 0; } -
#include<bits/stdc++.h> #include<windows.h> #include<conio.h> using namespace std; const int daytime=0,night=1; int day=0, during_time=daytime, player_number, my_number; HWND hwnd=GetForegroundWindow(); const int blue=0,yellow=1,red=2,green=3,purple=4,white=5; void color(int c){ switch(c){ case red:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);break; case green:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);break; case yellow:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN);break; case blue:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);break; case white:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN | FOREGROUND_BLUE);break; case purple:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_BLUE);break; } } int idx_police=-1; void gotoxy(int x,int y){ COORD position; position.X=x; position.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position); } void init_Show_Window(){ system("mode con lines=60 cols=188"); ShowWindow(hwnd,SW_MAXIMIZE);//窗口最大化 DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(GetConsoleWindow());//删除×字符 } const int nvwu=0,cunmin=1,yuyanjia=2,langren=3,lieren=4,shouwei=5,good=6,die=1,life=2; class player{ public: int type; int die_or_life; int how(){ return die_or_life; } int is_light; int killer; }; player players[1000]; void change_daytime(){ during_time=daytime; day++; } void change_night(){ during_time=night; } int nnvwu=0,ncunmin=0,nyuyanjia=0,nlangren=0,nlieren=0,nshouwei=0; int idxnvwu,idxshouwei,idxyuyanjia,idxlieren,idxlangren[4]={-1,-1,-1,-1}; bool is_include(int arr[],int b,int l){ for(int i=0;i<l;i++){ if(arr[i]b) return true; } return false; } void init_players(){ my_number=rand()%player_number; if(player_number12) nlangren=4; else if(player_number>=10) nlangren=3; else nlangren=2; for(int i=0;i<player_number;i++) { players[i].die_or_life=life; players[i].is_light=0; players[i].type=-1; players[i].killer=2147483647; } for(int i=0;i<nlangren;i++) { int p=rand()%player_number; if(!is_include(idxlangren,p,4)) idxlangren[i]=p,players[p].type=langren; else i--; Sleep(rand()%80+100); } if(player_number12) { do{ idxshouwei=rand()%player_number; }while(players[idxshouwei].type!=-1);
players[idxshouwei].type=shouwei;
} do{ idxnvwu=rand()%player_number; }while(players[idxnvwu].type!=-1); players[idxnvwu].type=nvwu; if(player_number>=10) { do{ idxlieren=rand()%player_number; }while(players[idxlieren].type!=-1); players[idxlieren].type=lieren;
} do{ idxyuyanjia=rand()%player_number; }while(players[idxyuyanjia].type!=-1); players[idxyuyanjia].type=yuyanjia; for(int i=0;i<player_number;i++) if(players[i].type-1) players[i].type=cunmin, ncunmin++; if(players[my_number].typelangren) { for(int i=0;i<nlangren;i++) { players[idxlangren[i]].is_light=1; } } players[my_number].is_light=1; } void print(){ gotoxy(0,0); cout<<"作者:洛谷393864"; gotoxy(90,0); if(during_timenight) color(red); else color(blue); printf("第%d天 | ",day); if(during_time==night) cout<<"黑夜"; else cout<<"白天";gotoxy(0,3); color(blue); cout<<" 我的号位:"<<my_number+1; for(int i=0;i<player_number;i++){ gotoxy(i*8+1,4); if(i==idx_police) color(yellow); else color(blue); cout<<i+1<<"号位"; gotoxy(i*8+1,5); if(players[i].how()==die){ color(red); cout<<"死 亡"; }else{ color(green); cout<<"存 活"; } gotoxy(i*8+1,6); color(blue); if(players[i].is_light){ if(players[i].is_light==1){ switch(players[i].type){ case nvwu: cout<<"女 巫";break; case yuyanjia: cout<<"\b预言家";break; case cunmin: cout<<"村 民";break; case langren:cout<<"狼 人"; break; case lieren:cout<<"猎 人"; break; case shouwei:cout<<"守 卫"; break; } }else{ cout<<"好人"; } }else{ cout<<"未知"; } }} int is_end(){ int die_bad=0; int die_people=0; int die_god=0; for(int i=0;i<player_number;i++){ if((players[i].type == nvwu || players[i].type == yuyanjia || players[i].type == shouwei)&&players[i].die_or_lifedie) die_god++; else if(players[i].type == langren && players[i].die_or_lifedie) die_bad++; else if(players[i].type == cunmin && players[i].die_or_lifedie) die_people++; } if((die_baddie_people || die_baddie_god)&&(die_bad>=nlangren)) return 3; if(die_bad>=nlangren) return 1; if(die_people>=ncunmin||die_god>=(player_number>=10 ? 3:2)) return 2; return 0; } void before_game(){ srand(time(NULL)); init_Show_Window(); color(green); cout<<"欢迎来到狼人杀游戏\t\t\t注:10 11 12人局开设猎人 12人局开设守卫警长\n______________________\n"; cout<<"请输入玩家人数(8-12人):"; cin>>player_number; while(player_number<8||player_number>12) { cout<<"请重新输入!\n"; cin>>player_number; } system("cls"); cout<<"初始化身份中,请稍等."; for(int i=0;i<6;i++){ for(int j=0;j<12;j++){ cout<<"."; Sleep(50); }
cout<<"\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b"; } system("cls"); init_players(); cout<<"我的号位:"<<my_number+1<<endl <<"我的身份:"; switch(players[my_number].type){ case nvwu: cout<<"女巫\n";break; case yuyanjia: cout<<"预言家\n";break; case cunmin: cout<<"村民\n";break; case langren:cout<<"狼人\n";break;
case lieren:cout<<"猎人\n"; break;
case shouwei:cout<<"守卫\n";break; } change_daytime(); system("pause"); system("cls"); cout<<"游戏加载中.";int ppppp=rand()%3+2; for(int i=0;i<ppppp;i++){ for(int j=0;j<6;j++){ cout<<"."; Sleep(rand()%100+150); }
cout<<"\b\b\b\b\b\b \b\b\b\b\b\b"; }
print(); } void something_before_everyday(){ change_night(); system("cls"); print(); gotoxy(0,7); cout<<"________________________"; gotoxy(0,8); color(white); cout<<"天黑~请闭眼~~~\n"; } int shouweishou=0; int ShouWei(){ color(blue); cout<<"守卫~请睁眼~~\n"; Sleep(1500); cout<<"你要守护的是?\n"; if(players[my_number].typeshouwei&&players[my_number].die_or_life == life){ cin>>shouweishou; while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){ cout<<"请重新输入!\n"; cin>>shouweishou; } cout<<"你今晚要守护的是"<<shouweishou<<"号\n"; Sleep(1500); shouweishou--; }else{ if(players[idxshouwei].die_or_life == life){ shouweishou=rand()%10; while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){ shouweishou=rand()%10; } } }
Sleep(2000); cout<<"守卫请闭眼"<<endl<<endl; return shouweishou; } int LangRen(){ int langrensha=-1; color(red); cout<<"狼人~请睁眼~~~\n"; Sleep(1500); cout<<"你们今晚要杀~谁~~??\n"; if(players[my_number].typelangren&&players[my_number].die_or_life == life){ cin>>langrensha; while(!(langrensha>=1&&langrensha<=player_number&&players[langrensha-1].die_or_lifelife)){ cout<<"请重新输入!\n"; cin>>langrensha; } cout<<"你们今晚要杀的是"<<langrensha--<<"号\n"; Sleep(3500); }else{ while(langrensha==-1 || players[langrensha].die_or_life == die || players[langrensha].typelangren){ langrensha=rand()%player_number; } Sleep(3000); } cout<<"狼人请~闭眼~~\n\n"; return langrensha; } int nvwujiu=0,nvwudu=0,is_nvwujiu=0,is_nvwudu=0; int NvWu(int langrensha){ color(purple); cout<<"女巫请睁眼\n"; Sleep(2000); if(players[my_number].typenvwu&&players[my_number].die_or_life == life){ if(is_nvwujiu) cout<<"你已经用过解药\n",Sleep(1500); else { cout<<"今晚"<<langrensha+1<<"号死了,你想用解药吗?(1想 / 2不想)\n"; int is_nvwujie=0; cin>>is_nvwujie; while(is_nvwujie!=1&&is_nvwujie!=2){ cout<<"请重新输入\n"; cin>>is_nvwujie; } if(is_nvwujie1) { Sleep(1000); cout<<"已经解救"<<langrensha+1<<"号\n"; nvwujiu=langrensha; } is_nvwujiu=1; } Sleep(1500); if(::is_nvwudu) cout<<"你已经用过解药\n",Sleep(1500); else { cout<<"你想用毒药吗?(1想 / 2不想)\n"; Sleep(1500); int is_nvwudu=0; cin>>is_nvwudu; while(is_nvwudu!=1&&is_nvwudu!=2){ cout<<"请重新输入\n"; cin>>is_nvwudu; }
if(is_nvwudu1){ Sleep(1500); cout<<"你想毒谁?\n"; cin>>nvwudu; while(!(nvwudu>=1&&nvwudu<=player_number&&players[nvwudu].die_or_lifelife)){ cout<<"请重新输入\n"; cin>>nvwudu; } nvwudu--; Sleep(1500); cout<<"已经毒死了"<<nvwudu+1<<"号\n"; } ::is_nvwudu=1; }
}else{ if(players[idxnvwu].die_or_life == life){ if(!is_nvwujiu) { int is_jiu=rand()%8; if(is_jiu0){ nvwujiu=langrensha; is_nvwujiu=1; }
} if(!is_nvwudu) { int is_du=rand()%4; if(is_du0){ int num=rand()%player_number; nvwudu=num; is_nvwudu=1; } } } } cout<<"女巫~请闭眼~~\n\n"; return nvwujiu*10000+nvwudu; } int yuyanjiabixutoupiao=-1; void YuYanJia(){ color(green); cout<<"预言家~请睁眼~~\n"; Sleep(2000); if(players[my_number].typeyuyanjia&&players[my_number].die_or_life == life){ cout<<"请问你想查验谁的身份\n"; int p; cin>>p; while(!(p>=1&&p<=player_number)){ cout<<"请重新输入!\n"; cin>>p; } Sleep(2000); cout<<p<<"号的身份是——"; Sleep(1000); if(players[p-1].type == langren){ cout<<"狼人\n"; players[p-1].is_light = 1; }else{ cout<<"好人\n"; players[p-1].is_light = 2; } }else{ int p=-1; while(p==-1||players[p].die_or_lifedie||pidxlieren) p=rand()%player_number; if(players[p].typelangren) { yuyanjiabixutoupiao=p; } } cout<<"预言家请闭眼\n"; } int LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1; void Night(){
LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1; if(player_number12){ SHOUWEISHOU=ShouWei(); Sleep(2000); } LANGRENSHA=LangRen(); Sleep(3500); int nvwu=NvWu(LANGRENSHA); NVWUDU=nvwu%10+nvwu/10%10; NVWUJIU=nvwu/10000%10+nvwu/100000%10; Sleep(3000); YuYanJia(); Sleep(2000); } void Lieren(){ int lierendai=-1; cout<<idxlieren+1<<"号是猎人\n"; players[idxlieren].is_light = 1; Sleep(1000); if(idxlierenmy_number){ cout<<"你想带走几号?\n"; cin>>lierendai; while(lierendai<1||lierendai>player_number||players[lierendai].die_or_lifedie){ cout<<"请重新输入!\n"; cin>>lierendai; } lierendai--; }else{ lierendai=rand()%player_number; while(players[lierendai].die_or_life == die){ lierendai=rand()%player_number; } } Sleep(2000); cout<<"猎人选择带走"<<lierendai+1<<"号\n"; Sleep(2000); players[lierendai].die_or_life = die; } void police_die(); void panduansiwang(){ system("cls"); print(); gotoxy(0,7); cout<<"\n"; Sleep(3000); color(white); cout<<"天亮了\n"; Sleep(2000); gotoxy(0,9); cout<<"昨晚"; bool is_die[15]={false},is_die_lieren=false,flag=false; for(int i=0;i<player_number;i++) { if(players[i].die_or_lifelife) { if(iLANGRENSHA||iNVWUDU) { if(players[i].typelieren) is_die_lieren=true; players[i].killer= (iLANGRENSHA ? langren:nvwu); players[i].die_or_life=die; is_die[i]=true; } if(iSHOUWEISHOU||iNVWUJIU) { if(players[i].typelieren) is_die_lieren=false; players[i].killer=-1; players[i].die_or_life=life; is_die[i]=false; }
} } bool is_police_die=false; for(int i=0;i<player_number;i++) { if(is_die[i]) { if(flag) cout<<"和"<<i+1<<"号"; else cout<<i+1<<"号",flag=true; if(i==idx_police) is_police_die=true; } } if(flag) cout<<"死了\n"; else cout<<"是平安夜\n"; if(is_die_lieren) Lieren(); if(is_police_die) police_die(); } void choose_police(){ system("cls"); print(); color(blue); gotoxy(0,7); cout<<"\n"; color(yellow); cout<<"下面开始选举警长,各位不能选举自己~\n"; int tong[100]={0},cannot[100],must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; memset(cannot,-1,sizeof(cannot)); CHOOSE: color(yellow); Sleep(1500); for(int i=0;i<player_number;i++) { if(players[i].die_or_lifelife&&!is_include(cannot,i,player_number)) { if(imy_number) { cout<<"你要选举几号?\n"; int n; cin>>n; while(n<1||n>player_number||ni+1||players[n-1].die_or_lifedie||!is_include(must,n-1,player_number)) { cout<<"请重新输入!\n"; cin>>n; } cout<<i+1<<"号选举"<<n--<<"号\n"; tong[n]++; } else { int n=rand()%player_number; while(ni||players[n].die_or_lifedie||!is_include(must,n,player_number)) n=rand()%player_number; cout<<i+1<<"号选举"<<n+1<<"号\n"; tong[n]++; } Sleep(1500); } } int idx_max=-1,maxn=-1,len=0; for(int i=0;i<player_number;i++) if(maxn<tong[i]) { maxn=tong[i]; idx_max=i; } int maxn_arr[15]={0}; for(int i=0;i<player_number;i++) { if(tong[i]maxn) { maxn_arr[len++]=i; } } color(blue); if(len>1) { for(int i=0;i<len;i++) { if(ilen-1) { cout<<maxn_arr[i]+1<<"号平票\n"; } else { cout<<maxn_arr[i]+1<<"号,"; } } for(int i=0;i<len;i++) cannot[i]=maxn_arr[i]; for(int i=0;i<player_number;i++) { if(is_include(cannot,i,len)) must[i]=i; else must[i]=-1; } color(white); goto CHOOSE; } cout<<"恭喜"<<idx_max+1<<"号当选警长\n"; Sleep(3000); idx_police=idx_max; return; } int toupiao(){ int tong[100]={0},cannot[100]={},must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; memset(cannot,-1,sizeof(cannot)); gotoxy(0,7); color(blue); cout<<"________________________\n"; color(white); cout<<"下面进入投票环节\n"; memset(tong,0,sizeof(tong)); Sleep(2000); TOUPIAO: for(int i=0;i<player_number;i++){ if(players[i].die_or_life == life&&!is_include(cannot,i,player_number)){ if(imy_number){ color(white); cout<<"你要投几号?\n"; int n; cin>>n; while(!(n>=1&&n<=player_number&&is_include(must,n-1,player_number))){ cout<<"请重新输入!\n"; cin>>n; } Sleep(2000); cout<<setw(2)<<my_number+1<<"号投了"<<setw(2)<<n<<"号"; if(my_numbern-1) color(red),cout<<"快来看!这有个疯子投自己!"; if(iidx_police) color(yellow),cout<<"(警长)\n"; else cout<<"\n"; if(iidx_police) tong[n-1]++; tong[n-1]++; }else{ color(white); int t=-1; while(t==-1 || players[t].die_or_life == die || ti || !is_include(must,t,player_number)){ if(iidxyuyanjia&&yuyanjiabixutoupiao!=-1) { t=yuyanjiabixutoupiao; yuyanjiabixutoupiao=-1; continue; } t=rand()%player_number; if(is_include(idxlangren,i,nlangren)) { if(players[t].type == langren) t=-1; } } cout<<setw(2)<<i+1<<"号"<<"投了"<<setw(2)<<t+1<<"号"; if(iidx_police) cout<<"(警长2票)\n"; else cout<<"\n"; if(iidx_police) tong[t]++; tong[t]++;
} Sleep(rand()%1000+1000); } } int idx_max=-1,maxn=-1,len=0; for(int i=0;i<player_number;i++) if(maxn<tong[i]) { maxn=tong[i]; idx_max=i; } int maxn_arr[15]={0}; for(int i=0;i<player_number;i++) { if(tong[i]maxn) { maxn_arr[len++]=i; } } color(blue); if(len>1) { for(int i=0;i<len;i++) { if(ilen-1) { cout<<maxn_arr[i]+1<<"号平票\n"; } else { cout<<maxn_arr[i]+1<<"号,"; } } for(int i=0;i<len;i++) cannot[i]=maxn_arr[i]; for(int i=0;i<player_number;i++) { if(is_include(cannot,i,len)) must[i]=i; else must[i]=-1; } color(white); goto TOUPIAO; } cout<<idx_max+1<<"号"<<"出局\n"; Sleep(4000); players[idx_max].die_or_life = die; players[idx_max].killer = good; return idx_max; } void police_die(){ color(yellow); int type; if(idx_police==my_number) { Sleep(1550); cout<<"你是想撕毁警徽还是移交警徽?(撕毁输入1,移交输入2)";cin>>type; while(!(type==1||type==2)) { cout<<"请重新输入!\n"; cin>>type; } } else{ type=rand()%3+1; } if(type==1) { cout<<"警长选择撕毁警徽\n"; Sleep(1000); idx_police=-1; } else { int lucky=-1; while(lucky==-1||players[lucky].die_or_life==die) lucky=rand()%player_number; cout<<"警长选择把警徽移交给"<<lucky+1<<"号\n"; Sleep(1500); idx_police=lucky; }} void the_end(){ system("cls"); switch(is_end()){ case 1:cout<<"好人胜利\n\n"; break; case 2:cout<<"狼人胜利\n\n"; break; case 3:cout<<"本局平局\n\n"; break; } for(int i=0;i<player_number;i++){ cout<<i+1<<"号位:\t"; switch(players[i].type){ case nvwu: cout<<"女巫\t";break; case yuyanjia: cout<<"预言家\t";break; case cunmin: cout<<"村民\t";break; case langren:cout<<"狼人\t";break; case lieren:cout<<"猎人\t"; break;
case shouwei:cout<<"守卫\t";break;
} cout<<"最终"; switch(players[i].killer){ case nvwu:cout<<"被女巫毒死\n"; break; case langren:cout<<"被狼人杀死\n"; break; case good:cout<<"被投票出局\n"; break; case lieren:cout<<"被猎人带走\n";break; default :cout<<"存活\n"; } cout<<endl; } } int main(){ int wheel=0; before_game(); while(!is_end()){ something_before_everyday(); Sleep(1500); Night();
change_daytime(); panduansiwang(); Sleep(2000); system("cls"); print(); if(is_end()) break; if(!wheel&&player_number12) { choose_police(); system("cls"); print(); } int idx_max=toupiao(); if(idx_maxidx_police){ police_die(); } if(players[idx_max].type==lieren){ Lieren(); if(is_end()) break; } system("cls"); print(); wheel++; } the_end(); system("pause"); return 0; }#include<bits/stdc++.h> #include<windows.h> #include<conio.h> using namespace std; const int daytime=0,night=1; int day=0, during_time=daytime, player_number, my_number; HWND hwnd=GetForegroundWindow(); const int blue=0,yellow=1,red=2,green=3,purple=4,white=5; void color(int c){ switch(c){ case red:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);break; case green:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);break; case yellow:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN);break; case blue:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);break; case white:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN | FOREGROUND_BLUE);break; case purple:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_BLUE);break; } } int idx_police=-1; void gotoxy(int x,int y){ COORD position; position.X=x; position.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position); } void init_Show_Window(){ system("mode con lines=60 cols=188"); ShowWindow(hwnd,SW_MAXIMIZE);//窗口最大化 DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(GetConsoleWindow());//删除×字符 } const int nvwu=0,cunmin=1,yuyanjia=2,langren=3,lieren=4,shouwei=5,good=6,die=1,life=2; class player{ public: int type; int die_or_life; int how(){ return die_or_life; } int is_light; int killer; }; player players[1000]; void change_daytime(){ during_time=daytime; day++; } void change_night(){ during_time=night; } int nnvwu=0,ncunmin=0,nyuyanjia=0,nlangren=0,nlieren=0,nshouwei=0; int idxnvwu,idxshouwei,idxyuyanjia,idxlieren,idxlangren[4]={-1,-1,-1,-1}; bool is_include(int arr[],int b,int l){ for(int i=0;i<l;i++){ if(arr[i]b) return true; } return false; } void init_players(){ my_number=rand()%player_number; if(player_number12) nlangren=4; else if(player_number>=10) nlangren=3; else nlangren=2; for(int i=0;i<player_number;i++) { players[i].die_or_life=life; players[i].is_light=0; players[i].type=-1; players[i].killer=2147483647; } for(int i=0;i<nlangren;i++) { int p=rand()%player_number; if(!is_include(idxlangren,p,4)) idxlangren[i]=p,players[p].type=langren; else i--; Sleep(rand()%80+100); } if(player_number12) { do{ idxshouwei=rand()%player_number; }while(players[idxshouwei].type!=-1);
players[idxshouwei].type=shouwei;
} do{ idxnvwu=rand()%player_number; }while(players[idxnvwu].type!=-1); players[idxnvwu].type=nvwu; if(player_number>=10) { do{ idxlieren=rand()%player_number; }while(players[idxlieren].type!=-1); players[idxlieren].type=lieren;
} do{ idxyuyanjia=rand()%player_number; }while(players[idxyuyanjia].type!=-1); players[idxyuyanjia].type=yuyanjia; for(int i=0;i<player_number;i++) if(players[i].type-1) players[i].type=cunmin, ncunmin++; if(players[my_number].typelangren) { for(int i=0;i<nlangren;i++) { players[idxlangren[i]].is_light=1; } } players[my_number].is_light=1; } void print(){ gotoxy(0,0); cout<<"作者:洛谷393864"; gotoxy(90,0); if(during_timenight) color(red); else color(blue); printf("第%d天 | ",day); if(during_time==night) cout<<"黑夜"; else cout<<"白天";gotoxy(0,3); color(blue); cout<<" 我的号位:"<<my_number+1; for(int i=0;i<player_number;i++){ gotoxy(i*8+1,4); if(i==idx_police) color(yellow); else color(blue); cout<<i+1<<"号位"; gotoxy(i*8+1,5); if(players[i].how()==die){ color(red); cout<<"死 亡"; }else{ color(green); cout<<"存 活"; } gotoxy(i*8+1,6); color(blue); if(players[i].is_light){ if(players[i].is_light==1){ switch(players[i].type){ case nvwu: cout<<"女 巫";break; case yuyanjia: cout<<"\b预言家";break; case cunmin: cout<<"村 民";break; case langren:cout<<"狼 人"; break; case lieren:cout<<"猎 人"; break; case shouwei:cout<<"守 卫"; break; } }else{ cout<<"好人"; } }else{ cout<<"未知"; } }} int is_end(){ int die_bad=0; int die_people=0; int die_god=0; for(int i=0;i<player_number;i++){ if((players[i].type == nvwu || players[i].type == yuyanjia || players[i].type == shouwei)&&players[i].die_or_lifedie) die_god++; else if(players[i].type == langren && players[i].die_or_lifedie) die_bad++; else if(players[i].type == cunmin && players[i].die_or_lifedie) die_people++; } if((die_baddie_people || die_baddie_god)&&(die_bad>=nlangren)) return 3; if(die_bad>=nlangren) return 1; if(die_people>=ncunmin||die_god>=(player_number>=10 ? 3:2)) return 2; return 0; } void before_game(){ srand(time(NULL)); init_Show_Window(); color(green); cout<<"欢迎来到狼人杀游戏\t\t\t注:10 11 12人局开设猎人 12人局开设守卫警长\n______________________\n"; cout<<"请输入玩家人数(8-12人):"; cin>>player_number; while(player_number<8||player_number>12) { cout<<"请重新输入!\n"; cin>>player_number; } system("cls"); cout<<"初始化身份中,请稍等."; for(int i=0;i<6;i++){ for(int j=0;j<12;j++){ cout<<"."; Sleep(50); }
cout<<"\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b"; } system("cls"); init_players(); cout<<"我的号位:"<<my_number+1<<endl <<"我的身份:"; switch(players[my_number].type){ case nvwu: cout<<"女巫\n";break; case yuyanjia: cout<<"预言家\n";break; case cunmin: cout<<"村民\n";break; case langren:cout<<"狼人\n";break;
case lieren:cout<<"猎人\n"; break;
case shouwei:cout<<"守卫\n";break; } change_daytime(); system("pause"); system("cls"); cout<<"游戏加载中.";int ppppp=rand()%3+2; for(int i=0;i<ppppp;i++){ for(int j=0;j<6;j++){ cout<<"."; Sleep(rand()%100+150); }
cout<<"\b\b\b\b\b\b \b\b\b\b\b\b"; }
print(); } void something_before_everyday(){ change_night(); system("cls"); print(); gotoxy(0,7); cout<<"________________________"; gotoxy(0,8); color(white); cout<<"天黑~请闭眼~~~\n"; } int shouweishou=0; int ShouWei(){ color(blue); cout<<"守卫~请睁眼~~\n"; Sleep(1500); cout<<"你要守护的是?\n"; if(players[my_number].typeshouwei&&players[my_number].die_or_life == life){ cin>>shouweishou; while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){ cout<<"请重新输入!\n"; cin>>shouweishou; } cout<<"你今晚要守护的是"<<shouweishou<<"号\n"; Sleep(1500); shouweishou--; }else{ if(players[idxshouwei].die_or_life == life){ shouweishou=rand()%10; while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){ shouweishou=rand()%10; } } }
Sleep(2000); cout<<"守卫请闭眼"<<endl<<endl; return shouweishou; } int LangRen(){ int langrensha=-1; color(red); cout<<"狼人~请睁眼~~~\n"; Sleep(1500); cout<<"你们今晚要杀~谁~~??\n"; if(players[my_number].typelangren&&players[my_number].die_or_life == life){ cin>>langrensha; while(!(langrensha>=1&&langrensha<=player_number&&players[langrensha-1].die_or_lifelife)){ cout<<"请重新输入!\n"; cin>>langrensha; } cout<<"你们今晚要杀的是"<<langrensha--<<"号\n"; Sleep(3500); }else{ while(langrensha==-1 || players[langrensha].die_or_life == die || players[langrensha].typelangren){ langrensha=rand()%player_number; } Sleep(3000); } cout<<"狼人请~闭眼~~\n\n"; return langrensha; } int nvwujiu=0,nvwudu=0,is_nvwujiu=0,is_nvwudu=0; int NvWu(int langrensha){ color(purple); cout<<"女巫请睁眼\n"; Sleep(2000); if(players[my_number].typenvwu&&players[my_number].die_or_life == life){ if(is_nvwujiu) cout<<"你已经用过解药\n",Sleep(1500); else { cout<<"今晚"<<langrensha+1<<"号死了,你想用解药吗?(1想 / 2不想)\n"; int is_nvwujie=0; cin>>is_nvwujie; while(is_nvwujie!=1&&is_nvwujie!=2){ cout<<"请重新输入\n"; cin>>is_nvwujie; } if(is_nvwujie1) { Sleep(1000); cout<<"已经解救"<<langrensha+1<<"号\n"; nvwujiu=langrensha; } is_nvwujiu=1; } Sleep(1500); if(::is_nvwudu) cout<<"你已经用过解药\n",Sleep(1500); else { cout<<"你想用毒药吗?(1想 / 2不想)\n"; Sleep(1500); int is_nvwudu=0; cin>>is_nvwudu; while(is_nvwudu!=1&&is_nvwudu!=2){ cout<<"请重新输入\n"; cin>>is_nvwudu; }
if(is_nvwudu1){ Sleep(1500); cout<<"你想毒谁?\n"; cin>>nvwudu; while(!(nvwudu>=1&&nvwudu<=player_number&&players[nvwudu].die_or_lifelife)){ cout<<"请重新输入\n"; cin>>nvwudu; } nvwudu--; Sleep(1500); cout<<"已经毒死了"<<nvwudu+1<<"号\n"; } ::is_nvwudu=1; }
}else{ if(players[idxnvwu].die_or_life == life){ if(!is_nvwujiu) { int is_jiu=rand()%8; if(is_jiu0){ nvwujiu=langrensha; is_nvwujiu=1; }
} if(!is_nvwudu) { int is_du=rand()%4; if(is_du0){ int num=rand()%player_number; nvwudu=num; is_nvwudu=1; } } } } cout<<"女巫~请闭眼~~\n\n"; return nvwujiu*10000+nvwudu; } int yuyanjiabixutoupiao=-1; void YuYanJia(){ color(green); cout<<"预言家~请睁眼~~\n"; Sleep(2000); if(players[my_number].typeyuyanjia&&players[my_number].die_or_life == life){ cout<<"请问你想查验谁的身份\n"; int p; cin>>p; while(!(p>=1&&p<=player_number)){ cout<<"请重新输入!\n"; cin>>p; } Sleep(2000); cout<<p<<"号的身份是——"; Sleep(1000); if(players[p-1].type == langren){ cout<<"狼人\n"; players[p-1].is_light = 1; }else{ cout<<"好人\n"; players[p-1].is_light = 2; } }else{ int p=-1; while(p==-1||players[p].die_or_lifedie||pidxlieren) p=rand()%player_number; if(players[p].typelangren) { yuyanjiabixutoupiao=p; } } cout<<"预言家请闭眼\n"; } int LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1; void Night(){
LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1; if(player_number12){ SHOUWEISHOU=ShouWei(); Sleep(2000); } LANGRENSHA=LangRen(); Sleep(3500); int nvwu=NvWu(LANGRENSHA); NVWUDU=nvwu%10+nvwu/10%10; NVWUJIU=nvwu/10000%10+nvwu/100000%10; Sleep(3000); YuYanJia(); Sleep(2000); } void Lieren(){ int lierendai=-1; cout<<idxlieren+1<<"号是猎人\n"; players[idxlieren].is_light = 1; Sleep(1000); if(idxlierenmy_number){ cout<<"你想带走几号?\n"; cin>>lierendai; while(lierendai<1||lierendai>player_number||players[lierendai].die_or_lifedie){ cout<<"请重新输入!\n"; cin>>lierendai; } lierendai--; }else{ lierendai=rand()%player_number; while(players[lierendai].die_or_life == die){ lierendai=rand()%player_number; } } Sleep(2000); cout<<"猎人选择带走"<<lierendai+1<<"号\n"; Sleep(2000); players[lierendai].die_or_life = die; } void police_die(); void panduansiwang(){ system("cls"); print(); gotoxy(0,7); cout<<"\n"; Sleep(3000); color(white); cout<<"天亮了\n"; Sleep(2000); gotoxy(0,9); cout<<"昨晚"; bool is_die[15]={false},is_die_lieren=false,flag=false; for(int i=0;i<player_number;i++) { if(players[i].die_or_lifelife) { if(iLANGRENSHA||iNVWUDU) { if(players[i].typelieren) is_die_lieren=true; players[i].killer= (iLANGRENSHA ? langren:nvwu); players[i].die_or_life=die; is_die[i]=true; } if(iSHOUWEISHOU||iNVWUJIU) { if(players[i].typelieren) is_die_lieren=false; players[i].killer=-1; players[i].die_or_life=life; is_die[i]=false; }
} } bool is_police_die=false; for(int i=0;i<player_number;i++) { if(is_die[i]) { if(flag) cout<<"和"<<i+1<<"号"; else cout<<i+1<<"号",flag=true; if(i==idx_police) is_police_die=true; } } if(flag) cout<<"死了\n"; else cout<<"是平安夜\n"; if(is_die_lieren) Lieren(); if(is_police_die) police_die(); } void choose_police(){ system("cls"); print(); color(blue); gotoxy(0,7); cout<<"\n"; color(yellow); cout<<"下面开始选举警长,各位不能选举自己~\n"; int tong[100]={0},cannot[100],must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; memset(cannot,-1,sizeof(cannot)); CHOOSE: color(yellow); Sleep(1500); for(int i=0;i<player_number;i++) { if(players[i].die_or_lifelife&&!is_include(cannot,i,player_number)) { if(imy_number) { cout<<"你要选举几号?\n"; int n; cin>>n; while(n<1||n>player_number||ni+1||players[n-1].die_or_lifedie||!is_include(must,n-1,player_number)) { cout<<"请重新输入!\n"; cin>>n; } cout<<i+1<<"号选举"<<n--<<"号\n"; tong[n]++; } else { int n=rand()%player_number; while(ni||players[n].die_or_lifedie||!is_include(must,n,player_number)) n=rand()%player_number; cout<<i+1<<"号选举"<<n+1<<"号\n"; tong[n]++; } Sleep(1500); } } int idx_max=-1,maxn=-1,len=0; for(int i=0;i<player_number;i++) if(maxn<tong[i]) { maxn=tong[i]; idx_max=i; } int maxn_arr[15]={0}; for(int i=0;i<player_number;i++) { if(tong[i]maxn) { maxn_arr[len++]=i; } } color(blue); if(len>1) { for(int i=0;i<len;i++) { if(ilen-1) { cout<<maxn_arr[i]+1<<"号平票\n"; } else { cout<<maxn_arr[i]+1<<"号,"; } } for(int i=0;i<len;i++) cannot[i]=maxn_arr[i]; for(int i=0;i<player_number;i++) { if(is_include(cannot,i,len)) must[i]=i; else must[i]=-1; } color(white); goto CHOOSE; } cout<<"恭喜"<<idx_max+1<<"号当选警长\n"; Sleep(3000); idx_police=idx_max; return; } int toupiao(){ int tong[100]={0},cannot[100]={},must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; memset(cannot,-1,sizeof(cannot)); gotoxy(0,7); color(blue); cout<<"________________________\n"; color(white); cout<<"下面进入投票环节\n"; memset(tong,0,sizeof(tong)); Sleep(2000); TOUPIAO: for(int i=0;i<player_number;i++){ if(players[i].die_or_life == life&&!is_include(cannot,i,player_number)){ if(imy_number){ color(white); cout<<"你要投几号?\n"; int n; cin>>n; while(!(n>=1&&n<=player_number&&is_include(must,n-1,player_number))){ cout<<"请重新输入!\n"; cin>>n; } Sleep(2000); cout<<setw(2)<<my_number+1<<"号投了"<<setw(2)<<n<<"号"; if(my_numbern-1) color(red),cout<<"快来看!这有个疯子投自己!"; if(iidx_police) color(yellow),cout<<"(警长)\n"; else cout<<"\n"; if(iidx_police) tong[n-1]++; tong[n-1]++; }else{ color(white); int t=-1; while(t==-1 || players[t].die_or_life == die || ti || !is_include(must,t,player_number)){ if(iidxyuyanjia&&yuyanjiabixutoupiao!=-1) { t=yuyanjiabixutoupiao; yuyanjiabixutoupiao=-1; continue; } t=rand()%player_number; if(is_include(idxlangren,i,nlangren)) { if(players[t].type == langren) t=-1; } } cout<<setw(2)<<i+1<<"号"<<"投了"<<setw(2)<<t+1<<"号"; if(iidx_police) cout<<"(警长2票)\n"; else cout<<"\n"; if(iidx_police) tong[t]++; tong[t]++;
} Sleep(rand()%1000+1000); } } int idx_max=-1,maxn=-1,len=0; for(int i=0;i<player_number;i++) if(maxn<tong[i]) { maxn=tong[i]; idx_max=i; } int maxn_arr[15]={0}; for(int i=0;i<player_number;i++) { if(tong[i]maxn) { maxn_arr[len++]=i; } } color(blue); if(len>1) { for(int i=0;i<len;i++) { if(ilen-1) { cout<<maxn_arr[i]+1<<"号平票\n"; } else { cout<<maxn_arr[i]+1<<"号,"; } } for(int i=0;i<len;i++) cannot[i]=maxn_arr[i]; for(int i=0;i<player_number;i++) { if(is_include(cannot,i,len)) must[i]=i; else must[i]=-1; } color(white); goto TOUPIAO; } cout<<idx_max+1<<"号"<<"出局\n"; Sleep(4000); players[idx_max].die_or_life = die; players[idx_max].killer = good; return idx_max; } void police_die(){ color(yellow); int type; if(idx_police==my_number) { Sleep(1550); cout<<"你是想撕毁警徽还是移交警徽?(撕毁输入1,移交输入2)";cin>>type; while(!(type==1||type==2)) { cout<<"请重新输入!\n"; cin>>type; } } else{ type=rand()%3+1; } if(type==1) { cout<<"警长选择撕毁警徽\n"; Sleep(1000); idx_police=-1; } else { int lucky=-1; while(lucky==-1||players[lucky].die_or_life==die) lucky=rand()%player_number; cout<<"警长选择把警徽移交给"<<lucky+1<<"号\n"; Sleep(1500); idx_police=lucky; }} void the_end(){ system("cls"); switch(is_end()){ case 1:cout<<"好人胜利\n\n"; break; case 2:cout<<"狼人胜利\n\n"; break; case 3:cout<<"本局平局\n\n"; break; } for(int i=0;i<player_number;i++){ cout<<i+1<<"号位:\t"; switch(players[i].type){ case nvwu: cout<<"女巫\t";break; case yuyanjia: cout<<"预言家\t";break; case cunmin: cout<<"村民\t";break; case langren:cout<<"狼人\t";break; case lieren:cout<<"猎人\t"; break;
case shouwei:cout<<"守卫\t";break;
} cout<<"最终"; switch(players[i].killer){ case nvwu:cout<<"被女巫毒死\n"; break; case langren:cout<<"被狼人杀死\n"; break; case good:cout<<"被投票出局\n"; break; case lieren:cout<<"被猎人带走\n";break; default :cout<<"存活\n"; } cout<<endl; } } int main(){ int wheel=0; before_game(); while(!is_end()){ something_before_everyday(); Sleep(1500); Night();
change_daytime(); panduansiwang(); Sleep(2000); system("cls"); print(); if(is_end()) break; if(!wheel&&player_number12) { choose_police(); system("cls"); print(); } int idx_max=toupiao(); if(idx_maxidx_police){ police_die(); } if(players[idx_max].type==lieren){ Lieren(); if(is_end()) break; } system("cls"); print(); wheel++; } the_end(); system("pause"); return 0; }#include<bits/stdc++.h> #include<windows.h> #include<conio.h> using namespace std; const int daytime=0,night=1; int day=0, during_time=daytime, player_number, my_number; HWND hwnd=GetForegroundWindow(); const int blue=0,yellow=1,red=2,green=3,purple=4,white=5; void color(int c){ switch(c){ case red:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);break; case green:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);break; case yellow:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN);break; case blue:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);break; case white:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN | FOREGROUND_BLUE);break; case purple:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_BLUE);break; } } int idx_police=-1; void gotoxy(int x,int y){ COORD position; position.X=x; position.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position); } void init_Show_Window(){ system("mode con lines=60 cols=188"); ShowWindow(hwnd,SW_MAXIMIZE);//窗口最大化 DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(GetConsoleWindow());//删除×字符 } const int nvwu=0,cunmin=1,yuyanjia=2,langren=3,lieren=4,shouwei=5,good=6,die=1,life=2; class player{ public: int type; int die_or_life; int how(){ return die_or_life; } int is_light; int killer; }; player players[1000]; void change_daytime(){ during_time=daytime; day++; } void change_night(){ during_time=night; } int nnvwu=0,ncunmin=0,nyuyanjia=0,nlangren=0,nlieren=0,nshouwei=0; int idxnvwu,idxshouwei,idxyuyanjia,idxlieren,idxlangren[4]={-1,-1,-1,-1}; bool is_include(int arr[],int b,int l){ for(int i=0;i<l;i++){ if(arr[i]b) return true; } return false; } void init_players(){ my_number=rand()%player_number; if(player_number12) nlangren=4; else if(player_number>=10) nlangren=3; else nlangren=2; for(int i=0;i<player_number;i++) { players[i].die_or_life=life; players[i].is_light=0; players[i].type=-1; players[i].killer=2147483647; } for(int i=0;i<nlangren;i++) { int p=rand()%player_number; if(!is_include(idxlangren,p,4)) idxlangren[i]=p,players[p].type=langren; else i--; Sleep(rand()%80+100); } if(player_number12) { do{ idxshouwei=rand()%player_number; }while(players[idxshouwei].type!=-1);
players[idxshouwei].type=shouwei;
} do{ idxnvwu=rand()%player_number; }while(players[idxnvwu].type!=-1); players[idxnvwu].type=nvwu; if(player_number>=10) { do{ idxlieren=rand()%player_number; }while(players[idxlieren].type!=-1); players[idxlieren].type=lieren;
} do{ idxyuyanjia=rand()%player_number; }while(players[idxyuyanjia].type!=-1); players[idxyuyanjia].type=yuyanjia; for(int i=0;i<player_number;i++) if(players[i].type-1) players[i].type=cunmin, ncunmin++; if(players[my_number].typelangren) { for(int i=0;i<nlangren;i++) { players[idxlangren[i]].is_light=1; } } players[my_number].is_light=1; } void print(){ gotoxy(0,0); cout<<"作者:洛谷393864"; gotoxy(90,0); if(during_timenight) color(red); else color(blue); printf("第%d天 | ",day); if(during_time==night) cout<<"黑夜"; else cout<<"白天";gotoxy(0,3); color(blue); cout<<" 我的号位:"<<my_number+1; for(int i=0;i<player_number;i++){ gotoxy(i*8+1,4); if(i==idx_police) color(yellow); else color(blue); cout<<i+1<<"号位"; gotoxy(i*8+1,5); if(players[i].how()==die){ color(red); cout<<"死 亡"; }else{ color(green); cout<<"存 活"; } gotoxy(i*8+1,6); color(blue); if(players[i].is_light){ if(players[i].is_light==1){ switch(players[i].type){ case nvwu: cout<<"女 巫";break; case yuyanjia: cout<<"\b预言家";break; case cunmin: cout<<"村 民";break; case langren:cout<<"狼 人"; break; case lieren:cout<<"猎 人"; break; case shouwei:cout<<"守 卫"; break; } }else{ cout<<"好人"; } }else{ cout<<"未知"; } }} int is_end(){ int die_bad=0; int die_people=0; int die_god=0; for(int i=0;i<player_number;i++){ if((players[i].type == nvwu || players[i].type == yuyanjia || players[i].type == shouwei)&&players[i].die_or_lifedie) die_god++; else if(players[i].type == langren && players[i].die_or_lifedie) die_bad++; else if(players[i].type == cunmin && players[i].die_or_lifedie) die_people++; } if((die_baddie_people || die_baddie_god)&&(die_bad>=nlangren)) return 3; if(die_bad>=nlangren) return 1; if(die_people>=ncunmin||die_god>=(player_number>=10 ? 3:2)) return 2; return 0; } void before_game(){ srand(time(NULL)); init_Show_Window(); color(green); cout<<"欢迎来到狼人杀游戏\t\t\t注:10 11 12人局开设猎人 12人局开设守卫警长\n______________________\n"; cout<<"请输入玩家人数(8-12人):"; cin>>player_number; while(player_number<8||player_number>12) { cout<<"请重新输入!\n"; cin>>player_number; } system("cls"); cout<<"初始化身份中,请稍等."; for(int i=0;i<6;i++){ for(int j=0;j<12;j++){ cout<<"."; Sleep(50); }
cout<<"\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b"; } system("cls"); init_players(); cout<<"我的号位:"<<my_number+1<<endl <<"我的身份:"; switch(players[my_number].type){ case nvwu: cout<<"女巫\n";break; case yuyanjia: cout<<"预言家\n";break; case cunmin: cout<<"村民\n";break; case langren:cout<<"狼人\n";break;
case lieren:cout<<"猎人\n"; break;
case shouwei:cout<<"守卫\n";break; } change_daytime(); system("pause"); system("cls"); cout<<"游戏加载中.";int ppppp=rand()%3+2; for(int i=0;i<ppppp;i++){ for(int j=0;j<6;j++){ cout<<"."; Sleep(rand()%100+150); }
cout<<"\b\b\b\b\b\b \b\b\b\b\b\b"; }
print(); } void something_before_everyday(){ change_night(); system("cls"); print(); gotoxy(0,7); cout<<"________________________"; gotoxy(0,8); color(white); cout<<"天黑~请闭眼~~~\n"; } int shouweishou=0; int ShouWei(){ color(blue); cout<<"守卫~请睁眼~~\n"; Sleep(1500); cout<<"你要守护的是?\n"; if(players[my_number].typeshouwei&&players[my_number].die_or_life == life){ cin>>shouweishou; while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){ cout<<"请重新输入!\n"; cin>>shouweishou; } cout<<"你今晚要守护的是"<<shouweishou<<"号\n"; Sleep(1500); shouweishou--; }else{ if(players[idxshouwei].die_or_life == life){ shouweishou=rand()%10; while(!(shouweishou>=1&&shouweishou<=player_number&&players[shouweishou-1].die_or_life == life)){ shouweishou=rand()%10; } } }
Sleep(2000); cout<<"守卫请闭眼"<<endl<<endl; return shouweishou; } int LangRen(){ int langrensha=-1; color(red); cout<<"狼人~请睁眼~~~\n"; Sleep(1500); cout<<"你们今晚要杀~谁~~??\n"; if(players[my_number].typelangren&&players[my_number].die_or_life == life){ cin>>langrensha; while(!(langrensha>=1&&langrensha<=player_number&&players[langrensha-1].die_or_lifelife)){ cout<<"请重新输入!\n"; cin>>langrensha; } cout<<"你们今晚要杀的是"<<langrensha--<<"号\n"; Sleep(3500); }else{ while(langrensha==-1 || players[langrensha].die_or_life == die || players[langrensha].typelangren){ langrensha=rand()%player_number; } Sleep(3000); } cout<<"狼人请~闭眼~~\n\n"; return langrensha; } int nvwujiu=0,nvwudu=0,is_nvwujiu=0,is_nvwudu=0; int NvWu(int langrensha){ color(purple); cout<<"女巫请睁眼\n"; Sleep(2000); if(players[my_number].typenvwu&&players[my_number].die_or_life == life){ if(is_nvwujiu) cout<<"你已经用过解药\n",Sleep(1500); else { cout<<"今晚"<<langrensha+1<<"号死了,你想用解药吗?(1想 / 2不想)\n"; int is_nvwujie=0; cin>>is_nvwujie; while(is_nvwujie!=1&&is_nvwujie!=2){ cout<<"请重新输入\n"; cin>>is_nvwujie; } if(is_nvwujie1) { Sleep(1000); cout<<"已经解救"<<langrensha+1<<"号\n"; nvwujiu=langrensha; } is_nvwujiu=1; } Sleep(1500); if(::is_nvwudu) cout<<"你已经用过解药\n",Sleep(1500); else { cout<<"你想用毒药吗?(1想 / 2不想)\n"; Sleep(1500); int is_nvwudu=0; cin>>is_nvwudu; while(is_nvwudu!=1&&is_nvwudu!=2){ cout<<"请重新输入\n"; cin>>is_nvwudu; }
if(is_nvwudu1){ Sleep(1500); cout<<"你想毒谁?\n"; cin>>nvwudu; while(!(nvwudu>=1&&nvwudu<=player_number&&players[nvwudu].die_or_lifelife)){ cout<<"请重新输入\n"; cin>>nvwudu; } nvwudu--; Sleep(1500); cout<<"已经毒死了"<<nvwudu+1<<"号\n"; } ::is_nvwudu=1; }
}else{ if(players[idxnvwu].die_or_life == life){ if(!is_nvwujiu) { int is_jiu=rand()%8; if(is_jiu0){ nvwujiu=langrensha; is_nvwujiu=1; }
} if(!is_nvwudu) { int is_du=rand()%4; if(is_du0){ int num=rand()%player_number; nvwudu=num; is_nvwudu=1; } } } } cout<<"女巫~请闭眼~~\n\n"; return nvwujiu*10000+nvwudu; } int yuyanjiabixutoupiao=-1; void YuYanJia(){ color(green); cout<<"预言家~请睁眼~~\n"; Sleep(2000); if(players[my_number].typeyuyanjia&&players[my_number].die_or_life == life){ cout<<"请问你想查验谁的身份\n"; int p; cin>>p; while(!(p>=1&&p<=player_number)){ cout<<"请重新输入!\n"; cin>>p; } Sleep(2000); cout<<p<<"号的身份是——"; Sleep(1000); if(players[p-1].type == langren){ cout<<"狼人\n"; players[p-1].is_light = 1; }else{ cout<<"好人\n"; players[p-1].is_light = 2; } }else{ int p=-1; while(p==-1||players[p].die_or_lifedie||pidxlieren) p=rand()%player_number; if(players[p].typelangren) { yuyanjiabixutoupiao=p; } } cout<<"预言家请闭眼\n"; } int LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1; void Night(){
LANGRENSHA=-1,NVWUDU=-1,NVWUJIU=-1,SHOUWEISHOU=-1; if(player_number12){ SHOUWEISHOU=ShouWei(); Sleep(2000); } LANGRENSHA=LangRen(); Sleep(3500); int nvwu=NvWu(LANGRENSHA); NVWUDU=nvwu%10+nvwu/10%10; NVWUJIU=nvwu/10000%10+nvwu/100000%10; Sleep(3000); YuYanJia(); Sleep(2000); } void Lieren(){ int lierendai=-1; cout<<idxlieren+1<<"号是猎人\n"; players[idxlieren].is_light = 1; Sleep(1000); if(idxlierenmy_number){ cout<<"你想带走几号?\n"; cin>>lierendai; while(lierendai<1||lierendai>player_number||players[lierendai].die_or_lifedie){ cout<<"请重新输入!\n"; cin>>lierendai; } lierendai--; }else{ lierendai=rand()%player_number; while(players[lierendai].die_or_life == die){ lierendai=rand()%player_number; } } Sleep(2000); cout<<"猎人选择带走"<<lierendai+1<<"号\n"; Sleep(2000); players[lierendai].die_or_life = die; } void police_die(); void panduansiwang(){ system("cls"); print(); gotoxy(0,7); cout<<"\n"; Sleep(3000); color(white); cout<<"天亮了\n"; Sleep(2000); gotoxy(0,9); cout<<"昨晚"; bool is_die[15]={false},is_die_lieren=false,flag=false; for(int i=0;i<player_number;i++) { if(players[i].die_or_lifelife) { if(iLANGRENSHA||iNVWUDU) { if(players[i].typelieren) is_die_lieren=true; players[i].killer= (iLANGRENSHA ? langren:nvwu); players[i].die_or_life=die; is_die[i]=true; } if(iSHOUWEISHOU||iNVWUJIU) { if(players[i].typelieren) is_die_lieren=false; players[i].killer=-1; players[i].die_or_life=life; is_die[i]=false; }
} } bool is_police_die=false; for(int i=0;i<player_number;i++) { if(is_die[i]) { if(flag) cout<<"和"<<i+1<<"号"; else cout<<i+1<<"号",flag=true; if(i==idx_police) is_police_die=true; } } if(flag) cout<<"死了\n"; else cout<<"是平安夜\n"; if(is_die_lieren) Lieren(); if(is_police_die) police_die(); } void choose_police(){ system("cls"); print(); color(blue); gotoxy(0,7); cout<<"\n"; color(yellow); cout<<"下面开始选举警长,各位不能选举自己~\n"; int tong[100]={0},cannot[100],must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; memset(cannot,-1,sizeof(cannot)); CHOOSE: color(yellow); Sleep(1500); for(int i=0;i<player_number;i++) { if(players[i].die_or_lifelife&&!is_include(cannot,i,player_number)) { if(imy_number) { cout<<"你要选举几号?\n"; int n; cin>>n; while(n<1||n>player_number||ni+1||players[n-1].die_or_lifedie||!is_include(must,n-1,player_number)) { cout<<"请重新输入!\n"; cin>>n; } cout<<i+1<<"号选举"<<n--<<"号\n"; tong[n]++; } else { int n=rand()%player_number; while(ni||players[n].die_or_lifedie||!is_include(must,n,player_number)) n=rand()%player_number; cout<<i+1<<"号选举"<<n+1<<"号\n"; tong[n]++; } Sleep(1500); } } int idx_max=-1,maxn=-1,len=0; for(int i=0;i<player_number;i++) if(maxn<tong[i]) { maxn=tong[i]; idx_max=i; } int maxn_arr[15]={0}; for(int i=0;i<player_number;i++) { if(tong[i]maxn) { maxn_arr[len++]=i; } } color(blue); if(len>1) { for(int i=0;i<len;i++) { if(ilen-1) { cout<<maxn_arr[i]+1<<"号平票\n"; } else { cout<<maxn_arr[i]+1<<"号,"; } } for(int i=0;i<len;i++) cannot[i]=maxn_arr[i]; for(int i=0;i<player_number;i++) { if(is_include(cannot,i,len)) must[i]=i; else must[i]=-1; } color(white); goto CHOOSE; } cout<<"恭喜"<<idx_max+1<<"号当选警长\n"; Sleep(3000); idx_police=idx_max; return; } int toupiao(){ int tong[100]={0},cannot[100]={},must[100]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; memset(cannot,-1,sizeof(cannot)); gotoxy(0,7); color(blue); cout<<"________________________\n"; color(white); cout<<"下面进入投票环节\n"; memset(tong,0,sizeof(tong)); Sleep(2000); TOUPIAO: for(int i=0;i<player_number;i++){ if(players[i].die_or_life == life&&!is_include(cannot,i,player_number)){ if(imy_number){ color(white); cout<<"你要投几号?\n"; int n; cin>>n; while(!(n>=1&&n<=player_number&&is_include(must,n-1,player_number))){ cout<<"请重新输入!\n"; cin>>n; } Sleep(2000); cout<<setw(2)<<my_number+1<<"号投了"<<setw(2)<<n<<"号"; if(my_numbern-1) color(red),cout<<"快来看!这有个疯子投自己!"; if(iidx_police) color(yellow),cout<<"(警长)\n"; else cout<<"\n"; if(iidx_police) tong[n-1]++; tong[n-1]++; }else{ color(white); int t=-1; while(t==-1 || players[t].die_or_life == die || ti || !is_include(must,t,player_number)){ if(iidxyuyanjia&&yuyanjiabixutoupiao!=-1) { t=yuyanjiabixutoupiao; yuyanjiabixutoupiao=-1; continue; } t=rand()%player_number; if(is_include(idxlangren,i,nlangren)) { if(players[t].type == langren) t=-1; } } cout<<setw(2)<<i+1<<"号"<<"投了"<<setw(2)<<t+1<<"号"; if(iidx_police) cout<<"(警长2票)\n"; else cout<<"\n"; if(iidx_police) tong[t]++; tong[t]++;
} Sleep(rand()%1000+1000); } } int idx_max=-1,maxn=-1,len=0; for(int i=0;i<player_number;i++) if(maxn<tong[i]) { maxn=tong[i]; idx_max=i; } int maxn_arr[15]={0}; for(int i=0;i<player_number;i++) { if(tong[i]maxn) { maxn_arr[len++]=i; } } color(blue); if(len>1) { for(int i=0;i<len;i++) { if(ilen-1) { cout<<maxn_arr[i]+1<<"号平票\n"; } else { cout<<maxn_arr[i]+1<<"号,"; } } for(int i=0;i<len;i++) cannot[i]=maxn_arr[i]; for(int i=0;i<player_number;i++) { if(is_include(cannot,i,len)) must[i]=i; else must[i]=-1; } color(white); goto TOUPIAO; } cout<<idx_max+1<<"号"<<"出局\n"; Sleep(4000); players[idx_max].die_or_life = die; players[idx_max].killer = good; return idx_max; } void police_die(){ color(yellow); int type; if(idx_police==my_number) { Sleep(1550); cout<<"你是想撕毁警徽还是移交警徽?(撕毁输入1,移交输入2)";cin>>type; while(!(type==1||type==2)) { cout<<"请重新输入!\n"; cin>>type; } } else{ type=rand()%3+1; } if(type==1) { cout<<"警长选择撕毁警徽\n"; Sleep(1000); idx_police=-1; } else { int lucky=-1; while(lucky==-1||players[lucky].die_or_life==die) lucky=rand()%player_number; cout<<"警长选择把警徽移交给"<<lucky+1<<"号\n"; Sleep(1500); idx_police=lucky; }} void the_end(){ system("cls"); switch(is_end()){ case 1:cout<<"好人胜利\n\n"; break; case 2:cout<<"狼人胜利\n\n"; break; case 3:cout<<"本局平局\n\n"; break; } for(int i=0;i<player_number;i++){ cout<<i+1<<"号位:\t"; switch(players[i].type){ case nvwu: cout<<"女巫\t";break; case yuyanjia: cout<<"预言家\t";break; case cunmin: cout<<"村民\t";break; case langren:cout<<"狼人\t";break; case lieren:cout<<"猎人\t"; break;
case shouwei:cout<<"守卫\t";break;
} cout<<"最终"; switch(players[i].killer){ case nvwu:cout<<"被女巫毒死\n"; break; case langren:cout<<"被狼人杀死\n"; break; case good:cout<<"被投票出局\n"; break; case lieren:cout<<"被猎人带走\n";break; default :cout<<"存活\n"; } cout<<endl; } } int main(){ int wheel=0; before_game(); while(!is_end()){ something_before_everyday(); Sleep(1500); Night();
change_daytime(); panduansiwang(); Sleep(2000); system("cls"); print(); if(is_end()) break; if(!wheel&&player_number12) { choose_police(); system("cls"); print(); } int idx_max=toupiao(); if(idx_maxidx_police){ police_die(); } if(players[idx_max].type==lieren){ Lieren(); if(is_end()) break; } system("cls"); print(); wheel++; } the_end(); system("pause"); return 0; } -
#include<bits/stdc++.h> #include<windows.h> #include<conio.h> using namespace std; const int DAY = 0, NIGHT = 1; enum Color { BLUE, YELLOW, RED, GREEN, PURPLE, WHITE }; enum Role { WITCH = 0, VILLAGER = 1, SEER = 2, WEREWOLF = 3, HUNTER = 4, GUARD = 5, GOOD = 6 }; enum Status { DEAD = 1, ALIVE = 2 }; int day = 0; int current_phase = DAY; int player_count; int my_seat; HWND hwnd = GetForegroundWindow(); int police_seat = -1; int wolf_count = 0; int witch_seat = -1, guard_seat = -1, seer_seat = -1, hunter_seat = -1; int wolf_seats[4] = {-1, -1, -1, -1}; bool witch_healed = false; bool witch_poisoned = false; int heal_target = -1; int poison_target = -1; int guard_protect = -1; int seer_check_target = -1; struct Player { Role role = (Role)-1; Status status = ALIVE; bool known = false; int killer = INT_MAX; }; Player players[100]; void set_color(Color c) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); switch(c) { case RED: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED); break; case GREEN: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_GREEN); break; case YELLOW: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); break; case BLUE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_BLUE); break; case WHITE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); break; case PURPLE: SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); break; } } void gotoxy(int x, int y) { COORD pos = { (SHORT)x, (SHORT)y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } void init_window() { system("mode con lines=60 cols=188"); ShowWindow(hwnd, SW_MAXIMIZE); DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(GetConsoleWindow()); } bool array_contains(int arr[], int val, int len) { for(int i = 0; i < len; i++) { if(arr[i] == val) return true; } return false; } int rand_alive_player(Role exclude_role = (Role)-1) { int target = -1; while(true) { target = rand() % player_count; if(players[target].status == ALIVE && players[target].role != exclude_role) { break; } } return target; } void init_players() { if(player_count == 12) wolf_count = 4; else if(player_count >= 10) wolf_count = 3; else wolf_count = 2; for(int i = 0; i < player_count; i++) { players[i].status = ALIVE; players[i].known = false; players[i].killer = INT_MAX; players[i].role = (Role)-1; } int wolf_idx = 0; while(wolf_idx < wolf_count) { int seat = rand() % player_count; if(!array_contains(wolf_seats, seat, 4)) { wolf_seats[wolf_idx++] = seat; players[seat].role = WEREWOLF; } Sleep(rand() % 80 + 100); } if(player_count == 12) { do { guard_seat = rand() % player_count; } while(players[guard_seat].role != (Role)-1); players[guard_seat].role = GUARD; } do { witch_seat = rand() % player_count; } while(players[witch_seat].role != (Role)-1); players[witch_seat].role = WITCH; if(player_count >= 10) { do { hunter_seat = rand() % player_count; } while(players[hunter_seat].role != (Role)-1); players[hunter_seat].role = HUNTER; } do { seer_seat = rand() % player_count; } while(players[seer_seat].role != (Role)-1); players[seer_seat].role = SEER; for(int i = 0; i < player_count; i++) { if(players[i].role == (Role)-1) { players[i].role = VILLAGER; } } my_seat = rand() % player_count; players[my_seat].known = true; if(players[my_seat].role == WEREWOLF) { for(int i = 0; i < wolf_count; i++) { players[wolf_seats[i]].known = true; } } } void print_ui() { gotoxy(0, 0); set_color(WHITE); cout << "作者:chenzhan"; gotoxy(90, 0); if(current_phase == NIGHT) set_color(RED); else set_color(BLUE); printf("第%d天 | %s\n", day, current_phase == NIGHT ? "黑夜" : "白天"); gotoxy(0, 3); set_color(BLUE); cout << " 我的号位:" << my_seat + 1; for(int i = 0; i < player_count; i++) { gotoxy(i * 8 + 1, 4); if(i == police_seat) set_color(YELLOW); else set_color(BLUE); cout << i + 1 << "号位"; gotoxy(i * 8 + 1, 5); if(players[i].status == DEAD) { set_color(RED); cout << "死 亡"; } else { set_color(GREEN); cout << "存 活"; } gotoxy(i * 8 + 1, 6); set_color(BLUE); if(players[i].known) { switch(players[i].role) { case WITCH: cout << "女 巫"; break; case SEER: cout << "预言家"; break; case VILLAGER: cout << "村 民"; break; case WEREWOLF: cout << "狼 人"; break; case HUNTER: cout << "猎 人"; break; case GUARD: cout << "守 卫"; break; default: cout << "好人"; } } else { cout << "未知"; } } set_color(WHITE); } int check_game_end() { int dead_wolf = 0, dead_villager = 0, dead_god = 0; int alive_wolf = 0, alive_good = 0; for(int i = 0; i < player_count; i++) { if(players[i].status == DEAD) { if(players[i].role == WEREWOLF) dead_wolf++; else if(players[i].role == VILLAGER) dead_villager++; else dead_god++; } else { if(players[i].role == WEREWOLF) alive_wolf++; else alive_good++; } } if(alive_wolf == 0) return 1; int god_total = (witch_seat != -1) + (seer_seat != -1) + (hunter_seat != -1) + (guard_seat != -1); int villager_total = player_count - god_total - wolf_count; if(dead_god >= god_total || dead_villager >= villager_total) return 2; if(alive_wolf == alive_good) return 3; return 0; } void pre_game() { srand(time(NULL)); init_window(); set_color(GREEN); cout << "欢迎来到狼人杀游戏\t\t\t注:10/11/12人局开设猎人 12人局开设守卫/警长\n"; cout << "----------------------------------------\n"; cout << "请输入玩家人数(8-12人): "; cin >> player_count; while(player_count < 8 || player_count > 12) { cout << "人数不符合要求,请重新输入(8-12): "; cin >> player_count; } system("cls"); cout << "初始化身份中,请稍等."; for(int i = 0; i < 6; i++) { for(int j = 0; j < 12; j++) { cout << "."; Sleep(50); } cout << "\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b"; } system("cls"); init_players(); cout << "我的号位: " << my_seat + 1 << endl; cout << "我的身份: "; switch(players[my_seat].role) { case WITCH: cout << "女巫\n"; break; case SEER: cout << "预言家\n"; break; case VILLAGER: cout << "村民\n"; break; case WEREWOLF: cout << "狼人\n"; break; case HUNTER: cout << "猎人\n"; break; case GUARD: cout << "守卫\n"; break; } current_phase = DAY; day = 1; system("pause"); system("cls"); cout << "游戏加载中."; int load_times = rand() % 3 + 2; for(int i = 0; i < load_times; i++) { for(int j = 0; j < 6; j++) { cout << "."; Sleep(rand() % 100 + 150); } cout << "\b\b\b\b\b\b \b\b\b\b\b\b"; } system("cls"); print_ui(); } void guard_action() { set_color(BLUE); cout << "守卫~请睁眼~~\n"; Sleep(1500); guard_protect = -1; if(players[my_seat].role == GUARD && players[my_seat].status == ALIVE) { cout << "你要守护的是? (输入座位号1-" << player_count << "): "; cin >> guard_protect; guard_protect--; while(guard_protect < 0 || guard_protect >= player_count || players[guard_protect].status == DEAD) { cout << "输入无效,请重新输入存活玩家的座位号: "; cin >> guard_protect; guard_protect--; } cout << "你今晚要守护的是" << guard_protect + 1 << "号\n"; } else if(guard_seat != -1 && players[guard_seat].status == ALIVE) { guard_protect = rand_alive_player(); } Sleep(2000); cout << "守卫请闭眼\n\n"; } int wolf_action() { set_color(RED); cout << "狼人~请睁眼~~~\n"; Sleep(1500); int kill_target = -1; if(players[my_seat].role == WEREWOLF && players[my_seat].status == ALIVE) { cout << "你们今晚要杀谁?(输入座位号1-" << player_count << "): "; cin >> kill_target; kill_target--; while(kill_target < 0 || kill_target >= player_count || players[kill_target].status == DEAD || players[kill_target].role == WEREWOLF) { cout << "输入无效,请重新输入非狼人存活玩家的座位号: "; cin >> kill_target; kill_target--; } cout << "你们今晚要杀的是" << kill_target + 1 << "号\n"; } else { kill_target = rand_alive_player(WEREWOLF); } Sleep(3500); cout << "狼人请~闭眼~~\n\n"; return kill_target; } void witch_action(int wolf_kill) { set_color(PURPLE); cout << "女巫~~请睁眼~~\n"; Sleep(2000); heal_target = -1; poison_target = -1; if(players[my_seat].role == WITCH && players[my_seat].status == ALIVE) { if(!witch_healed) { cout << "今晚" << wolf_kill + 1 << "号被狼人杀死,你想用解药吗?(1=想 / 2=不想): "; int choice; cin >> choice; while(choice != 1 && choice != 2) { cout << "输入无效,请输入1或2: "; cin >> choice; } if(choice == 1) { heal_target = wolf_kill; witch_healed = true; cout << "已解救" << wolf_kill + 1 << "号\n"; } } else { cout << "你已经用过解药\n"; Sleep(1500); } Sleep(1500); if(!witch_poisoned) { cout << "你想用毒药吗?(1=想 / 2=不想): "; int choice; cin >> choice; while(choice != 1 && choice != 2) { cout << "输入无效,请输入1或2: "; cin >> choice; } if(choice == 1) { cout << "你想毒谁?(输入座位号1-" << player_count << "): "; cin >> poison_target; poison_target--; while(poison_target < 0 || poison_target >= player_count || players[poison_target].status == DEAD) { cout << "输入无效,请重新输入存活玩家的座位号: "; cin >> poison_target; poison_target--; } witch_poisoned = true; cout << "已毒死" << poison_target + 1 << "号\n"; } } else { cout << "你已经用过毒药\n"; Sleep(1500); } } else if(players[witch_seat].status == ALIVE) { if(!witch_healed && rand() % 10 == 0) { heal_target = wolf_kill; witch_healed = true; } if(!witch_poisoned && rand() % 4 == 0) { poison_target = rand_alive_player(); witch_poisoned = true; } } Sleep(1500); cout << "女巫~请闭眼~~\n\n"; } void seer_action() { set_color(GREEN); cout << "预言家~请睁眼~~\n"; Sleep(2000); seer_check_target = -1; if(players[my_seat].role == SEER && players[my_seat].status == ALIVE) { cout << "你想查验谁的身份?(输入座位号1-" << player_count << "): "; int check; cin >> check; check--; while(check < 0 || check >= player_count || players[check].status == DEAD) { cout << "输入无效,请重新输入存活玩家的座位号: "; cin >> check; check--; } Sleep(2000); cout << check + 1 << "号的身份是——"; Sleep(1000); if(players[check].role == WEREWOLF) { cout << "狼人\n"; players[check].known = true; } else { cout << "好人\n"; players[check].known = true; } } else if(players[seer_seat].status == ALIVE) { seer_check_target = rand_alive_player(); if(players[seer_check_target].role == WEREWOLF) { seer_check_target = seer_check_target; } } Sleep(1000); cout << "预言家~~请闭眼~~\n\n"; } void night_phase() { current_phase = NIGHT; system("cls"); print_ui(); gotoxy(0, 7); cout << "----------------------------------------\n"; set_color(WHITE); cout << "天黑~请闭眼~~~\n"; Sleep(1500); if(player_count == 12) { guard_action(); Sleep(2000); } int wolf_kill = wolf_action(); Sleep(3500); witch_action(wolf_kill); Sleep(3000); seer_action(); Sleep(2000); current_phase = DAY; } void hunter_skill(int dead_seat) { if(players[dead_seat].role != HUNTER || players[dead_seat].status == ALIVE) return; set_color(RED); cout << dead_seat + 1 << "号是猎人,触发技能!\n"; Sleep(1000); int take_seat = -1; if(dead_seat == my_seat) { cout << "你想带走几号?(输入座位号1-" << player_count << "): "; cin >> take_seat; take_seat--; while(take_seat < 0 || take_seat >= player_count || players[take_seat].status == DEAD) { cout << "输入无效,请重新输入存活玩家的座位号: "; cin >> take_seat; take_seat--; } } else { take_seat = rand_alive_player(); } Sleep(2000); cout << "猎人选择带走" << take_seat + 1 << "号\n"; players[take_seat].status = DEAD; players[take_seat].killer = HUNTER; Sleep(3000); } void announce_night_result() { system("cls"); print_ui(); gotoxy(0, 7); cout << "----------------------------------------\n"; Sleep(3000); set_color(WHITE); cout << "天亮了\n"; Sleep(2000); vector<int> dead_players; if(wolf_kill != -1 && wolf_kill != guard_protect && wolf_kill != heal_target) { players[wolf_kill].status = DEAD; players[wolf_kill].killer = WEREWOLF; dead_players.push_back(wolf_kill); } if(poison_target != -1) { players[poison_target].status = DEAD; players[poison_target].killer = WITCH; dead_players.push_back(poison_target); } cout << "昨晚"; if(dead_players.empty()) { cout << "是平安夜\n"; } else { for(int i = 0; i < dead_players.size(); i++) { if(i > 0) cout << "和"; cout << dead_players[i] + 1 << "号"; if(dead_players[i] == police_seat) { cout << "(警长)"; } } cout << "死了\n"; for(int seat : dead_players) { hunter_skill(seat); } } Sleep(3000); } void elect_police() { system("cls"); print_ui(); gotoxy(0, 7); cout << "----------------------------------------\n"; set_color(YELLOW); cout << "开始选举警长,不能选举自己!\n"; Sleep(2000); int votes[100] = {0}; int cannot_vote[100] = {-1}; int cannot_len = 0; while(true) { memset(votes, 0, sizeof(votes)); for(int i = 0; i < player_count; i++) { if(players[i].status == DEAD || array_contains(cannot_vote, i, cannot_len)) { continue; } int vote_target = -1; if(i == my_seat) { cout << "你要选举谁当警长?(输入座位号1-" << player_count << "): "; cin >> vote_target; vote_target--; while(vote_target < 0 || vote_target >= player_count || vote_target == i || players[vote_target].status == DEAD || array_contains(cannot_vote, vote_target, cannot_len)) { cout << "输入无效,请重新输入: "; cin >> vote_target; vote_target--; } cout << i + 1 << "号选举" << vote_target + 1 << "号\n"; } else { do { vote_target = rand() % player_count; } while(vote_target == i || players[vote_target].status == DEAD || array_contains(cannot_vote, vote_target, cannot_len)); cout << i + 1 << "号选举" << vote_target + 1 << "号\n"; } votes[vote_target] += (i == police_seat) ? 2 : 1; Sleep(1500); } int max_vote = -1; vector<int> max_seats; for(int i = 0; i < player_count; i++) { if(votes[i] > max_vote) { max_vote = votes[i]; max_seats.clear(); max_seats.push_back(i); } else if(votes[i] == max_vote) { max_seats.push_back(i); } } if(max_seats.size() == 1) { police_seat = max_seats[0]; cout << "恭喜" << police_seat + 1 << "号当选警长!\n"; Sleep(3000); break; } else { cout << "平票:"; cannot_len = 0; for(int seat : max_seats) { cout << seat + 1 << "号 "; cannot_vote[cannot_len++] = seat; } cout << "进入重选,平票者不能参与\n"; Sleep(2000); } } } void police_death() { set_color(YELLOW); cout << "警长死亡,处理警徽!\n"; Sleep(1500); int choice = 1; // 1=撕毁,2=移交 if(police_seat == my_seat) { cout << "你想撕毁警徽还是移交警徽?(1=撕毁 / 2=移交): "; cin >> choice; while(choice != 1 && choice != 2) { cout << "输入无效,请输入1或2: "; cin >> choice; } } else { choice = rand() % 2 + 1; } if(choice == 1) { cout << "警长选择撕毁警徽\n"; police_seat = -1; } else { int new_police = rand_alive_player(); cout << "警长选择把警徽移交给" << new_police + 1 << "号\n"; police_seat = new_police; } Sleep(2000); } int vote_phase() { system("cls"); print_ui(); gotoxy(0, 7); cout << "----------------------------------------\n"; set_color(WHITE); cout << "进入投票环节,选出要放逐的玩家!\n"; Sleep(2000); int votes[100] = {0}; int cannot_vote[100] = {-1}; int cannot_len = 0; while(true) { memset(votes, 0, sizeof(votes)); for(int i = 0; i < player_count; i++) { if(players[i].status == DEAD || array_contains(cannot_vote, i, cannot_len)) { continue; } int vote_target = -1; if(i == my_seat) { cout << "你要投谁出局?(输入座位号1-" << player_count << "): "; cin >> vote_target; vote_target--; while(vote_target < 0 || vote_target >= player_count || players[vote_target].status == DEAD) { cout << "输入无效,请重新输入: "; cin >> vote_target; vote_target--; } cout << i + 1 << "号投了" << vote_target + 1 << "号"; if(i == police_seat) cout << "(警长,2票)"; cout << endl; } else { do { vote_target = rand() % player_count; if(i == seer_seat && seer_check_target != -1 && players[seer_check_target].role == WEREWOLF) { vote_target = seer_check_target; seer_check_target = -1; break; } if(array_contains(wolf_seats, i, wolf_count) && players[vote_target].role == WEREWOLF) { vote_target = -1; continue; } } while(vote_target == -1 || players[vote_target].status == DEAD); cout << i + 1 << "号投了" << vote_target + 1 << "号"; if(i == police_seat) cout << "(警长,2票)"; cout << endl; } votes[vote_target] += (i == police_seat) ? 2 : 1; Sleep(rand() % 1000 + 1000); } int max_vote = -1; vector<int> max_seats; for(int i = 0; i < player_count; i++) { if(votes[i] > max_vote) { max_vote = votes[i]; max_seats.clear(); max_seats.push_back(i); } else if(votes[i] == max_vote) { max_seats.push_back(i); } } if(max_seats.size() == 1) { int out_seat = max_seats[0]; cout << out_seat + 1 << "号获得最高票,出局!\n"; players[out_seat].status = DEAD; players[out_seat].killer = GOOD; Sleep(4000); if(out_seat == police_seat) { police_death(); } hunter_skill(out_seat); return out_seat; } else { cout << "平票:"; cannot_len = 0; for(int seat : max_seats) { cout << seat + 1 << "号 "; cannot_vote[cannot_len++] = seat; } cout << "进入重选!\n"; Sleep(3000); } } } void game_over() { system("cls"); set_color(WHITE); int result = check_game_end(); switch(result) { case 1: cout << "?? 好人阵营胜利!??\n\n"; break; case 2: cout << "?? 狼人阵营胜利!??\n\n"; break; case 3: cout << "?? 本局平局!??\n\n"; break; } cout << "===== 最终结算 =====\n"; for(int i = 0; i < player_count; i++) { cout << i + 1 << "号位: \t"; switch(players[i].role) { case WITCH: cout << "女巫\t"; break; case SEER: cout << "预言家\t"; break; case VILLAGER: cout << "村民\t"; break; case WEREWOLF: cout << "狼人\t"; break; case HUNTER: cout << "猎人\t"; break; case GUARD: cout << "守卫\t"; break; } cout << "最终: "; if(players[i].status == ALIVE) { cout << "存活\n"; } else { switch(players[i].killer) { case WEREWOLF: cout << "被狼人杀死\n"; break; case WITCH: cout << "被女巫毒死\n"; break; case GOOD: cout << "被投票出局\n"; break; case HUNTER: cout << "被猎人带走\n"; break; default: cout << "死亡(未知原因)\n"; } } } set_color(WHITE); } int main() { pre_game(); int round = 0; while(!check_game_end()) { night_phase(); announce_night_result(); if(check_game_end()) break; system("cls"); print_ui(); if(round == 0 && player_count == 12 && police_seat == -1) { elect_police(); system("cls"); print_ui(); } vote_phase(); if(check_game_end()) break; day++; round++; system("cls"); print_ui(); } game_over(); system("pause"); return 0; }
- 1
