魔兽世界之一备战

#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int dHP, iHP, lHP, nHP, wHP;    // 全局魔兽初始生命

/* class: 游戏钟表 */
class Clock
{
public:
    Clock(){ hour = 0; }
    void tick()
    {
        hour++;
    }
    void printHour()
    {
        cout << setfill('0') << setw(3) << hour;
    }
    void reset()
    {
        hour = 0;
    }
private:
    int hour;
}CLOCK;                         // 全局游戏时钟

/* class:司令部 */
class Headquater
{
public:
    Headquater(string color_, int M)
    {
        color = color_;
        elements = M;
        nWarrior = nNinja = nIceman = nWolf = nLion = nDragon = 0;
        stop_producing = false;
        produce_peek = 0;            
    }
    int elements;
    string color;
    bool stop_producing;
    void produceWarrior();
private:
    int nNinja;
    int nIceman;
    int nWolf;
    int nLion;
    int nDragon;
    bool producePeek(int peek);             // 生产武士帧为peek的武士
    bool produceIceman();                   // 生产一个Iceman,成功返回true,下同
    bool produceNinja();                    // 对五个生产函数进行一点说明,首先,这五个函数完全可以由一个
    bool produceDragon();                   // 接受参数的函数代替,在此没有节省这点代码量,主要是因为:
    bool produceLion();                     // 1.这个游戏到后期怪兽的异质性很强,共性很弱,函数总会拆离
    bool produceWolf();                     // 2.在一定情况下可方便地为其他函数提供好的接口,这一点在游戏初期不明显
    int produce_peek;                       // 记录当前应该生产武士的“指针”(武士帧),构造函数中初始为0
    int nWarrior;
};

/* produceWarrior的实现 */
void Headquater::produceWarrior()
{
    bool has_produced = false;              // 标记是否生产武士
    for(int i = produce_peek; i < 5; i++)   // 从当前武士帧枚举至末,满足条件便生产武士,武士帧更新
    {
        has_produced = producePeek(i);  
        if(has_produced)                
        {
            produce_peek = i + 1;
            break;
        }
    }
    if(!has_produced)                       // 若无法生产,则从前枚举到武士帧
    {
        for(int i = 0; i < produce_peek; i++)
        {
            has_produced = producePeek(i);
            if(has_produced)
            {
                produce_peek = i + 1;
                break;
            }
        }
    }
    if(!has_produced)                       // 断定无法生产武士,司令部停产,输出停产信息
    {
        stop_producing = true;
        CLOCK.printHour();
        cout << " " << color << " headquarter stops making warriors" << endl;
    }
    else                                    // 共五个武士,让指向5的武士帧指向1
    {
        if(produce_peek >= 5)
            produce_peek = 0;
    }
}

bool Headquater::producePeek(int peek)
{
    if(color == "red")                      // 在函数执行中书写武士生产顺序,以color区别不同的顺序
    {
        switch(peek)
        {
            case 0: return produceIceman();
            case 1: return produceLion();
            case 2: return produceWolf();
            case 3: return produceNinja();
            case 4: return produceDragon();
            default: return false;
        }
    }
    if(color == "blue")
    {
        switch(peek)
        {
            case 0: return produceLion();
            case 1: return produceDragon();
            case 2: return produceNinja();
            case 3: return produceIceman();
            case 4: return produceWolf();
            default: return false;
        }
    }
}

bool Headquater::produceIceman()
{
    if(elements >= iHP)
    {
        nWarrior++;
        nIceman++;
        elements -= iHP;
        CLOCK.printHour();
        cout << " " << color << " iceman "  << nWarrior 
        <<  " born with strength " << iHP << "," << nIceman << " iceman in " << color << " headquarter" << endl;
        return true;
    }
    else return false;
}
bool Headquater::produceWolf()
{
    if(elements >= wHP)
    {
        nWarrior++;
        nWolf++;
        elements -= wHP;
        CLOCK.printHour();
        cout << " " << color << " wolf "  << nWarrior 
        <<  " born with strength " << wHP << "," << nWolf << " wolf in " << color << " headquarter" << endl;
        return true;
    }
    else return false;
}
bool Headquater::produceLion()
{
    if(elements >= lHP)
    {
        nWarrior++;
        elements -= lHP;
        nLion++;
        CLOCK.printHour();
        cout << " " << color << " lion "  << nWarrior 
        <<  " born with strength " << lHP << "," << nLion << " lion in " << color << " headquarter" << endl;
        return true;
    }
    else return false;
}
bool Headquater::produceNinja()
{
    if(elements >= nHP)
    {
        elements -= nHP;
        nWarrior++;
        nNinja++;
        CLOCK.printHour();
        cout << " " << color << " ninja "  << nWarrior
        <<  " born with strength " << nHP << "," << nNinja << " ninja in " << color << " headquarter" << endl;
        return true;
    }
    else return false;
}
bool Headquater::produceDragon()
{
    if(elements >= dHP)
    {
        elements -= dHP;
        nWarrior++;
        nDragon++;
        CLOCK.printHour();
        cout << " " << color << " dragon "  << nWarrior 
        <<  " born with strength " << dHP << "," << nDragon << " dragon in " << color << " headquarter" << endl;
        return true;
    }
    else return false;
}

int main()
{
    int nTest;
    cin >> nTest;
    for(int test = 1; test <= nTest; test++)
    {
        CLOCK.reset();
        int M;
        cin >> M;
        cin >> dHP >> nHP >> iHP >> lHP >> wHP;
        Headquater red("red", M);
        Headquater blue("blue", M);
        cout << "Case:" << test << endl;
        /* 以上,初始化战场模型,数据 */

        while(true)
        {
            if(red.stop_producing && blue.stop_producing)   // 蓝军红军停产,模拟结束
            {
                break;
            }
            if(!red.stop_producing)                         
            {
                red.produceWarrior();
            }
            if(!blue.stop_producing)
            {
                blue.produceWarrior();
            }
            CLOCK.tick();                                   // 游戏钟表跳
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值