Main Program main.cpp
#include<iostream>
#include<string>
#include<time.h>
#include"Land.h"
#include"Cost.h"
#include"Creature.h"
#include"Enchantment.h"
using namespace std;
int main() {
srand((unsigned)time(0));
Land l1("black", "snow covered swamp");
Land l2("blue", "island");
l1.print();
// for the following array, default constructor is called 5 times
// let’s randomly initialize land cards in the default constructor srand called at the beginning of the code
Land l3[5];
// cost object: we need total 3 manas, one of them must be
// red mana (the last one) color of other two manas are unimportant
// order of the parameters: total, white, blue, black, green, red
Cost *cost = new Cost(3,0,0,0,0,1);
// parameters : name, color, ap, hp, cost, description-flavor text
Creature c1("anaba shaman", "red", 2, 2, cost, "just try taking this bull by the horns.");
delete cost;
cost = new Cost(5,1,0,0,0,0);
Creature c2("angel of mercy","white",3,3, cost,"when angel of mercy enters the battlefield, you gain 3 life.");
delete cost;
cost = new Cost(1,0,0,0,0,1);
Enchantment e("firebreathing", "red", 1,0, cost,"enchanted creature gets +1/+0 until end of turn.");
// when a creature is enchanted, update its attack power and hit point
if (c2.canEnchantable(e)){
cout << e.getName() << " is enchantable for " << c2.getName() << endl;
c2 + e;
} else if (c1.canEnchantable(e)){
cout << e.getName() << " is not enchantable for " << c2.getName() << endl;
cout << e.getName() << " is enchantable for " << c1.getName() << endl;
c1 + e;
} else
cout << e.getName() << " can’t be enchanted to any creature!" << endl;
if (c1.isAffordable(l3,5))
// if you look closely, you can see that 5 is size :)
cout << c1.getName() << " is affordable according to land deck l3 " << endl;
if (e.isAffordable(l3,5))
cout << e.getName() << " is affordable according to land deck l3" << endl;
// show time
Land *l4[8];
for (int i=0; i<5; i++)
l4[i]=&l3[i];
l4[5]=&c1;
l4[6]=&c2;
l4[7]=&e;
for (int i=0; i<8; i++)
l4[i]->print();
getchar();
return 0;
}
Land Base class cpp file Land.cpp
#include<iostream>
#include<string>
#include "Land.h"
using namespace std;
Land::Land()//default constructor
{
int random = rand()%5+1;
switch(random){
case 1:
name = "Plains";
color = "white";
break;
case 2:
name = "Island";
color = "blue";
break;
case 3:
name = "Swamp";
color = "black";
break;
case 4 :
name = "Forest";
color = "green";
break;
case 5:
name = "Mountain";
color = "red";
break;
}
}
Land::Land(const string &s1,const string &s2)
{
name = s1;
color = s2;
}
void Land::print()const
{
cout << "Name : "<< name << " Color : "<< color << endl;
}
Land Base class header file Land.h
#ifndef LAND_H
#define LAND_H
#include<string>
#include<time.h>
#include<cstdlib>
using namespace std;
class Land{//Base Class
private :
string name;
string color;
public :
Land();//default constructor
Land(const string &,const string &);//copy constructor
string getname()const {return name;}//return name variable
string getcolor()const {return color;}//return color variable
virtual void print()const;//virtual function from polymorphism
};
#endif
Creature Derived Class header file Creature.h
#ifndef CREATURE_H
#define CREATURE_H
#include<string>
#include "Enchantment.h"
#include "Land.h"
#include "Cost.h"
using namespace std;
class Creature: public Land //public inhertance base class is Land
{
private :
int ap;//attack point
int hp;//hit point
Cost *c1; //Cost pointer
string description;//description of creature
public :
Creature(const string &,const string &,int AP,int,Cost *,const string &);//Constructor
bool canEnchantable(const Enchantment &);//to control is Creature's color is useful for Land
string getName()const{return Land::getname();}//return name variable
bool isAffordable(const Land *,int);//to control is mana enough for Creature
void print()const; //virtual function from polymorphism
void operator+(const Enchantment &); //operator overloading
~Creature(){delete c1;}//Destructor
};
#endif
Creature Derived Class cpp file Creature.cpp
#include<iostream>
#include<string>
#include "Creature.h"
using namespace std;
Creature::Creature(const string &s1,const string &s2,int AP,int HP,Cost *cost,const string &s3):Land(s1,s2)
{
ap = AP;
hp = HP;
c1 = new Cost(cost->getTotal(),cost->getWhite(),cost->getBlue(),cost->getBlack(),cost->getGreen(),cost->getRed());
description = s3;
}
bool Creature::canEnchantable(const Enchantment &e)
{
if(Land::getcolor().compare(e.getColor())==0)//compare function in string library return integer value
return true;
else
return false;
}
void Creature::print()const //virtual function from polymorphism
{
Land::print(); // virtual base print function
cout << "Attack Point : " << ap << "\n"
<< "Hit Point : " << hp << "\n"
<< "Total Mana : " << c1->getTotal() << "\n"
<< Land::getcolor() <<" Mana : " << c1->getmana() << "\n"
<< description << endl;
}
void Creature::operator+(const Enchantment &e) //operator overloading
{
ap = ap + e.getAp();
hp = hp + e.getHp();
}
bool Creature::isAffordable(const Land *l,int size)
{ //Mana Array's size = 5 and it includes number of mana colors
int manas[5] = {0,0,0,0,0};//White,Blue,Black,Green,Red
if(c1->getTotal()>size)
return false;
else{
for(int i = 0;i<size;i++){
if(l[i].getcolor().compare("white")==0)
//increment number of White Mana
manas[0]++;
if(l[i].getcolor().compare("blue")==0)
//increment number of Blue Mana
manas[1]++;
if(l[i].getcolor().compare("black")==0)
//increment number of Black Mana
manas[2]++;
if(l[i].getcolor().compare("green")==0)
//increment number of Green Mana
manas[3]++;
if(l[i].getcolor().compare("red")==0)
//increment number of Red Mana
manas[4]++;
}
if(Land::getcolor().compare("white")==0){
if(c1->getWhite() <= manas[0])
return true;
}else if(Land::getcolor().compare("blue")==0){
if(c1->getBlue() <= manas[1])
return true;
}else if(Land::getcolor().compare("black")==0){
if(c1->getBlack() <= manas[2])
return true;
}else if(Land::getcolor().compare("green")==0){
if(c1->getGreen() <= manas[3])
return true;
}else if(Land::getcolor().compare("red")==0){
if(c1->getRed() <= manas[4])
return true;
}
return false;
}
}
Enchantment Derived Class header file Enchantment.h
#ifndef ENCHANTMENT_H
#define ENCHANTMENT_H
#include<string>
#include "Cost.h"
#include "Land.h"
using namespace std;
class Enchantment : public Land //public inhertance base class is Land
{
private :
int inc_ap;//increment atack point
int inc_hp;//increment hit point;
string description;//description of enchantment
Cost *c2;//Cost pointer
public :
Enchantment(const string &,const string &,int AP,int HP,Cost *,const string &);//Constructor
string getColor()const{return Land::getcolor();}//get method to access private member called color in base class Land
string getName()const{return Land::getname();}//get method to access private member called name in base class Land
bool isAffordable(const Land *,int);//To control is mana enough for Enchantment
void print()const; //virtual function from polymorphism
int getAp()const {return inc_ap;}//get method to access private inc_ap
int getHp()const {return inc_hp;}//get method to access private inc_hp
~Enchantment(){delete c2;}//destructor
};
#endif
Enchantment Derived Class cpp file Enchantment.cpp
#include<iostream>
#include<string>
#include "Enchantment.h"
using namespace std;
bool Enchantment::isAffordable(const Land *l,int size)
{
int manas[5] = {0,0,0,0,0}; //White,Blue,Black,Green,Red
if(c2->getTotal()>size)
return false;
else{
for(int i = 0;i<size;i++){
if(l[i].getcolor().compare("white")==0)
//increment number of White Mana
manas[0]++;
if(l[i].getcolor().compare("blue")==0)
//increment number of White Mana
manas[1]++;
if(l[i].getcolor().compare("black")==0)
//increment number of White Mana
manas[2]++;
if(l[i].getcolor().compare("green")==0)
//increment number of White Mana
manas[3]++;
if(l[i].getcolor().compare("red")==0)
//increment number of White Mana
manas[4]++;
}
if(Land::getcolor().compare("white")==0){
if(c2->getWhite() <= manas[0])
return true;
}else if(Land::getcolor().compare("blue")==0){
if(c2->getBlue() <= manas[1])
return true;
}else if(Land::getcolor().compare("black")==0){
if(c2->getBlack() <= manas[2])
return true;
}else if(Land::getcolor().compare("green")==0){
if(c2->getGreen() <= manas[3])
return true;
}else if(Land::getcolor().compare("red")==0){
if(c2->getRed() <= manas[4])
return true;
}
return false;
}
}
void Enchantment::print()const //virtual function from polymorphism
{
Land::print();//virtual base print function
cout << "Increment Attack Point by : " << inc_ap << "\n"
<< "Increment Hit Point by : " << inc_hp << "\n"
<< "Total Mana : " << c2->getTotal() << "\n"
<< Land::getcolor() << " Mana : " << c2->getmana() << "\n"
<< description << endl;
}
Enchantment::Enchantment(const string &s,const string &s2,int AP,int HP,Cost *cost,const string &s3):Land(s,s2)
{
// Constructor
inc_ap = AP;
inc_hp = HP;
c2 = new Cost(cost->getTotal(),cost->getWhite(),cost->getBlue(),cost->getBlack(),cost->getGreen(),cost->getRed());
description = s3;
}
Cost Class header file Cost.h
#ifndef COST_H
#define COST_H
using namespace std;
class Cost{
private :
int Total;
int White;
int Blue;
int Black;
int Green;
int Red;
public :
Cost(int,int,int,int,int,int);
int getmana()const;
int getTotal()const {return Total;}
int getWhite()const {return White;}
int getBlue()const {return Blue;}
int getBlack()const {return Black;}
int getGreen()const {return Green;}
int getRed()const {return Red;}
};
#endif
Cost Class cpp file Cost.cpp
#include<iostream>
#include<string>
#include "Cost.h"
using namespace std;
Cost::Cost(int total,int white,int blue,int black,int green,int red)
{
Total = total;
White = white;
Blue = blue;
Black = black;
Green = green;
Red = red;
}
int Cost::getmana()const
{
if(White!=0)
return White;
else if(Blue!=0)
return Blue;
else if(Black!=0)
return Black;
else if(Green!=0)
return Green;
else if(Red!=0)
return Red;
else
return 0;
}
OUTPUT