using namespace std;
#include <iostream>
#include <time.h>
#include <string.h>
#include <stdio.h>
int n,j;//总人数 ,cunzhang
int red,black;//红黑人数
int e=0,e0;//环境,红黑夜
int cunzhang=1,count=0;//村长 ,计数器
///
struct Player{
char name[20];
int ID;//0,1==red,black
bool cunzhang;//1==yes
double ticket_001;//被投的
double ticket_002;//拥有的
double ticket_003;//被投的
bool life;//0,1==die ,alive
int number;//i
char words[50];
int eye;//1==open ,0==closed
void (*chushihua)(void);
//char (*setname)(char *name);
//int (*get_ID)(int ID);
void (*vote_cunzhang)(void);
void (*vote_cunzhang2)(void);
void (*vote)(void);
//bool (*live_die)(*Player a)
void (*set_environment1)(void);
void (*set_environment2)(void);
void (*saysomething)(void);
void (*saysomethingelse)(void);
void (*show1)(void);
void(*show2)(void);
void (*openyoureyes)(void);
}player[100];
void chushihua(void)
{
for(int i=0;i<n;i++)
{
cout<<"请输入"<<i<<"玩家的游戏昵称:"<<endl;
cin>>player[i].name;
player[i].life=1;
player[i].number=i;
player[i].cunzhang=0;
player[i].ticket_001=0;
player[i].ticket_002=1;
player[i].ticket_003=0;
if(red==0) player[i].ID=1;
else if(black==0) player[i].ID=0;
else {
player[i].ID=rand()%2;
if(player[i].ID==0) red--;
else black--;
}//ID
}
}
/*char setname(char *name)
{
cin>>name;
}*/
//name
/*int get_ID(int ID)
{
if(::red==0) ID=1;
if(::black==0) ID=0;
ID=rand()%2;
}*///ID
void vote_cunzhang(void)
{
for(int i=0;i<n;i++)
{
if(player[i].life==0) continue;//die
int t;
cout<<"请玩家"<<player[i].name<<"选择心仪的村长:"<<endl;
cin>>t;
player[t].ticket_001+=1;
}
int temp;
for(int i=0;i<n;i++)
{
if(i==0) {
temp=player[0].ticket_001;
j=0;
}
else if(player[i].ticket_001>temp) {
temp=player[i].ticket_001;
j=i;
}
}//找cunzhang
cout<<player[j].name<<"玩家为村长!"<<endl;
player[j].cunzhang=1;
if(n%2==0) player[j].ticket_002=1.5;
else player[j].ticket_002=0.5;//cunzhang的初始化
for(int i=0;i<n;i++) player[i].ticket_001=0;//置零
}
void saysomething(void)
{
for(int i=0;i<n;i++)
{
if(player[i].life==0) continue;//die , alive
cout<<"请玩家"<<player[i].name<<"说一说自己的想法";
cin>>player[i].words;
}//say you want
}
void show1(void)
{
cout<<"========================="<<endl;
for(int i=0;i<n;i++)
{
if(player[i].life==0) break;//die, alive
cout<<player[i].name<<": "<<player[i].words<<endl;
}
cout<<"========================="<<endl;
}//show it
void vote(void)
{
for(int i=0;i<n;i++)
{
if(player[i].life==0) continue;
else {
cout<<"请"<<player[i].name<<"vote:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=player[i].ticket_002;
}
}
int temp=0,k;
for(int i=0;i<n;i++)
{
if(player[i].life==0) continue;
if(player[i].ticket_003>=temp) {
temp=player[i].ticket_003;
k=i;
}
}
player[k].life=0;
cout<<"======================================"<<endl;
cout<<player[k].name<<"玩家已死亡!"<<endl;
if(player[k].ID==0) {
red--;
cout<<"身份是red"<<endl;
}
else {
cout<<"身份是black"<<endl;
black--;
}
cout<<"======================================"<<endl;
if(player[k].cunzhang)
{
cout<<"村长死亡,请重新选举!"<<endl;
cunzhang=0;
}
}
void saysomethingelse(void)
{
cout<<"请村长决定发言顺序:(请输入 right 或 left)"<<endl;
char a[6];
cin>>a;
if(strcmp(a,"left")==0)
{
int temp1=n,temp2=j;
while(player[temp2--].life==0)
{
if(temp2==-1) temp2=n-1;
}
while(temp1--)
{
temp2++;
if(player[temp2].life)
{
cout<<"请玩家"<<player[temp2].name<<"陈述自己的观点:"<<endl;
cin>>player[temp2].words;
}
if(temp2==n-1) temp2=-1;
}
/*if(j==0)
{
cout<<"请"<<player[n-1].name<<"投一票你怀疑的对象:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=1;
for(int i=0;i<n-1;i++)
{
cout<<"请"<<player[i].name<<"投一票你怀疑的对象:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=1;
}
}
for(int i=j-1;i<::n;i++)
{
cout<<"请"<<player[i].name<<"投一票你怀疑的对象:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=1;
}
for(int i=0;i<j-1;i++)
{
cout<<"请"<<player[i].name<<"投一票你怀疑的对象:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=1;
}
}
else
{
if(j==n-1)
{
cout<<"请"<<player[0].name<<"投一票你怀疑的对象:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=1;
for(int i=n-2;i>=0;i--)
{
cout<<"请"<<player[i].name<<"投一票你怀疑的对象:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=1;
}
}
else {
for(int i=j+1;i>=0;i--)
{
cout<<"请"<<player[i].name<<"投一票你怀疑的对象:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=1;
}
for(int i=n-1;i>j+1;i--)
{
cout<<"请"<<player[i].name<<"投一票你怀疑的对象:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=1;
}
}*/
}
else{
int temp1=n,temp2=j;
while(player[temp2++].life==0)
{
if(temp2==n) temp2=0;
}
while(temp1--)
{
temp2--;
if(player[temp2].life)
{
cout<<"请玩家"<<player[temp2].name<<"陈述自己的观点:"<<endl;
cin>>player[temp2].words;
}
if(temp2==0) temp2=n;
}
}
/* for(int i=0;i<::n;i++)
{
cout<<"请"<<player[i].name<<"投一票你怀疑的对象:"<<endl;
int t;
cin>>t;
player[t].ticket_003+=1;
}*/
}
void openyoureyes(void)
{
count=0;
for(int i=0;i<n;i++)
{
if(player[i].life==0) continue;
cout<<player[i].name<<"是否选择睁眼?(1 or 0)"<<endl;
int t;
cin>>t;
if(t==0) continue;
else{
if(e0==0&&player[i].ID==0)
count++;
else if(e0==1&&player[i].ID==1)
count++;
}
}
if(count%2==0) player[j].ticket_002=1;
else if(n%2==0) player[j].ticket_002=1.5;
else player[j].ticket_002=0.5;
}
void vote_cunzhang2(void)
{
for(int i=0;i<n;i++)
{
if(player[i].life==0) continue;//die
int t;
cout<<"请玩家"<<player[i].name<<"选择心仪的村长:"<<endl;
cin>>t;
player[t].ticket_001+=1;
}
int temp;
for(int i=0;i<n;i++)
{
if(i==0) {
temp=player[0].ticket_001;
j=0;
}
else if(player[i].ticket_001>temp) {
temp=player[i].ticket_001;
j=i;
}
}//找cunzhang
cout<<player[j].name<<"玩家为村长!"<<endl;
player[j].cunzhang=1;
if(count%2==0) player[j].ticket_002=1;
else if(n%2==0) player[j].ticket_002=1.5;
else player[j].ticket_002=0.5;//cunzhang的初始化
for(int i=0;i<n;i++) player[i].ticket_001=0;//置零
}
void set_environment1(void)
{
cout<<"========================================"<<endl;
if(e0=rand()%2==0) cout<<"进入红夜"<<endl;
else cout<<"进入黑夜"<<endl;
cout<<"========================================"<<endl;
}
void set_environment2(void)
{
cout<<"========================================"<<endl;
cout<<"进入 白天!"<<endl;
cout<<"========================================"<<endl;
}
void set_red_black(int n)
{
if(n%2==0)
{
red=n/2;
black=n/2;
}
else{
red=n/2+1;
black=n/2;
}
}
/
int main(void)
{
int t=0;
srand((unsigned)time(NULL));
cout<<"请输入参加游戏的玩家人数:"<<endl;
cin>>::n;
set_red_black(n);
chushihua();
set_red_black(n);
vote_cunzhang();
while((red+black)!=1)
{
set_environment1();
if(t++==0){
saysomething();
}
else{
saysomethingelse();
}
openyoureyes();
vote();
if(red&&black==0)
{
cout<<"red win!"<<endl;
break;
}
else if(red==0&&black)
{
cout<<"balck win!"<<endl;
break;
}
if(cunzhang==0)
{
vote_cunzhang2();
cunzhang=1;
}
set_environment2();
}
return 0;
}
红黑夜法官 c++(缺陷版)
最新推荐文章于 2024-08-22 23:21:41 发布