c++小棒抽取(规则见游戏内)

#include<bits/stdc++.h>
using namespace std;
int n;
bool pd(string d,int x){
	if(d=="1"||d=="2"){
		if(x<=n){
			return true;
		}
	}
	return false;
}
int main(){
	string s[3]={{""},{"one"},{"two"}};
	cout<<"The rules of the game: there are a total of n small sticks, one person and one AI take turns to take it, you can take one or two, and if you get the last one, you will be judged to lose, and the order of taking it is determined by AI.\n Please enter the number of sticks:";
	cin>>n;
	if(n%3==1){
		cout<<"AI chooses the next hand.\n Please take it first:";
		while(true){
			int x;
			cin>>x;
			if(pd(to_string(x),x)){
				n-=x;
				if(n==0){
					break;
				}
				cout<<"AI takes "<<s[3-x]<<" small sticks.\nremainder "<<n-3+x<<" small sticks.\n";
				n-=3-x;
				cout<<"Please pick it up again:";
			}
			else{
				cout<<"There is an incorrect input. Please re-enter:";
			}
		}
		cout<<"You lose!";
	}
	else{
		if(n%3==2){
			cout<<"AI chooses to strike first.\nAI extracts one Small sticks.\nRemaining sticks: "<<n-1<<" small sticks.\n"<<"Please draw a stick:";
			n--;
			while(true){
				int x;
				cin>>x; 
				if(pd(to_string(x),x)){
					n-=x;
					if(n==0){
						break;
					}
					cout<<"AI takes "<<s[3-x]<<" small sticks.\nremainder "<<n-3+x<<" Small sticks.\n";
					n-=3-x;
					cout<<"Please pick it up again:";
				}
				else{
					cout<<"There is an incorrect input. Please re-enter:";
				}
			}
			cout<<"You lose!";			
		}
		else{
			cout<<"AI chooses to strike first.\nAI extracts two Small sticks.\nRemaining sticks: "<<n-2<<" small sticks.\n"<<"Please draw a stick:";
			n-=2;
			while(true){
				int x;
				cin>>x; 
				if(pd(to_string(x),x)){
					n-=x;
					if(n==0){
						break;
					}
					cout<<"AI takes "<<s[3-x]<<" small sticks.\nremainder "<<n-3+x<<" Small sticks.\n";
					n-=3-x;
					cout<<"Please pick it up again:";
				}
				else{
					cout<<"There is an incorrect input. Please re-enter:";
				}
			}
			cout<<"You lose!";	
		}
	}
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值