恶魔轮盘赌(第三版)

· · 休闲·娱乐

进行了 AI 的一些微调


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define FAIL_FIND (18446744073709551615)
const int name_max=10;//名字长度
const int name_min=3;
const int gun_max=20;//子弹
const int SORT_MAX=100;//最大排序数
const int MAX_ITEM=8;
const int ITEM_TYPE=10;
const int START_HP=4;
const int MAX_HP=4;
const int DAN_MAX=8;//最大子弹数
const int DAN_MIN=3;
const int MAX_PUSH_ITEM=4;
const int MIN_PUSH_ITEM=1;
bool wujin=false;//无尽模式
int level=1;//无尽模式闯关数
string s[ITEM_TYPE+1]={" ","  啤酒  ","   烟   "," 放大镜 ","   刀   ","  手铐  ",
    " 通讯机 ","肾上腺素","  锤子  "," 过期药 "," 翻转器 "};
void ERROR(int type){
    switch (type) {
    case 1:
        cout<<"\n溢出警告!\n";
        system("pause");
        break;
    case 2:
        cout<<"\n不存在的索引...\n";
        system("pause");
        break;
    case 3:
        cout<<"\n能遇到这种错误真是神经...\n";
        system("pause");
        break;
    case 4:
        cout<<"\n死循环了阿喂\n";
        system("pause");
        break;
    default:
        cout<<"\n未知的错误...\n";
        system("pause");
        break;
    }
    exit(0);
}
enum {
    beer =1,
    huazi=2,
    bigger=3,
    knife=4,
    stopper=5, 
    phone=6,
    chief=7,
    reverser=8,
    bad_machine=9,
    changer=10,
};
int random(int a,int b){
    if(a<b)swap(a,b);
    return(rand()*100000000ll+rand()*10000ll+rand())%(a-b+1)+b;
}
string NAME(){
    int len=random(name_max,name_min);
    int num=0;
    string s="";
    bool flag=true;//防止特殊字符凑在一起
    while(len>num){
        if(num!=0){
            if(random(1,10)<=1&&flag)s+=' ',flag=false;
            else if(random(1,9)<=1&&flag)s+='_',flag=false;
            else if(random(1,8)<=1&&flag)s+='.',flag=false;
            else { 
                s+=char('a'+random(0,25));
                num++;
                flag=true;
                continue;
            }
        }
        s+=char('a'+random(0,25));
        flag=true;
        num++;
    }
    return s;
}
class GUN{
public:
    int d[gun_max+1];
    int top;
    int atk;
    int _empty,full;
    GUN(){
        top=0;
        atk=1;
    }
    void compute(){
        full=0;
        _empty=0;
        for(int i=1;i<=top;i++){
            if(d[i])full++;
            else _empty++;
        }
    }
    void init(int num){
        again1:
        if(num>gun_max)ERROR(1);
        for(int i=1;i<=num;i++){
            d[i]=random(1,0);
        }
        top=num;
        compute();
        if(full==0||_empty==0){
            clear();
            goto again1;
        }
        if(full*1000/top<=180||_empty*1000/top<180)goto again1;
    }
    void push(int type){
        d[++top]=type;
        compute();
    }
    void pop(){
        top--;
        compute();
    }
    int get_top(){
        return d[top];
    }
    void rand_rank(){
        for(int i=1;i<=SORT_MAX;i++){
            int u=random(1,top);
            int v=random(1,top);
            swap(d[u],d[v]);
        }
    }
    void rand_type(){
        for(int i=1;i<=top;i++){
            d[i]=random(1,0);
        }
        compute();
    }
    void reverse(){
        for(int i=1;i<=top;i++){
            d[i]=1-d[i];
        }
        compute();
    }
    void change_one(int place,int type){
        d[place]=type;
        compute();
    }
    void reverse_one(int place){
        d[place]=1-d[place];
    }
    bool empty(){
        return (top==0?1:0);
    }
    void clear(){
        top=0;
    }
};
class ITEM{
public:
    int type[MAX_ITEM+1];
    int top;
    ITEM(){
        top=0;
    }
    void pop(int pl){
        if(pl>top||pl<0)ERROR(1);
        for(int i=pl;i<top;i++){
            type[i]=type[i+1];
        }
        top--;
    }
    void clear(){
        top=0;
    }
    void push(int type,string name){
        if(top>=MAX_ITEM){
            cout<<name<<"的道具已满,装不下任何东西了"<<endl;
            return;
        }
        this->type[++top]=type;
    }
    void push(int type){
        if(top>=MAX_ITEM){
            return;
        }
        this->type[++top]=type;
    }
    void rand_push_much(int num,string name){
        if(num>MAX_ITEM)ERROR(1);
        for(int i=1;i<=num;i++){
            if(top+i>MAX_ITEM){//超了
                top=MAX_ITEM;
                return;
            }
            int _type=random(1,ITEM_TYPE);
            type[top+i]=_type;
            cout<<name<<"获得了"<<s[_type]<<endl;
        }
        top+=num;
    }
    int get(int pl){
        return type[pl];
    }
    int count(int type){
        int cnt=0;
        for(int i=1;i<=top;i++){
            if(this->type[i]==type)cnt++;
        }
        return cnt;
    }
    bool exist(int type){
        return (count(type)>=1);
    }
    int bound(int type){
        int place=0;
        for(int i=1;i<=top;i++){
            if(this->type[i]==type)place=i;
        }
        return place;
    }
    void pop_type(int type){
        if(!exist(type))ERROR(2);
        pop(bound(type));
    }
    bool empty(){
        return (top==0?1:0);
    }
    void print(string name){
        cout<<"The following is "<<name<<"`s thing:\n";
        for(int i=1;i<=top;i++){
            cout<<i<<":"<<s[type[i]]<<" ";
        }
    }
};
GUN gun;
class PLAYER{
public:
    int sure;//-1不知道 1实弹0空弹
    int hp;
    ITEM item;
    bool ai;
    string name;
    int huitime;
    bool stopper_used;
    bool knife_used;
    PLAYER(){
        hp=START_HP;
        sure=-1;
        stopper_used=false;
        knife_used=false;
//      ai=0;
    }
    void update(){
        if(gun.full==0)sure=0;
        if(gun._empty==0)sure=1;
    }
    int AI(PLAYER p2){//-1打自己-2打对方1~ITEM_TYPE用道具
        gun.compute();
        update();
        //不那么人机的操作
        if(hp<MAX_HP&&item.exist(huazi))return huazi;
        if(sure==1&&item.exist(knife)&&gun.atk==1&&!knife_used)return knife;
        if(sure==1)return -2;
        if(sure==0&&(item.exist(reverser)||item.exist(changer))){
            if(item.exist(reverser))return reverser;
            else return changer;
        }
        if(sure==0&&item.exist(beer))return beer;
        if(sure==0)return -1;
        if(gun.top>=2&&item.exist(stopper)&&!stopper_used)return stopper;
        if(item.exist(chief)&&!p2.item.empty())return chief;
        if(hp>=2&&hp<=MAX_HP-2&&item.exist(bad_machine))return bad_machine;
        if(item.exist(bigger))return bigger;
        if(item.empty()){
            if((gun.full>=gun._empty-1)||knife_used)return -2;
            else return -1;
        }
        else {
            while(true){
                int type=random(1,ITEM_TYPE);
                if(item.exist(type)){
                    if(random(1,3)<=1){
                        if((gun.full>=gun._empty-1)||knife_used)return -2;//用刀肯定不打自己(太唐了)
                        else return -1;
                    }
                    if(hp==MAX_HP&&type==huazi)continue;
                    if(p2.item.empty()&&type==chief)continue;
                    return type;
                }
            }
        }
    }
    int AI_choose_thing(ITEM item){
        if(item.empty())ERROR(4);
        if(item.exist(chief))return chief;//先选有用的东西
        if(item.exist(huazi))return huazi;
        if(item.exist(stopper))return stopper;
        if(item.exist(bigger))return bigger;
        while(true){
            int type=random(1,ITEM_TYPE);
            if(item.exist(type))return type;
        }
    }
    void print_hp(){
        cout<<name<<"`s hp:";
        for(int i=1;i<=hp;i++)cout<<"*";
    }
};
bool over(PLAYER p1,PLAYER p2){
    system("cls");
    if(p1.hp<=0&&p2.hp<=0){
        cout<<"平局...\n";
        system("pause");
//      exit(0);
        return 1;
    }
    else if(p1.hp<=0||p2.hp<=0){
        if(p2.hp<=0)swap(p1,p2);//保证p1是输家
        if((!p1.ai&&p2.ai)){
            if(!wujin)cout<<"打人机都打不过,菜就多练\n";
            else {
                cout<<"你坚持了"<<level<<"关";
                if(level>=3)cout<<"orzorzorz\n";
                cout<<endl;
                system("pause");
                exit(0);
            }
            system("pause");
            return 1;
        }
        else if((p1.ai&&p2.ai)||(!p1.ai&&!p2.ai)){
            cout<<p2.name<<"胜利\n";
            system("pause");
            return 1;
        }
        else {
            cout<<p2.name<<"赢得了胜利\n";
            system("pause");
            return 1;
        }
    }
    return 0;
}
//-1:self -2:anemy 1~ITEM_TYPE用道具
void compute(PLAYER &p1,PLAYER &p2,int type){//p1为发起者
    switch (type) {
    case -1:
        if(gun.get_top()==1){
            cout<<p1.name<<"向自己打了一发,是实弹,"<<p1.name<<"  hp-"<<gun.atk<<endl;
            p1.huitime--;
            p1.hp-=gun.atk;
            gun.atk=1;
            gun.pop();
        }
        else {
            cout<<p1.name<<"向自己打了一发,是空弹\n";
            gun.atk=1;//不能延续
            gun.pop();
        }
        p1.sure=-1;
        p1.knife_used=false;
        break;
    case -2:
        if(gun.get_top()==1){
            cout<<p1.name<<"向"<<p2.name<<"打了一发,是实弹,"<<p2.name<<"  hp-"<<gun.atk<<endl;
            p1.huitime--;
            p2.hp-=gun.atk;
            gun.atk=1;
            gun.pop();
        }
        else {
            cout<<p1.name<<"向"<<p2.name<<"打了一发,是空弹\n";
            p1.huitime--;
            gun.atk=1;
            gun.pop();
        }
        p1.sure=-1;
        p1.knife_used=false;
        break;
    case beer:
        cout<<p1.name<<"喝了一罐啤酒,退了一发"<<(gun.get_top()==1?"实":"空")<<"弹\n";
        gun.pop();
        p1.sure=-1;
        break;
    case huazi:
        cout<<p1.name<<"抽了一根华子,hp+1\n";
        p1.hp++;
        break;
    case bigger:
        cout<<p1.name<<"使用一个放大器\n";
        if(!p1.ai)cout<<"下一发子弹是"<<(gun.get_top()==1?"实":"空")<<"的\n";
        else p1.sure=gun.get_top();//人机
        break;
    case knife:
        if(p1.ai&&p1.knife_used){
            p1.item.push(knife);
            return;
        }
        if(!p1.ai&&p1.knife_used){
            cout<<"你本回合已经用过刀了\n";
            p1.item.push(knife);
            return;
        }
        cout<<p1.name<<"使用了破伤风之刃\n";
        gun.atk=2;
        p1.knife_used=true;
        break;
    case stopper:
        if(p1.ai&&p1.stopper_used){
            p1.item.push(stopper);
            return;
        }
        if(!p1.ai&&p1.stopper_used){
            cout<<"你本回合已经用过手铐了\n";
            p1.item.push(stopper);
            return;
        }
        cout<<p1.name<<"使用了手铐,铐住了"<<p2.name<<endl;
        p1.huitime++;//可以再打一次了
        p1.stopper_used=true;
        break;
    case phone:
        cout<<p1.name<<"使用了"<<s[type]<<endl;
        if(p1.ai){
            if(random(1,3)<=1){
                p1.sure=gun.get_top();
            }
        }//AI的我还没想好怎么写(懒得写)
        else {
            int pl=random(1,min(4ll,gun.top));
            int tt=gun.d[gun.top-pl+1];
            cout<<"下"<<pl<<"个子弹是"<<(tt==1?"实":"空")<<"弹\n";
        }
        break;
    case chief:
        cout<<p1.name<<"使用了"<<s[type]<<endl;
        if(p2.item.top<=0){
            cout<<"但"<<p2.name<<"已经没有东西了\n";
            return;
        }
        if(!p1.ai){
            p2.item.print(p2.name);
            cout<<"你要那"<<p2.name<<"的哪一个东西?\n";
            int pl;
            cin>>pl;
            int _type=p2.item.type[pl];
            p2.item.pop(pl);
            p1.item.push(_type,p1.name);
        }
        else {
            int _type=p1.AI_choose_thing(p2.item);
            p2.item.pop(p2.item.bound(_type));
            p1.item.push(_type,p1.name);
            cout<<p1.name<<"拿走了"<<p2.name<<"的"<<s[_type]<<endl;
        }
        break;
    case reverser:
        cout<<p1.name<<"使用了"<<s[type]<<",枪里所有子弹都反向了\n";
        gun.reverse();
        if(p1.sure!=-1){
            p1.sure=1-p1.sure;//反向
        }
        break;
    case bad_machine:
        cout<<p1.name<<"使用了"<<s[type]<<endl;
        if(random(2,1)<=1){//糟luo~
            cout<<"这药有毒"<<p1.name<<"  hp-1"<<endl;
            p1.hp--;
        }
        else {
            cout<<"这药没毒,"<<p1.name<<"  hp+2"<<endl;
            p1.hp+=2;
        }
        break;
    case changer:
        cout<<p1.name<<"使用了"<<s[type]<<",翻转了枪里的第一颗子弹\n";
        gun.reverse_one(gun.top);
        if(p1.sure!=-1){
            p1.sure=1-p1.sure;//反向
        }
        break;
    default:
        ERROR(2);
        break;
    }
}
void GAME_INIT(PLAYER &p1,PLAYER &p2){
    cout<<"-----恶魔轮盘赌v1.1.0-----\n";
    cout<<"出品人:xzc\n\n";
    cout<<"upd:AI已加强\n";
    cout<<"upd:修复了一些问题\n";
    cout<<"upd:修复了一些Bug,加强了AI(现在AI不唐了)\n";
    cout<<"upd:更新无尽模式\n";
    cout<<"upd:修复了亿些bug\n"; 
    cout<<"p1人机? 1Yes 0No\n";
    int c;
    cin>>c;
    if(c!=0){
        p1.ai=1;
        p1.name=NAME();
    }
    else {
        p1.ai=0;
        cout<<"您的贵名?";
        cin>>p1.name;
    }
    cout<<"p2人机? 1Yes 0No\n";
    cin>>c;
    if(c!=0){
        p2.ai=1;
        p2.name=NAME();
    }
    else {
        p2.ai=0;
        cout<<"您的贵名?";
        cin>>p2.name;
    }
    if((p1.ai&&!p2.ai)||(!p1.ai&&p2.ai)){
        cout<<"是否开启无尽模式? 1Yes 0No\n";
        int op;
        cin>>op;
        if(op!=0)wujin=true;
    }
}
signed main(){
    srand(time(NULL));
    PLAYER p1,p2;
    GAME_INIT(p1,p2);
    p1.huitime=1;
    int round=0;//回合
    if(random(2,1)<=1)swap(p1,p2);
    while(true){
        if(over(p1,p2)){//判断游戏是否结束
            if(!wujin)return 0;
            else {
                if(p1.ai)swap(p1,p2);
                if(p1.hp>0){
                    level++;
                    p1.huitime=1;
                    p2.huitime=0;
                    p1.stopper_used=false;
                    p1.knife_used=false;
                    p2.stopper_used=false;
                    p2.knife_used=false;
                    p1.sure=-1;
                    p2.sure=-1;
                    p1.hp=START_HP;
                    p2.hp=START_HP;
                    p1.item.clear();
                    p2.item.clear();
                    round=1;
                    gun.clear();
                    gun.init(random(DAN_MAX,DAN_MIN));
                    gun.atk=1;
                    int num=random(MAX_PUSH_ITEM,MIN_PUSH_ITEM);
                    p1.item.rand_push_much(num,p1.name);
                    p2.item.rand_push_much(num,p2.name);
                }
                if(random(2,1)<=1)swap(p1,p2);
            }
        }
        if(p1.huitime<=0){//p1回合结束
            p1.huitime=0;
            p2.huitime=1;
            p1.stopper_used=false;
            p1.knife_used=false;
            p2.stopper_used=false;
            p2.knife_used=false;
            p1.sure=-1;
            p2.sure=-1;
            swap(p1,p2);
        }
        if(gun.top<=0){//子弹打完了
            cout<<"装子弹中...\n";
            gun.init(random(DAN_MAX,DAN_MIN));
            gun.atk=1;
            cout<<"枪里有"<<gun.full<<"发实弹  "<<gun._empty<<"发空弹"<<endl;
            system("pause");
            p1.huitime=1;
            p2.huitime=0;
            p1.stopper_used=false;
            p1.knife_used=false;
            p2.stopper_used=false;
            p2.knife_used=false;
            p1.sure=-1;
            p2.sure=-1;
            int num=random(MAX_PUSH_ITEM,MIN_PUSH_ITEM);
            p1.item.rand_push_much(num,p1.name);
            p2.item.rand_push_much(num,p2.name);
            round++;//装一次子弹算一个回合
            system("pause");
            system("cls");
        }
        if(wujin)cout<<"level:"<<level<<endl;
        cout<<"round:"<<round<<endl;
        cout<<"-----"<<p1.name<<"的回合-----\n";
        p1.print_hp();
        cout<<endl<<endl;
        p1.item.print(p1.name);
        cout<<endl<<endl<<endl;
        p2.print_hp();
        cout<<endl<<endl;
        p2.item.print(p2.name);
        cout<<endl<<endl<<endl;
        cout<<"枪里还有"<<gun.top<<"发子弹"<<endl;
        cout<<"实弹:"<<gun.full<<" 空弹:"<<gun._empty<<endl;
        if(p1.ai){
            int _type=p1.AI(p2);
            compute(p1,p2,_type);

            if(_type>=1&&_type<=ITEM_TYPE)p1.item.pop(p1.item.bound(_type));
        }
        else {
            cout<<"你要干什么?-1打自己,-2打对方,1~"<<p1.item.top<<"用物品\n";
            int pl;
            cin>>pl;
            int _type=p1.item.type[pl];
            if(pl!=-1&&pl!=-2)compute(p1,p2,_type);
            else compute(p1,p2,pl);
            if(pl!=-1&&pl!=-2)p1.item.pop(pl);
        }
        if(p1.hp>=MAX_HP)p1.hp=MAX_HP;
        if(p2.hp>=MAX_HP)p2.hp=MAX_HP;
        gun.compute();
        system("pause");
        system("cls");
    }
    return 0;
}