取火柴游戏c语言,取火柴游戏

玩完几把游戏后。。突然想起了一个比较老的游戏:取火柴游戏!!(*^▽^*)

取火柴游戏

游戏规则简单:不需要捡装备,也没有危险的空投。emm言归正传!

有n根火柴,每人每次最多取4根火柴,最少取一根火柴。如果某人取到最后一根火柴,那么恭喜你,输了!

emmm上代码!

void quhuochai() {

srand(time(NULL));//随机种子;

while(1) {

printf("----------目前还有火柴 %d 根----------\n",last);

printf("用户取火柴数量:");

scanf("%d",&user);

if(user<1||user>4||user>last) {

printf("(*^▽^*)你犯规了!火柴取的有问题!");

continue;

}

last=last-user;

if(last==0) {

printf("\n用户取走了最后一根火柴,因此计算机GG了!\n");

break;

} else {

if(last>4) {//注意条件,之前脑残写错了。。。。

computer=rand()%5;

} else if(last<=1) {

computer=1;

} else {

computer=last-1;

}

last=last-computer;

printf("计算机取火柴数量:%d \n",computer);

if(last==0) {

printf("计算机取走了最后一根火柴,于是你输啦~(*^▽^*)!!\n");

break;

}

}

}

}

其中,每次抽取的时候都应该判断一下是否违规,并计算剩余的火柴数量last。第一次由用户进行输入,之后计算机根据之前的算法思路来抽取,直到所有的火柴都抽取完毕。

完整的代码:

#include #include #include using namespace std;

int computer,user,last;

void quhuochai() {

srand(time(NULL));//随机种子;

while(1) {

printf("----------目前还有火柴 %d 根----------\n",last);

printf("用户取火柴数量:");

scanf("%d",&user);

if(user<1||user>4||user>last) {

printf("(*^▽^*)你犯规了!火柴取的有问题!");

continue;

}

last=last-user;

if(last==0) {

printf("\n用户取走了最后一根火柴,因此计算机GG了!\n");

break;

} else {

if(last>4) {//注意条件,之前脑残写错了。。。。

computer=rand()%5;

} else if(last<=1) {

computer=1;

} else {

computer=last-1;

}

last=last-computer;

printf("计算机取火柴数量:%d \n",computer);

if(last==0) {

printf("计算机取走了最后一根火柴,于是你输啦~(*^▽^*)!!\n");

break;

}

}

}

}

int main() {

int num;

printf("取火柴游戏!\n");

printf("请先输入火柴的总量为:");

scanf("%d",&num);

printf("火柴的总量为:%d: ",num);

last=num;

quhuochai();

return 0;

}

期待一下明天的游戏吧!~^_^!(*╹▽╹*)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值