函数参数传递问题,编译的时候没错,运行有错,求大神给改改,尽量快点
#include<iostream>#include<ctime>
#include<string>
#include<cstdlib>
using namespace std;
void exit();
void menu();
void renji();
void renren();
void aboutgame();
void select();
void go();
void back();
int shezhi();
void renji(int total,int m)
{
int k;
cout<<"请问谁先谁后? 1.玩家 2.电脑"<<endl;
cin>>k;
int my;
int computer;
int t1=total;
int m1=m;
int i;
srand(time(0));
i=rand()%m1+1;
if(k==1)
{
while(t1!=0)
{
cout<<"请输入您要拿取几个:"<<endl;
cin>>my;
while(my>m1||my<1)
{
cout<<"您所输入的值不符合要求,请重新输入:"<<endl;
cin>>my;
}
while(my>t1)
{
cout<<"您所取得大于剩于的个数,请重新输入:"<<endl;
cin>>my;
}
t1=t1-my;
cout<<"还剩余"<<t1<<"个"<<endl;
if(t1==0)
{
cout<<"恭喜您获胜!"<<endl;
break;
}
computer=i;
cout<<"电脑取了"<<computer<<"个"<<endl;
t1=t1-computer;
cout<<"还剩余"<<t1<<"个"<<endl;
if(t1==0)
{
cout<<"恭喜电脑获胜!"<<endl;
break;
}
}
}
if(k==2)
{
while(t1!=0)
{
computer=i;
cout<<"电脑取了"<<computer<<"个"<<endl;
t1=t1-computer;
cout<<"还剩余"<<t1<<"个"<<endl;
if(t1==0)
{
cout<<"恭喜电脑获胜!"<<endl;
break;
}
cout<<"请输入你要拿取几个:"<<endl;
cin>>my;
while(my>m1||my<1)
{
cout<<"您所输入的值不符合要求,请重新输入:"<<endl;
cin>>my;
}
while(my>t1)
{
cout<<"您所取得大于剩于的个数,请重新输入:"<<endl;
cin>>my;
}
t1=t1-my;
cout<<"还剩余"<<t1<<"个"<<endl;
if(t1==0)
{
cout<<"恭喜玩家获胜!"<<endl;
break;
}
}
}
}
void renren(int total,int m,char play1[],char play2[])
{
int k;
cout<<"请问谁先谁后? 1.玩家"<<play1<<" 2.玩家"<<play2<<endl;
cin>>k;
int p1;
int p2;
int t2=total;
int m2=m;
if(k==1)
{
cout<<"玩家"<<play1<<"先开始"<<endl;
while(t2!=0)
{
cout<<play1<<"拿取几个:"<<endl;
cin>>p1;
while(p1>m2||p1<1)
{
cout<<"您所输入的值不符合要求,请重新输入:"<<endl;
cin>>p1;
}
while(p1>t2)
{
cout<<"您所取得大于剩于的个数,请重新输入:"<<endl;
cin>>p1;
}
t2=t2-p1;
cout<<"还剩余"<<t2<<"个"<<endl;
if(t2==0)
{
cout<<"恭喜玩家"<<play1<<"获胜!"<<endl;
break;
}
cout<<play2<<"拿取几个:"<<endl;
cin>>p2;
while(p2>m2||p2<1)
{
cout<<"您所输入的值不符合要求,请重新输入:"<<endl;
cin>>p2;
}
while(p2>t2)
{
cout<<"您所取得大于剩于的个数,请重新输入:"<<endl;
cin>>p2;
}
t2=t2-p2;
cout<<"还剩余"<<t2<<"个"<<endl;
if(t2==0)
{
cout<<"恭喜玩家"<<play2<<"获胜!"<<endl;
break;
}
}
}
if(k==2)
{
cout<<"玩家"<<play2<<"先开始"<<endl;
while(t2!=0)
{
cout<<play2<<"拿取几个:"<<endl;
cin>>p2;
while(p2>m2||p2<1)
{
cout<<"您所输入的值不符合要求,请重新输入:"<<endl;
cin>>p2;
}
while(p2>t2)
{
cout<<"您所取得大于剩于的个数,请重新输入:"<<endl;
cin>>p2;
}
t2=t2-p2;
cout<<"还剩余"<<t2<<"个"<<endl;
if(t2==0)
{
cout<<"恭喜玩家"<<play2<<"获胜!"<<endl;
break;
}
cout<<play1<<"拿取几个:"<<endl;
cin>>p1;
while(p1>m2||p1<1)
{
cout<<"您所输入的值不符合要求,请重新输入:"<<endl;
cin>>p1;
}
while(p1>t2)
{
cout<<"您所取得大于剩于的个数,请重新输入:"<<endl;
cin>>p1;
}
t2=t2-p1;
cout<<"还剩余"<<t2<<"个"<<endl;
if(t2==0)
{
cout<<"恭喜玩家"<<play1<<"获胜!"<<endl;
break;
}
}
}
}
void aboutgame()
{
cout<<"拈游戏是比较经典的游戏之一,我们这个游戏所探讨的是单堆拈游戏"<<endl;
cout<<"即从一堆石子中拿取石子,石子的个数和每次所拿取的石子个数上限"<<endl;
cout<<"提前人为定好,能够把最后的石子取出来的玩家即为胜者。"<<endl;
}
int shezhi()
{
int total;
int m;
while(true)
{
cout<<"请设置游戏石子的总数:"<<endl;
cin>>total;
cout<<"请设置每次拿取石子的上限数:"<<endl;
cin>>m;
if(m>0 && total>=m)
{
break;
}
cout<<"设置有误,请重新设置!"<<endl;
}
cout<<"总共有"<<total<<"个,每次最多拿取"<<m<<"个,至少拿取1个"<<endl;
return total;
return m;
}
void menu()
{
cout<<"**********欢迎使用拈游戏**********"<<endl;
cout<<" 1.人机游戏模式 "<<endl;
cout<<" 2.人人游戏模式 "<<endl;
cout<<" 3.游戏简要说明 "<<endl;
cout<<" 4.游戏退出 "<<endl;
cout<<"请选择想要执行的操作:"<<endl;
int s;
cin>>s;
switch(s)
{
case 1:shezhi();
renji();
back();
getchar();
getchar();
system("CLS");
go();
break;
case 2:renren();
back();
getchar();
getchar();
system("CLS");
go();
break;
case 3:aboutgame();
back();
getchar();
getchar();
system("CLS");
go();
break;
case 4:exit(1);
back();
getchar();
getchar();
system("CLS");
go();
break;
default:cout<<"输入指令错误!系统退出!"<<endl;
break;
}
}
void back()
{
cout<<"按回车键返回菜单"<<endl;
}
void exit(void)
{
exit(1);
}
void go()
{
menu();
select();
}
void main()
{
go();
}