黄金点游戏v0.1

    2020年11月01日 字数:1992

基本实现了游戏的功能,UI功能尚未上线

#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
double read()
{
    bool flag=false;
    double first=0,second=0,f=1,q=1;
    char ch;
    do
    {
        ch=getch();
        if(ch=='\r') return -1;
        if(ch=='-') f=-1;
        else if(ch<'0'||ch>'9') flag=true;
    }
    while(!('0'<=ch&&ch<='9'));
    while('0'<=ch&&ch<='9') first=first*10+(ch-'0'),ch=getch();
    if(ch=='.')
    {
        ch=getch();
        while('0'<=ch&&ch<='9')
            q/=10,second=second+q*(ch-'0'),ch=getch();
    }
    while(ch!='\r')
    {
        if(!('0'<=ch&&ch<='9')) flag=true;
        ch=getch(); 
    }
    if(flag) return -1;
    return f*(first+second); 
}
struct Player
{
    private:
    bool is_human;
    int score;
    string name; 
    double choice[111];
    int gain[111];
    public:
    Player()
    {
        is_human=false;
        name="Hawaii";
        score=0;
    }
    void setPlayername(string str)
    {
        is_human=true;
        name=str;
    }
    void newchoice(int t)
    {
        double temp;
        if(is_human==false)
        {
            temp=100.0*(rand()+1)/(0x7fff+2);
            cout<<"AI"<<name<<"已输入数x(0<x<100)"<<endl; 
        }
        else
        {
            while(1)
            {
                cout<<"请玩家"<<name<<"输入一个数x(0<x<100),以回车结束:\n";
                temp=read();
                if(!(0<temp&&temp<100)) printf("输入非法,请重新输入。\n"); 
                else break; 
            }
        }
        choice[t]=temp;
        return;
    }
    void win(int t,int n)
    {
        gain[t]=n;
        score+=n;
    }
    void draw(int t)
    {
        gain[t]=0;
        score+=0;
    }
    void lose(int t)
    {
        gain[t]=-2;
        score+=-2;
    }
    string showname()
    {
        return name;
    }
    double choice_i(int i)
    {
        if(0<=i&&i<1011) return choice[i];
        return -1; 
    }
    int gain_i(int i)
    {
        if(0<=i&&i<1011) return gain[i];
        return -1; 
    } 
};
struct Game
{
    int n;
    int t;
    Player p[1011];
    Game()
    {
        n=0;
        t=0;
        while(n<1)
        {
            printf("请输入游戏人数:\n");
            scanf("%d",&n); 
        }
        for(int i=1;i<=n;i++)
        {
            string str;
            printf("请输入玩家%d的姓名(若姓名为AI,则为AI):\n",i);
            cin>>str; 
            if(str!="AI") p[i].setPlayername(str);
        }
    };
    void newTurn()
    {
        printf("第 %d 轮:\n",++t);
        for(int i=1;i<=n;i++) p[i].newchoice(t);
        double avg=0;
        for(int i=1;i<=n;i++) avg+=p[i].choice_i(t);
        avg/=n;
        avg*=0.618;
        printf("裁判公布G值:%20lf\n",avg); 
        double mindis=100,maxdis=0;
        for(int i=1;i<=n;i++)
        {
            mindis=min(mindis,fabs(p[i].choice_i(t)-avg));
            maxdis=max(maxdis,fabs(p[i].choice_i(t)-avg));
        }
        for(int i=1;i<=n;i++)
        {
            if(mindis==fabs(p[i].choice_i(t)-avg)) p[i].win(t,n);
            else if(maxdis==fabs(p[i].choice_i(t)-avg)) p[i].lose(t);
            else p[i].draw(t);
        }
    };
    void scoreboard(int t_){
        if(t_<=t)
        {
            for(int i=1;i<=n;i++)
            {
                cout<<setw(20)<<p[i].showname()<<": ";
                printf("输入%20lf,得%4d分。\n",p[i].choice_i(t),p[i].gain_i(t)); 
            }
        }
        else printf("还未进行第%d轮游戏。\n",t_);
    };
}; 
int main()
{
    printf("欢迎进入黄金点游戏!\n");
    while(1) 
    {
        Game game;
        while(1)
        {
            game.newTurn();
            game.scoreboard(game.t);
        }
    }
    return 0;
} 

请我喝杯咖啡

取消

感谢您的支持,我会继续写出更优秀的文章!

扫码支持
扫码支持
请我喝杯咖啡

打开微信扫一扫,即可进行扫码打赏哦

comments powered by Disqus