资深光能
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#include<sstream>
using namespace std;
bool f,fff;
string pipei[4]={"匹配中","匹配中.","匹配中..","匹配中..."};
int lieren[4],nvwu[4],yuyanjia[3],langren[4],shouwei[5];//猎人4个 女巫4个 预言家3个 狼人4个 守卫5个
int a[35];//0 平民 1 狼人 2 猎人 3 女巫 4 预言家 5 守卫
int shengming[35];//30个人的生命值
string js;
int nn=1;
int vote[35];//30个人投票票数
bool fvote[35];//30个人是否已经投票
int win,lose;//赢的次数 输得次数
double hsl;//获胜率
bool fbreak;
string wk="--------------------------------------------------------------------------------";
void jingyong(){
HWND hwnd=GetConsoleWindow();
HMENU hmenu=GetSystemMenu(hwnd,false);
RemoveMenu(hmenu,SC_CLOSE,MF_BYCOMMAND);
LONG style=GetWindowLong(hwnd,GWL_STYLE);
style&=~(WS_MINIMIZEBOX);
SetWindowLong(hwnd,GWL_STYLE,style);
SetWindowPos(hwnd,HWND_TOP,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
ShowWindow(hwnd,SW_SHOWNORMAL);
DestroyMenu(hmenu);
ReleaseDC(hwnd,NULL);
}
bool jiance_langrensha(){//判断是否有存档
std::ifstream ifs ("langrensha.txt");
std::ifstream ifss ("weikuan.txt");
return ifs.is_open()&&ifss.is_open();;
}
void color(int corcorcor){//颜色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),corcorcor);
}
void cls(){//清屏
system("cls");
}
int changdu_l(string a){//提取字符长度
int ans=0,n=0,m=0;
while(a[n]!='\0'){
if((a[n]&0x80)!=0){
m+=2;
n+=2;
ans+=2;
}else{
n++;
ans++;
}
}
return ans;
}
void line(string a){//中对齐 该函数如需借鉴请先联系沙兴安
int l,w;
ifstream fw;
fw.open("weikuan.txt");
fw>>wk;
fw.close();
w=wk.size();
if(a!="狼") l=changdu_l(a);//提取字符长度
else l=6;
for(int i=0;i<(w-l)/2;i++){
cout<<" ";
}
cout<<a;
return ;
}
void fenpei(){//分配角色
Sleep(rand()%5000+1500);
while(!(langren[0]&&langren[1]&&langren[2]&&langren[3])){//狼人 分配
int n=rand()%30+1;
if(!langren[0]){
a[n]=1;
langren[0]=n;
}else if(!langren[1]&&!a[n]){
a[n]=1;
langren[1]=n;
}else if(!langren[2]&&!a[n]){
a[n]=1;
langren[2]=n;
}else if(!langren[3]&&!a[n]){
a[n]=1;
langren[3]=n;
}
}
cout<<endl;
line("狼人 匹配完成");
Sleep(rand()%5000+1500);
while(!(lieren[0]&&lieren[1]&&lieren[2]&&lieren[3])){//猎人 分配
int n=rand()%30+1;
if(!lieren[0]&&!a[n]){
a[n]=2;
lieren[0]=n;
}else if(!lieren[1]&&!a[n]){
a[n]=2;
lieren[1]=n;
}else if(!lieren[2]&&!a[n]){
a[n]=2;
lieren[2]=n;
}else if(!lieren[3]&&!a[n]){
a[n]=2;
lieren[3]=n;
}
}
cout<<endl;
line("猎人 匹配完成");
Sleep(rand()%5000+1500);
while(!(nvwu[0]&&nvwu[1]&&nvwu[2]&&nvwu[3])){//女巫 分配
int n=rand()%30+1;
if(!nvwu[0]&&!a[n]){
a[n]=3;
nvwu[0]=n;
}else if(!nvwu[1]&&!a[n]){
a[n]=3;
nvwu[1]=n;
}else if(!nvwu[2]&&!a[n]){
a[n]=3;
nvwu[2]=n;
}else if(!nvwu[3]&&!a[n]){
a[n]=3;
nvwu[3]=n;
}
}
cout<<endl;
line("女巫 匹配完成");
Sleep(rand()%5000+1500);
while(!(yuyanjia[0]&&yuyanjia[1]&&yuyanjia[2])){//预言家 分配
int n=rand()%30+1;
if(!yuyanjia[0]&&!a[n]){
a[n]=4;
yuyanjia[0]=n;
}else if(!yuyanjia[1]&&!a[n]){
a[n]=4;
yuyanjia[1]=n;
}else if(!yuyanjia[2]&&!a[n]){
a[n]=4;
yuyanjia[2]=n;
}
}
cout<<endl;
line("预言家 匹配完成");
Sleep(rand()%5000+1500);
while(!(shouwei[0]&&shouwei[1]&&shouwei[2]&&shouwei[3]&&shouwei[4])){//守卫 分配
int n=rand()%30+1;
if(!shouwei[0]&&!a[n]){
a[n]=5;
shouwei[0]=n;
}else if(!shouwei[1]&&!a[n]){
a[n]=5;
shouwei[1]=n;
}else if(!shouwei[2]&&!a[n]){
a[n]=5;
shouwei[2]=n;
}else if(!shouwei[3]&&!a[n]){
a[n]=5;
shouwei[3]=n;
}else if(!shouwei[4]&&!a[n]
){
a[n]=5;
shouwei[4]=n;
}
}
cout<<endl;
line("守卫 匹配完成");
Sleep(rand()%5000+1500);
cout<<endl;
line("平民 匹配完成");
Sleep(rand()%5000+1500);
}
void cover_langrensha(){
line("狼人杀");
ofstream fw;
fw<<wk;
fw.close();
string o=wk;
cout<<endl<<o<<endl;
if(!shengming[nn]){
line("你已死亡");
cout<<endl;
}else if(f){
string tmp;
if(a[nn]==0){
tmp="平民";
}else if(a[nn]==1){
tmp="狼人";
}else if(a[nn]==2){
tmp="猎人";
}else if(a[nn]==3){
tmp="女巫";
}else if(a[nn]==4){
tmp="预言家";
}else if(a[nn]==5){
tmp="守卫";
}
line("你是 "+js+" 号 "+tmp);
cout<<endl;
}
}
bool check(){
for(int i=1;i<=30;i++){
if(a[i]!=-1){
return 0;
}
}
return 1;
}
int check_jieshu(){
int bad=0,good=0;
for(int i=1;i<=30;i++){
if(shengming[i]){
if(a[i]==1){
bad++;
}else{
good++;
}
}
}
if(bad>=good){
return 1;
}else if(bad==0){
return 2;
}else{
return 0;
}
}
void kaishiyouxi(){
memset(a,0,sizeof(a));
memset(lieren,0,sizeof(lieren));
memset(nvwu,0,sizeof(nvwu));
memset(yuyanjia,0,sizeof(yuyanjia));
memset(langren,0,sizeof(langren));
memset(shouwei,0,sizeof(shouwei));
Sleep(1000);
line("开始匹配人数");
Sleep(1000);
for(int i=1;i<=3;i++){
cls();
cover_langrensha();
line(pipei[0]);
Sleep(500);
cls();
cover_langrensha();
line(pipei[1]);
Sleep(500);
cls();
cover_langrensha();
line(pipei[2]);
Sleep(500);
cls();
cover_langrensha();
line(pipei[3]);
Sleep(500);
}
cls();
cover_langrensha();
line("匹配成功!");
Sleep(3000);
cls();
cover_langrensha();
line("正在匹配其他人的角色,请耐心等待");
fenpei();
cls();
cover_langrensha();
int n=rand()%30+1;
char o1=n/10+'0',o2=n%10+'0';
nn=n;
js="";
js+=o1;
js+=o2;
line("你是 "+js+" 号!");
cout<<endl;
Sleep(1000);
if(a[nn]==0){
line("平民!");
}else if(a[nn]==1){
line("狼人!");
}else if(a[nn]==2){
line("猎人!");
}else if(a[nn]==3){
line("女巫!");
}else if(a[nn]==4){
line("预言家!");
}else{
line("守卫!");
}
f=1;
Sleep(2000);
string h="";
while(!check()){
cls();
cover_langrensha();
memset(vote,0,sizeof(vote));
memset(fvote,0,sizeof(fvote));
system("color 0C");
Sleep(3000);
line("天黑请闭眼......");
cout<<endl;
Sleep(3000);
cls();
cover_langrensha();
for(int i=1;i<=30;i++){
n=rand()%30+1;
o1=n/10+'0';
o2=n%10+'0';
h="";
h+=o1;
h+=o2;
int b;
if(i==nn&&a[i]!=0&&shengming[i]){
string ttt="剩余人数:";
for(int j=1;j<=30;j++){
if(shengming[j]){
o1=j/10+'0';
o2=j%10+'0';
ttt+=o1;
ttt+=o2;
ttt+=" ";
}
}
line(ttt);
cout<<endl;
if(a[i]==1||a[i]==2){
if(a[i]==1){
ttt="你还活着的狼人队友是:";
for(int j=1;j<=30;j++){
if(a[j]==1&&shengming[j]){
o1=j/10+'0';
o2=j%10+'0';
ttt+=o1;
ttt+=o2;
ttt+=" ";
}
}
line(ttt);
cout<<endl;
}
line("请选择你要杀的人");
cin>>b;
while(b<1||b>30||b==n||!shengming[b]){
cout<<endl;
line("输入错误");
cin>>b;
}
if(shengming[b]){
shengming[b]--;
}
h="";
o1=b/10+'0';
o2=b%10+'0';
h+=o1;
h+=o2;
line("你杀死了 "+h+" 号");
cout<<endl;
}else if(a[i]==3){
line("你要选择:1. 毒药 2. 解药");
char c;
c=getch();
cout<<endl;
if(c=='2'){
line("谁?");
cin>>n;
while(n==nn){
cout<<endl;
line("输入错误");
cin>>n;
}
o1=n/10+'0';
o2=n%10+'0';
h="";
h+=o1;
h+=o2;
if(!shengming[n]){
line("你救活了 "+h+" 号");
}else{
line("你给 "+h+" 号增加了1条生命");
}
shengming[n]++;
}else{
line("谁?");
cin>>n;
while(!shengming[n]||n==nn){
cout<<endl;
line("输入错误");
cin>>n;
}
o1=n/10+'0';
o2=n%10+'0';
h="";
h+=o1;
h+=o2;
line("你把一瓶毒药泼在了 "+h+" 号身上 TA死了");
shengming[n]=0;
}
}else if(a[i]==4){
line("你要预言谁?");
cin>>n;
if(a[n]==0){
line("悄悄告诉你,TA是平民");
}else if(a[n]==1){
line("悄悄告诉你,TA是狼人");
}else if(a[n]==2){
line("悄悄告诉你,TA是猎人");
}else if(a[n]==3){
line("悄悄告诉你,TA是女巫");
}else if(a[n]==4){
line("悄悄告诉你,TA是预言家");
}else if(a[n]==5){
line("悄悄告诉你,TA是守卫");
}
cout<<endl;
}else{
line("你要守护谁?");
cin>>n;
o1=n/10+'0';
o2=n%10+'0';
h="";
h+=o1;
h+=o2;
line("你守护了 "+h+" 号");
shengming[n]++;
}
Sleep(3000);
cls();
cover_langrensha();
}else if(shengming[i]){
if(a[i]==1){
int cnt=0;
while(!shengming[n]||n==i||a[n]==1){
n=rand()%30+1;
o1=n/10+'0';
o2=n%10+'0';
h="";
h+=o1;
h+=o2;
if(cnt>=100){
break;
}
}
line("狼人杀死了 "+h+" 号");
shengming[n]--;
cout<<endl;
}else if(a[i]==2){
int cnt=0;
while(!shengming[n]||n==i){
n=rand()%30+1;
o1=n/10+'0';
o2=n%10+'0';
h="";
h+=o1;
h+=o2;
if(cnt>=100){
break;
}
}
line("猎人杀死了 "+h+" 号");
shengming[n]--;
cout<<endl;
}else if(a[i]==3){
if(rand()%2==1){
while(n==i){
n=rand()%30+1;
}
if(!shengming[n]){
line("女巫救活了 "+h+" 号");
}else{
line("女巫给 "+h+" 号增加了1条生命");
}
shengming[n]++;
}else{
int cnt=0;
while(!shengming[n]||i==n){
n=rand()%30+1;
o1=n/10+'0';
o2=n%10+'0';
h="";
h+=o1;
h+=o2;
cnt++;
if(cnt>=100){
break;
}
}
line("女巫把一瓶毒药泼在了 "+h+" 号身上 TA死了");
shengming[n]=0;
}
cout<<endl;
}else if(a[i]==4){
line("预言家开始预言......");
n=rand()%30+1;
while(!shengming[n]||i==n){
n=rand()%30+1;
}
if(a[n]==1){
vote[n]++;
fvote[i]=1;
}
}else if(a[i]==5){
n=rand()%30+1;
while(n==i){
n=rand()%30+1;
}
o1=n/10+'0';
o2=n%10+'0';
h="";
h+=o1;
h+=o2;
line("守卫守护了 "+h+" 号");
shengming[n]++;
}
if(a[i]>0){
Sleep(3000);
cls();
cover_langrensha();
}
}
}
cls();
color(240);
cls();
cover_langrensha();
Sleep(3000);
line("天亮了");
Sleep(3000);
cout<<endl;
if(check_jieshu()==1){
line("狼人胜利!");
Sleep(3000);
cout<<endl;
if(a[nn]==1){
line("你赢了");
win++;
}else{
line("你输了");
lose++;
}
Sleep(3000);
break;
}else if(check_jieshu()==2){
line("好人胜利!");
Sleep(3000);
if(a[nn]==1){
line("你输了");
lose++;
}else{
line("你赢了");
win++;
}
Sleep(3000);
break;
}
string ttt="存活人数:";
for(int i=1;i<=30;i++){
if(shengming[i]){
o1=i/10+'0';
o2=i%10+'0';
ttt+=o1;
ttt+=o2;
ttt+=" ";
}
}
line(ttt);
Sleep(3000);
cout<<endl;
line("开始投票......");
Sleep(3000);
cout<<endl;
if(shengming[nn]){
line("你要投谁?");
cin>>n;
cout<<endl;
while(!shengming[n]){
line("TA已死亡");
cin>>n;
cout<<endl;
}
vote[n]++;
}
for(int i=1;i<=30;i++){
if(i!=nn&&shengming[i]&&!fvote[i]){
n=rand()%30+1;
while(!shengming[n]||i==n){
n=rand()%30+1;
}
vote[n]++;
fvote[i]=1;
}
}
line("投票情况如下:");
cout<<endl;
int mx=0,pos;
for(int i=1;i<=30;i++){
if(shengming[i]){
if(vote[i]>mx){
mx=vote[i];
pos=i;
}
o1=i/10+'0';
o2=i%10+'0';
char o3=vote[i]/10+'0';
char o4=vote[i]%10+'0';
string t="";
t+=o3;
t+=o4;
h="";
h+=o1;
h+=o2;
Sleep(1500);
line(h+"号:"+t+"票");
cout<<endl;
}
}
Sleep(3000);
int fffff=0;
for(int i=1;i<=30;i++){
if(mx==vote[i]){
fffff++;
}
}
if(fffff>1){
line("没有人被投出去");
Sleep(3000);
continue;
}
o1=pos/10+'0';
o2=pos%10+'0';
h="";
h+=o1;
h+=o2;
line(h+" 号被投了出去");
cout<<endl;
shengming[pos]=0;
Sleep(3000);
if(check_jieshu()==1){
line("狼人胜利!");
Sleep(3000);
cout<<endl;
if(a[nn]==1){
line("你赢了");
win++;
}else{
line("你输了");
lose++;
}
break;
}else if(check_jieshu()==2){
line("好人胜利!");
Sleep(3000);
cout<<endl;
if(a[nn]==1){
line("你输了");
lose++;
}else{
line("你赢了");
win++;
}
break;
}
}
Sleep(3000);
cls();
}
void youxiguize(){//游戏规则
char a,b;
while(a!='0'){
cls();
cover_langrensha();
line("1. 平民");
cout<<endl;
line("2. 狼人");
cout<<endl;
line("3. 猎人");
cout<<endl;
line("4. 女巫");
cout<<endl;
line("5. 预言家");
cout<<endl;
line("6. 守卫");
cout<<endl;
line("0. 退出");
a=getch();
cls();
cover_langrensha();
if(a=='1'){
line("只有投票权,暂时没有其他技能");
}else if(a=='2'){
line("可以随机使一个人扣 1 点血");
}else if(a=='3'){
line("可以随机使一个人扣 1 点血");
}else if(a=='4'){
line("可以使用 毒药 和 解药");
cout<<endl;
Sleep(1000);
line("毒药:使一个人直接死亡");
cout<<endl;
Sleep(1000);
line("解药:使一个人增加 1 点血");
}else if(a=='5'){
line("可以预言一个人");
cout<<endl;
Sleep(1000);
line("如果预言家为人机,预言到狼人,就会在投票时投TA一票,否则随机投票");
}else if(a=='6'){
line("可以守护一个人,使他增加 1 点血");
}else{
break;
}
cout<<endl;
line("请点击任意继续......");
b=getch();
}
}
void guanyuzuozhe(){
line("姓名:葛子豪");
cout<<endl;
Sleep(1000);
line("第1任工作室:合肥师范附小第一战队 副室长");
cout<<endl;
Sleep(1000);
line("第2任工作室:清华内脑科技天团 室长");
cout<<endl;
Sleep(1000);
line("现任工作室:清华内脑科技天团 室长");
cout<<endl;
Sleep(1000);
line("酷丁问答里是资深光能");
cout<<endl;
Sleep(1000);
line("请点击任意继续......");
char a;
a=getch();
}
void emmm___(){
while(1){
memset(shengming,0,sizeof(shengming));
for(int i=1;i<=30;i++){
shengming[i]=1;
}
char a;
cls();
cover_langrensha();
line("1. 开始游戏(共30人)");
cout<<endl;
line("2. 游戏规则");
cout<<endl;
line("3. 关于作者");
cout<<endl;
line("4. 查看胜负次数");
cout<<endl;
line("5. 保存");
cout<<endl;
line("6. 退出");
a=getch();
cls();
if(a=='1'){
break;
}
cover_langrensha();
if(a=='2'){
youxiguize();
}else if(a=='3'){
guanyuzuozhe();
}else if(a=='4'){
ifstream fw;
fw.open("langrensha.txt");
fw>>win>>lose>>hsl;
string wins="",loses="",hsls="";
int p1=win,p2=lose;
double p3=hsl;
while(p1){
char o=p1%10+'0';
wins+=o;
p1/=10;
}
while(p2){
char o=p2%10+'0';
loses+=o;
p2/=10;
}
p3=p3*pow(10,6);
double p4=int(p3);
p4/=(pow(10,6));
ostringstream os;
os<<p4;
istringstream is(os.str());
is>>hsls;
if(wins==""){
wins='0';
}
if(loses==""){
loses='0';
}
line("胜: "+wins+" 局");
cout<<endl;
line("负: "+loses+" 局");
cout<<endl;
line("获胜率: "+hsls+"%");
cout<<endl;
line("请点击任意继续......");
a=getch();
}else if(a=='5'){
ofstream fr;
fr.open("langrensha.txt");
if(win+lose>0) hsl=win*1.0/(win+lose)*100;
fr<<win<<lose<<hsl;
fr.close();
line("保存成功!");
Sleep(3000);
}else{
fbreak=1;
break;
}
}
}
void langrensha_MAIN(){//狼人杀 主函数
jingyong();
srand(time(NULL));
for(int i=1;i<=30;i++){//生命值 初始化
shengming[i]=1;
}
system("color 01");
line("狼");
Sleep(1000);
system("color 06");
cout<<"人";
Sleep(1000);
system("color 0C");
cout<<"杀\n";
Sleep(1000);
ifstream fw;
fw.open("weikuan.txt");
fw>>wk;
fw.close();
string o=wk;
cout<<o<<endl;
Sleep(1000);
fbreak=0;
emmm___();
if(fbreak){
return ;
}
while(1){
f=0;
cls();
cover_langrensha();
memset(shengming,0,sizeof(shengming));
for(int i=1;i<=30;i++){
shengming[i]=1;
}
kaishiyouxi();
memset(shengming,0,sizeof(shengming));
for(int i=1;i<=30;i++){
shengming[i]=1;
}
while(1){
cls();
cover_langrensha();
system("color 0C");
line("1. 查看所有人的身份");
Sleep(1000);
cout<<endl;
line("2. 再来一局");
Sleep(1000);
cout<<endl;
line("3. 退出");
char b;
b=getch();
fff=0;
if(b=='1'){
for(int i=1;i<=30;i++){
cout<<endl;
char o1,o2;
string j="";
o1=i/10+'0';
o2=i%10+'0';
j+=o1;
j+=o2;
if(a[i]==0){
j+=":平民";
}else if(a[i]==1){
j+=":狼人";
}else if(a[i]==2){
j+=":猎人";
}else if(a[i]==3){
j+=":女巫";
}else if(a[i]==4){
j+=":预言家";
}else{
j+=":守卫";
}
line(j);
}
cout<<endl;
line("请点击任意继续......");
b=getch();
}else if(b=='3'){
fff=1;
break;
}else{
break;
}
}
if(fff){
emmm___();
if(fbreak){
return ;
}
}
}
}
int main(){
if(!jiance_langrensha()){
color(240);
cls();
cout<<"\n\n\n\n\n\n\n\n\n\n";
line("检测到第一次登录,正在新建存档");
ofstream out1;
ofstream out2;
out1.open("langrensha.txt");
Sleep(3000);
out2.open("weikuan.txt");
cls();
cout<<"\n\n\n\n\n\n\n\n\n\n";
line("开始设置位宽,输入+/-调整位宽,当 '-' 占满一行,按F/f确认");
cout<<"\n";
line("请点击任意继续......");
char a;
a=getch();
while(1){
cls();
cout<<wk;
a=getch();
if(a=='+'){
wk+='-';
}else if(a=='-'){
wk.erase(0,1);
}else if(a=='f'||a=='F'){
break;
}
}
cls();
ofstream fn;
fn.open("weikuan.txt");
fn<<wk;
fn.close();
ofstream fr;
fr.open("langrensha.txt");
fr<<win<<lose<<hsl;
fr.close();
cout<<"\n\n\n\n\n\n\n\n\n\n";
line("设置完成,如果调错可以到weikuan文件里调整");
Sleep(3000);
cls();
cout<<"\n\n\n\n\n\n\n\n\n\n";
line("请重启游戏");
cout<<endl;
line("请点击任意关闭游戏,再手动运行......");
a=getch();
return 0;
}
langrensha_MAIN(); //狼人杀 主函数
line("正在为你保存......");
ofstream fr;
fr.open("langrensha.txt");
fr<<win<<lose<<hsl;
fr.close();
Sleep(3000);
return 0;
}