C语言—“23”游戏

这篇博客主要介绍了如何使用C语言编写'23'游戏。博主分享了代码,并鼓励读者指出可能的错误或提供改进建议,旨在共同学习和进步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看题目:

在这里插入图片描述
接着上代码~

#include<stdio.h>

int main(void){
	
	int toothpick = 23,player_oper,flag = 0,oper;
	 
	printf("游戏开始......\n");
	while(toothpick > 0){
		printf("当前的牙签数为:%d 根\n",toothpick);
		printf("\n请输入你要取走的牙签数量:");	
		scanf("%d",&player_oper);
		fflush(stdin);
		while(!(player_oper > 0&&player_oper < 4)){
			printf("每次取走的牙签数只能为 1 、2 或 3 哦!\n");
re:			printf("请重新输入你要取走的牙签数量:");
			scanf("%d",&player_oper);
			fflush(stdin);
		}
		if(player_oper > toothpick){
			printf("取走的牙签数不能超过当前剩余的牙签数");
			goto re; 
		}
		printf("\n您取走的牙签数为:%d 根\n",player_oper);
		toothpick -= player_oper;
		printf("当前的牙签数为:%d 根\n",toothpick);
		if(toothpick == 0){
			flag = 1;
			break;
		}else{
			if(toothpick > 4){
				oper = 4 - player_oper;
			}else if(toothpick > 1){
				oper = toothpick - 1;
			}else{
				oper = 1;
			}
			toothpick -= oper;
			printf("\nAlice取走的牙签数为:%d 根\n",oper);
			if(toothpick == 0){
				flag = 2;
			} 
		} 
	}
	printf("游戏结束!");
	if(flag == 1){
		printf("Alice取得胜利!\n");
	}else{
		printf("您取得胜利!\n");
	}
	
	return 0;
}
如果有错误或者可以改进的可以评论或私信我哦!!!
一起学习 共同进步!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值