麻烦大家帮帮忙 直接把代码写下来 讲算法我什么都不懂
有23根火柴(或小棒)。每个游戏者轮流移走1根、2根或3根火柴,拿到最后一根火柴的就算输了。编一程序与计算机玩这个两个游戏者开始拥游戏
用C++做23根火柴游戏
答案:2 悬赏:70 手机版
解决时间 2021-01-29 05:18
- 提问者网友:萌卜娃娃
- 2021-01-28 22:45
最佳答案
- 五星知识达人网友:毛毛
- 2021-01-28 23:16
终于改完啦!嗯,完整版,包括函数,注释!
#include
#include
#define NUM 20
using namespace std;
void main()
{
//规则的输出
cout<
cout<<"***************************************************"<
cout<<"游戏规则:共有23根火柴,2个人依次拿取,每个人每次只"<
cout<<"能拿1跟或者2跟或者3跟火柴。拿到最后一跟火柴的人算输"<
cout<<"****************************************************"<
cout<
//变量的定义
int match_num=23; //火柴数目
int *p_match_num; //指向火柴数目的指针
int player = 0; //玩家变量,偶数代表玩家1,奇数代表玩家2
char player_first[NUM]; //玩家1的名字
char player_second[NUM]; //玩家2的名字
char judge; //判断变量,值为'y'时,表示玩家1先开局
int put_num=0; //拿走火柴的数目
char temp[NUM];
char *p_player_now;
int *p_put_num; //指向拿走火柴数目的指针
int *p_player;
//指针赋值
p_match_num=&match_num;
p_put_num=&put_num;
p_player=&player;
p_player_now=player_first;
//函数的定义
void match(int* p_player, int* p_match_num,int* p_put_num,char* p_player_now); //火柴拿走数目函数
//游戏开始
cout<<"请输入玩家1的名字:";
gets(player_first);
cout<<"请输入玩家2的名字:";
gets(player_second);
//玩家开局顺序的选择
cout<<"玩家“"<
cin>>judge;
if(judge=='s')
{
strcpy(temp,player_first);
strcpy(player_first,player_second);
strcpy(player_second,temp);
}
cout<<"玩家“"<
cout<
//火柴的拿取
while(1)
{
if(player%2==0)
p_player_now=player_first;
else
p_player_now=player_second;
match(p_player,p_match_num,p_put_num,p_player_now);
match_num=match_num-put_num;
if(match_num==0)
{
cout<<"玩家“"<
break;
}
}
}
void match(int* p_player,int* p_match_num,int* p_put_num,char* p_player_now)
{
cout<<"请“"<
cin>>*p_put_num;
while(*p_put_num!=1 && *p_put_num!=2 && *p_put_num!=3 || *p_match_num-*p_put_num<0)
{
if(*p_put_num!=1 && *p_put_num!=2 && *p_put_num!=3)
{
cout<<"您输入的数值不合法,拿走的火柴数目只能是 1 或 2 或 3,请重新输入:";
cin>>*p_put_num;
}
if(*p_match_num-*p_put_num<0)
{
cout<<"您输入的数值大于剩余火柴数目,请重新输入:";
cin>>*p_put_num;
}
}
cout<<"您拿走的火柴数目是: "<<*p_put_num<
cout<<"剩余火柴数目是: "<<*p_match_num-*p_put_num<
cout<
(*p_player)++;
}
#include
#include
#define NUM 20
using namespace std;
void main()
{
//规则的输出
cout<
int match_num=23; //火柴数目
int *p_match_num; //指向火柴数目的指针
int player = 0; //玩家变量,偶数代表玩家1,奇数代表玩家2
char player_first[NUM]; //玩家1的名字
char player_second[NUM]; //玩家2的名字
char judge; //判断变量,值为'y'时,表示玩家1先开局
int put_num=0; //拿走火柴的数目
char temp[NUM];
char *p_player_now;
int *p_put_num; //指向拿走火柴数目的指针
int *p_player;
//指针赋值
p_match_num=&match_num;
p_put_num=&put_num;
p_player=&player;
p_player_now=player_first;
//函数的定义
void match(int* p_player, int* p_match_num,int* p_put_num,char* p_player_now); //火柴拿走数目函数
//游戏开始
cout<<"请输入玩家1的名字:";
gets(player_first);
cout<<"请输入玩家2的名字:";
gets(player_second);
//玩家开局顺序的选择
cout<<"玩家“"<
if(judge=='s')
{
strcpy(temp,player_first);
strcpy(player_first,player_second);
strcpy(player_second,temp);
}
cout<<"玩家“"<
while(1)
{
if(player%2==0)
p_player_now=player_first;
else
p_player_now=player_second;
match(p_player,p_match_num,p_put_num,p_player_now);
match_num=match_num-put_num;
if(match_num==0)
{
cout<<"玩家“"<
}
}
}
void match(int* p_player,int* p_match_num,int* p_put_num,char* p_player_now)
{
cout<<"请“"<
while(*p_put_num!=1 && *p_put_num!=2 && *p_put_num!=3 || *p_match_num-*p_put_num<0)
{
if(*p_put_num!=1 && *p_put_num!=2 && *p_put_num!=3)
{
cout<<"您输入的数值不合法,拿走的火柴数目只能是 1 或 2 或 3,请重新输入:";
cin>>*p_put_num;
}
if(*p_match_num-*p_put_num<0)
{
cout<<"您输入的数值大于剩余火柴数目,请重新输入:";
cin>>*p_put_num;
}
}
cout<<"您拿走的火柴数目是: "<<*p_put_num<
}
全部回答
- 1楼网友:鱼忧
- 2021-01-28 23:59
#include
#include
#include
int main()
{
int n,i,ct;
int win[24]={-1,1,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2};
for(srand(time(0)),ct=1,n=23;n;++ct)
{
printf("Turn %d (there is %d matches): %s takes ",ct,n,ct%2?"Player":"Computer");
if(ct%2)
{
scanf("%d",&i);
n-=i;
}
else
{
if(win[n]) i=win[n];
else i=rand()%3+1;
n-=i;
printf("%d\n",i);
}
}
printf("\n%s wins
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯