取火柴游戏

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

取火柴游戏

游戏规则简单:不需要捡装备,也没有危险的空投。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;
}

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

你可能感兴趣的:(经典游戏算法重现)