一个取石子游戏

· · 休闲·娱乐

RT

游戏来源:B 站视频链接

游戏代码:

#include<cstdio>
#include<iostream>
#include<fstream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define Set(a) memset(a,0,sizeof(a))
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define openf(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define re register
#define ri re int
#define il inline
typedef long long ll;
typedef unsigned long long ull;
template<typename T> inline T rd(T& x)
{
    T f=1;x=0;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(T)(c-'0');
    x*=f;
    return x;
}
ll rd(){ll x;rd(x);return x;}
inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;}
const int inf=1<<30;

#include<string>
int l,r;
#define cls system("cls")
#define pus system("pause")
void gen()
{
    cls;
    printf("请选择生成初始局面的方法:\n");
    printf("1.机器生成\n2.手动输入\n");
    string s;
    sas:cin>>s;
    while(s!="1"&&s!="2") {printf("输入错误,请重新输入\n");pus;goto sas;}
    if(s=="1")
    {
        cls;
        l=rand()%20+1;
        r=2*(rand()%10+1)-(l%2);
        printf("生成结果:左 %d,右 %d\n",l,r);
    }
    else
    {
        yyy:cls;
        printf("请输入初始局面左、右分别有多少石子:\n");
        printf("(注意:要求两边石子数都不超过 20,而且同奇偶)\n");
        cin>>l>>r;
        while(l<=0||l>20||r<=0||r>20||((r-l)%2!=0)) {printf("输入不合法,请重新输入\n");pus;goto yyy;}
    }
    pus;
}
void A(string name){cout<<name<<" 选择了 A 操作。"<<endl;--l;++r;}
void B(string name){cout<<name<<" 选择了 B 操作。"<<endl;--l;--r;}
void C(string name){cout<<name<<" 选择了 C 操作。"<<endl;r-=2;}
void doit(string name){cout<<"现在轮到 "<<name<<"。"<<endl<<"左  右"<<endl;printf("%2d  %2d\n",l,r);}
void comp()
{
    cls;
    string s="电脑";
    doit(s);
    switch(r%4)
    {
        case 1:B(s);break;
        case 2:C(s);break;
        case 3:A(s);break;
        default:
            int x;
            qaq:x=rand()%3;
            if(x==0) {if(!l) goto qaq;A(s);}
            if(x==1) {if(!l||!r) goto qaq;B(s);}
            if(x==2) {if(r<=1) goto qaq;C(s);}
    }
    pus;
}
void player(string name)
{
    awa:cls;
    doit(name);
    cout<<"A:从左边拿一颗石子到右边;"<<endl;
    cout<<"B:左右各拿掉一颗石子;"<<endl;
    cout<<"C:从右边拿掉两颗石子。"<<endl;
    cout<<"请选择:"<<endl;
    string s;
    cin>>s;
    if(s=="A"){if(!l){printf("操作不合法,请重新输入\n");pus;goto awa;}A(name);}
    else if(s=="B"){if(!l||!r){printf("操作不合法,请重新输入\n");pus;goto awa;}B(name);}
    else if(s=="C"){if(r<=1){printf("操作不合法,请重新输入\n");pus;goto awa;}C(name);}
    else {printf("输入不合法,请重新输入\n");pus;goto awa;}
    pus;
}
void player_to_player()
{
    cls;
    string s[2];s[0]="玩家 A",s[1]="玩家 B";
    int cnt=-1;
    cout<<s[0]<<" 为先手,"<<s[1]<<" 为后手。"<<endl;
    pus;cls;
    while(l||r)
    {
        ++cnt;
        player(s[cnt&1]);
    }
    cls;
    doit(s[(cnt&1)^1]);
    cout<<s[(cnt&1)^1]<<" 无棋可走。"<<endl;
    cout<<s[cnt&1]<<" 获胜。"<<endl;
    pus;
}
void player_to_computer()
{
    cls;
    string s[2];s[0]="玩家",s[1]="电脑";
    int cnt=-1;
    cout<<s[0]<<" 为先手,"<<s[1]<<" 为后手。"<<endl;
    pus;cls;
    while(l||r)
    {
        ++cnt;
        if(cnt&1) comp();else player(s[0]);
    }
    cls;
    doit(s[(cnt&1)^1]);
    cout<<s[(cnt&1)^1]<<" 无棋可走。"<<endl;
    cout<<s[cnt&1]<<" 获胜。"<<endl;
    pus;
}
void computer_to_player()
{
    cls;
    string s[2];s[0]="电脑",s[1]="玩家";
    int cnt=-1;
    cout<<s[0]<<" 为先手,"<<s[1]<<" 为后手。"<<endl;
    pus;cls;
    while(l||r)
    {
        ++cnt;
        if(!(cnt&1)) comp();else player(s[1]);
    }
    cls;
    doit(s[(cnt&1)^1]);
    cout<<s[(cnt&1)^1]<<" 无棋可走。"<<endl;
    cout<<s[cnt&1]<<" 获胜。"<<endl;
    pus;
}
void help()
{
    cls;
    cout<<"规则如下:"<<endl;
    cout<<"有左右两堆石子,有三种操作,两人轮流执行,直到某人执行不了为止。"<<endl;
    cout<<"三种操作分别如下:"<<endl;
    cout<<"A:从左边拿一颗石子到右边;"<<endl;
    cout<<"B:左右各拿掉一颗石子;"<<endl;
    cout<<"C:从右边拿掉两颗石子。"<<endl;
    cout<<"游玩愉快!"<<endl;
    pus;
}
#include<ctime>
int main()
{
    system("title 石子游戏I");
    srand(time(NULL));
    string s,ss;
    start:cls;
    cout<<"欢迎来到石子游戏I!"<<endl;
    cout<<"输入 help 查看帮助"<<endl;
    cout<<"输入 pp 来 玩家-玩家 对战"<<endl;
    cout<<"输入 cp 来 电脑-玩家 对战(先后手可以自定义)"<<endl;
    cout<<"输入 exit 来退出"<<endl;
    cin>>s;
    if(s=="help") help();
    else if(s=="exit") return 0;
    else if(s=="pp"){gen();player_to_player();}
    else if(s=="cp")
    {
        gen();cls;
        cout<<"左  右"<<endl;printf("%2d  %2d\n",l,r);
        cout<<"请选择先后手(0 为先手,1 为后手):"<<endl;
        pvp:cin>>ss;
        if(ss!="0"&&ss!="1") {printf("输入不合法,请重新输入\n");pus;goto pvp;}
        if(ss=="0") player_to_computer();else computer_to_player();
    }
    else {printf("输入不合法,请重新输入\n");pus;goto start;}
    goto start;
    return 0;
}

(这**玩意我整整写了 1h)