jeudi 17 septembre 2020

I have some problems about this project?

I have 3 Robot Types: Alpha, Beta and Gamma. Generate random 3 Robots from mentioned type, and try to combine them into Megatron, with each combination have different attributes:

  1. Alpha + Gamma: totalHP * 2
  2. Gamma + Beta: totalHP
  3. Alpha + Beta: totalHP * 3
  4. Alpha + Beta + Gamma: all attribute * 7
  5. Same type: no change

Eg: Gamma + Alpha + Beta: (4.), Beta + Beta + Beta: (5.)
Currently I combine them by using a lot of if cases, are there any design patterns to avoid this, I have researched a bit about Abstract Factory and Builder, but non of them quite meet the requirements.

class Robot {
protected:
    int HP;
    int ATK;
    string name;
public:
    virtual string getName() = 0;
    int getHP() {
        return this->HP;
    }
};

class Alpha :public Robot {
protected:
    string name = "Alpha";
public:
    string getName() {
        return this->name = "Alpha";
    }
};

//Alpha, Beta and Gamma class is the same for the sake of simplicity

class Megatron :public Robot {
protected:
    string name = "Mega";
public:
    string getName() {
        return this->name;
    }
    Megatron(Robot& a, Robot& b, Robot& c) {
        if (a.getName() != b.getName() && b.getName() != c.getName()) {
            //*7
        }
        else if (a.getName() == b.getName() && b.getName() == c.getName()) {
            //no change
        }
        else {
            //lots of if cases
        }
    }
};

Aucun commentaire:

Enregistrer un commentaire