红色警戒c++

#include <iostream>
#include <string>
#include<list>
#include<sstream>
using namespace std;
class tank
{
private:
    int maxlife = 0;//最大生命
    int attack=0;//攻击力,攻击方式会用一个函数处理
    int defense=0;//防御
    int life=0;//生命值
    string name;//名字
    bool transform = false;//是否变形
    string plus;//升级的后缀
public:
    tank(int a, int b, int c, string d)
    {
        maxlife = c;
        attack = a;
        defense = b;
        life = c;
        name = d;
    }//坦克的构造函数,用于造坦克。
    int attackk()
    {
        return attack;
    }//获取攻击力
    void renew()
    {
        transform = false;
    }//回合结束后变形冷却
    bool alive()
    {
        if (life <= 0)return false;
        else return true;
    }//判断是否返回基地回复
    string nam()
    {
        return name;
    }//读取名字
    string andplus()
    {
        return plus;
    }//加上升星后缀
    void cure()
    {
        if (life < maxlife)
        {
            life += 4;
        }
        if (life >= maxlife)
        {
            life = maxlife;
        }
    }//每个单位时间回4点血
    void addplus()
    {
        plus = "*";
        attack += 5;
        defense += 5;
    }//可以升星了
    void attacked(int x,string attacker)
    {
        if (name == "Bear")life -= x-defense;
        if (name == "Phantom")
        {
            if (transform)
            {
                transform = false; return;
            }
            else {
                transform = true;
                life -= x-defense;
            }
        }
        if (name == "Prism")life -= x-defense;
        if (name == "Apocalypse")
        {
            if (attacker == "Prism")
            {
                life -= x  * 8 / 10;
                return;
            }
            else
            {
                life -= (x - defense) * 8 / 10;
                return;
            }
        }
        if (attacker == "Prism")life -= defense;
    }//被攻击,输入值为攻击力以及攻击者名字用来判断如何扣血
};
//我方基地class的封装
class center {
    int money=0;//总经济
    list<tank> fighter;//用来装tank
    int time = 0;//计算制造时间,后面输出到主函数中
    int invaded = 0;//预计被侵入时间
    int star = 0;//间谍星级
    int plustime = 0;//预计间谍进入地方大本营时间
public:
    int thept()
    {
        return plustime;
    }//输出偷取升星秘籍时间
    int thestar()
    {
        return star;
    }//输出星级
    void addplus()
    {
        list<tank>::iterator p = fighter.begin();
        for (; p != fighter.end(); p++)
        {
            p->addplus();
        }
    }//用迭代器遍历仍然在基地的tank并调用升星函数升星
    void cure()
    {
        list<tank>::iterator p = fighter.begin();
        for (; p != fighter.end(); p++)
        {
            p->cure();
        }
    }//回复血量
    void outcount1()
    {
        outcount2();
        if (fighter.size() != 0)
        {
            cout << '.';
        }
    }//可作为结尾,输出基地内所有坦克
    void outcount2()
    {
        list<tank>::iterator p = fighter.begin();
        for (int x = 0; x < fighter.size(); x++, p++)
        {
            cout << " ";
            cout << p->nam() << p->andplus();
        }
    }//输出基地内所有坦克
    void add(tank &a)
    {
        fighter.push_back(a);
    }//出生的tank和回复的tank补刀队尾
    tank fight()
    {
        tank temper = *fighter.begin();
        fighter.pop_front();
        return temper;
    }//坦克出发,出栈
    int outtime()
    {
        return time;
    }//将初步计算后的时间输出
    bool exist()
    {
        return (fighter.size() > 0);
    }//基地内还有坦克吗
    void create()
    {
        int turn = 1;
        while(money>=600&&(invaded-time)>=1)
        {
            if (turn == 1)
            {
                if (money >= 600)
                {
                    fighter.push_back(tank(30, 15, 100, "Bear"));
                    money -= 600;
                    turn++;
                    time += 1;
                    continue;
                }
                else return;
            }
            if (turn == 2)
            {
                if (money >= 1000)
                {
                    if ((invaded - time) < 2)
                    {
                        return;
                    }
                    fighter.push_back(tank(40, 15, 100, "Phantom"));
                    money -= 1000;
                    turn++;
                    time += 2;
                    continue;
                }
                else { turn = 1; continue; }
            }
            if (turn == 3)
            {
                if (money >= 1200)
                {
                    if ((invaded - time) < 4)
                    {
                        return;
                    }
                    fighter.push_back(tank(50, 10, 100, "Prism"));
                    money -= 1200;
                    turn++;
                    time += 4;
                    continue;
                }
                else { turn = 1; continue; }
            }
            if (turn == 4)
            {
                if (money >= 1750)
                {
                    if ((invaded - time) < 6)
                    {
                        return;
                    }
                    fighter.push_back(tank(60, 20, 120, "Apocalypse"));
                    money -= 1750;
                    turn = 1;
                    time += 6;
                    continue;
                }
                else { turn = 1; continue; }
            }
        }
    }//开始制造,造不下去直接停止
    center(int x,int c,int s)
    {
        star = s;
        if (star == 3)
        {
            plustime = c + 5;
        }
        else if (star == 2)
        {
            plustime = c + 20;
        }
        else if (star == 1)
        {
            plustime = c + 40;
        }
        invaded = c;
        money = x;
        if (money >= 600)
        {
            create();
        }
        else return;
        if (money >= 600)
        {
            if (time <= invaded)
            {
                time = invaded;
                cout << "Time:" << time << " Invaded by enemy spies ";
            }
        }
        if (money < 600)
        {
            if (time < invaded)
            {
                cout << "Time:" << time << " Money is empty ";
            }
            if (time ==invaded)
            {
                cout << "Time:" << time << " Money is empty, Invaded by enemy spies ";
            }
        }
        cout << "and production stalled,";
        list<tank>::iterator p = fighter.begin();//用来装tank
        for (;p!=fighter.end();p++)
        {
            cout << ' ' << p->nam();
        }
        cout<<'.' << endl;
        if (money < 600)
        {
            if (time < invaded)
            {
                time = invaded;
                cout << "Time:" << time;
                cout << " Invaded by enemy spies." << endl;
                return;
            }
        }
        time = invaded;
    }//确定定义的一系列数值,包括对先没钱还是先被侵入的判断
};
class enemy
{
    list<tank> fighter;
public:
    void cure()
    {
        list<tank>::iterator p = fighter.begin();
        for (; p != fighter.end(); p++)
        {
            p->cure();
        }
    }
    void add(tank& a)
    {
        fighter.push_back(a);
    }
    tank fight()
    {
        tank temper = *fighter.begin();
        fighter.pop_front();
        return temper;
    }
    bool exist()
    {
        return (fighter.size() > 0);
    }
    void in(string x)
    {
        if (x == "Bear")
        {
            fighter.push_back(tank(30, 15, 100, "Bear"));
        }
        if (x == "Phantom")
        {
            fighter.push_back(tank(40, 15, 100, "Phantom"));
        }
        if (x == "Prism")
        {
            fighter.push_back(tank(50, 10, 100, "Prism"));
        }
        if (x == "Apocalypse")
        {
            fighter.push_back(tank(60, 20, 120, "Apocalypse"));
        }
    }
};//地方大本营,基本所有内容与center对应

void outime(double x)
{
    cout << "Time:" << x << " ";
}//输出时间

void instarwithoutendl(center& my, int time)
{
    if (my.thept() == time)
    {
        outime(time * 1.0 + 0.5);
        cout << "Spy successfully steal enemy tank technology";
        if (my.exist())
        {
            cout << ',';
            my.outcount2();
            cout << " will be upgraded!" << endl;
            my.addplus();
        }
        else
        {
            cout << '.' << endl;
        }
    }
}
void instarwithendl(center& my, int time){
    if (my.thept() == time)
    {
        cout << endl;
        outime(time * 1.0 + 0.5);
        cout << "Spy successfully steal enemy tank technology";
        if (my.exist())
        {
            cout << ',';
            my.outcount2();
            cout << " will be upgraded!";
            my.addplus();
        }
        else
        {
            cout << '.';
        }
    }
}
void service(center &my, enemy&enemy,int &time)
{
    for (int i = 0; i < 5; i++)
    {
        my.cure();
        enemy.cure();
        time++;
        if (i != 4)instarwithendl(my, time);
    }
}//不断回血五次,用于在路上的时间

bool afterfight(center& my, enemy& enemy, int& time, int& round,tank& mytank,tank&youtank)
{
    if (mytank.alive())//己方胜利了吗
    {
        if (enemy.exist())//敌方还有吗
        {
            outime(time);
            cout << "Round " << round << " End, You won the encounter!";
            my.outcount1();
            if (my.exist())//己方还有吗
            {
                tank temp = mytank;
                mytank = my.fight(); youtank = enemy.fight();//都有就都派出新坦克
                instarwithendl(my, time);
                service(my, enemy, time);
                my.add(temp);//旧坦克最后时刻才到
            }
            else
            {
                youtank = enemy.fight();//己方没有只有对方派出
                instarwithendl(my, time);
                service(my, enemy, time);
            }
        }
        else
        {
            outime(time);
            cout << "Round " << round << " End, You won the encounter!";
            my.outcount1();
            cout << " You won the war!" << endl;;
            return true;
        }//敌方没坦克了我直接获胜
    }
    else
    if (youtank.alive())
    {
        if (my.exist())
        {
            outime(time);
            cout << "Round " << round << " End, You lost the encounter!";
            my.outcount1();
            if (enemy.exist())
            {
                tank temp = youtank;
                mytank = my.fight(); youtank = enemy.fight();//双方都有坦克就都派出新的
                instarwithendl(my, time);
                service(my, enemy, time);
                enemy.add(temp);
            }
            else
            {
                mytank = my.fight();
                instarwithendl(my, time);
                service(my, enemy, time);
            }
        }
        else//己方没有坦克了直接失败
        {
            outime(time);
            cout << "Round " << round << " End, You lost the encounter!";
            my.outcount1();
            cout << " You lost the war!" << endl;
            return true;
        }
    }
    else//同归于尽了
    {
        if (my.exist())
        {
            if (enemy.exist())
            {
                outime(time);
                cout << "Round " << round << " End, Deadlock!";
                my.outcount1();
                mytank = my.fight(); youtank = enemy.fight();//大家都有就派出新的
                instarwithendl(my, time);
                service(my, enemy, time);
            }
            else {
                outime(time);
                cout << "Round " << round << " End, Deadlock!";
                my.outcount1();
                cout << " You won the war!" << endl;;
                return true;
            }
        }
        else if (enemy.exist())
        {
            outime(time);
            cout << "Round " << round << " End, Deadlock!";
            my.outcount1();
            cout << " You lose the war!" << endl; ;
            return true;
        }
        else {
            outime(time);
            cout << "Round " << round << " End, Deadlock!";
            my.outcount1();
            cout << " It ended in a draw!" << endl;;
            return true;
        }
    }
    return false;
}

int main()
{
    int n;
    string a;
    int ctime=0;
    int star;
    cin >> n;
    enemy enemy;
    while (cin >> a)
    {
        if (a == "Bear" || a == "Phantom" || a == "Prism" || a == "Apocalypse")enemy.in(a);
        else
        {
            istringstream is(a);//网上找的一个算法,把string转化为int
            is >> ctime;
            break;
        }
    }
    cin >> star;
    center my(n,ctime,star);
    int round = 1;
    int time = my.outtime();
    if (my.exist()) {}
    else
    {
        outime(time);
        cout << "You lost the war!" << endl;
        return 0;
    }//先判断我方是否造出tank
    time += 5;
    tank mytank = my.fight(), youtank = enemy.fight();
    while (1)
    {
        outime(time);
        cout << "Round " << round << " Start, ";
        cout << mytank.nam() <<mytank.andplus()<< " vs " << youtank.nam()<<youtank.andplus()<< '.' << endl;
        instarwithoutendl(my, time);
        if (mytank.nam() == "Phantom")mytank.renew();
        if (youtank.nam() == "Phantom")youtank.renew();
        while (mytank.alive() && youtank.alive())
        {
            mytank.attacked(youtank.attackk(), youtank.nam());
            youtank.attacked(mytank.attackk(), mytank.nam());
            my.cure(); enemy.cure();
            time++;
            if (mytank.alive() && youtank.alive())instarwithoutendl(my, time);
        }
        bool judge=afterfight(my, enemy, time, round, mytank, youtank);
        if (judge)return 0;
        cout << endl;
        round++;
    }
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值