Postscript version

CEng 242 Homework 5
Due: 12nd May 2000

In this homework, you will write a class library for a very simple strategy game. In this game you have players which may have ability to fight and/or cast spells. They start with an initial strength and mana (ability to cast spell) power and they can collect some tools to increase their powers. They have a standart attacking strategy. And a standart one-on-one fight procedure is defined between two players.

In the implemantation you should have a base class Player with the following definition:

#define MAXATTACK 4
#define MAXSPELL 4

struct Move {
   unsigned hit;
   unsigned cast;
};

class Player {
protected:
   char name[40];
public:
   Player(char *s) { strncpy(name,s,40);name[39]='\0';}
   virtual ~Player () { cout << name << " is death!\n";}
   const char *getname() { return name;}
   virtual unsigned strength()=0;
   virtual unsigned mana()=0;
   virtual void operator++() {};
   virtual void operator--() {};
   virtual unsigned loosestrength(unsigned i) {return i;};
   virtual unsigned loosemana(unsigned i) {return i;};
   Move attack();
   void operator*(Player &);
   friend ostream & operator<< (ostream &,Player &);
};

You are not allowed to change the definition of class Player. You should only implement the unimplemented functions. Then you should derive the following classes directly or indirectly from players.

All players should have the constructors implemented with a single char * parameter which stands for the name of the player and should be passed to the constructor of Player class. All member definitions in Player class should be completed-implemented so they can be usable from any player instance.

Also you will define an Army structure with the following definition:

struct Army {
   char name[40];
   Player *soldiers[100];
   int n;

   Army(char *s) { strncpy(name,s,40);name[39]='\0';n=0;}
   ~Army();
   void operator+=(Player *p) { soldiers[n++]=p;}
   void operator-=(unsigned);
   void operator-=(Player *);
   Player & operator[](int i) { return(*soldiers[i]); }
};

name is the name of the army. soldiers keeps the pointers of the army members. Army contains n soldiers. So first n element of the soldiers array is used. += request is used to add a new Player to the army. For example:

Army a("Kifirbizan");
a += new Warior("Hosterbulgan");
a += new Darklord("Hinzurgabon");
a += new Peasant("Paspason");
a += new Wizard("Hokustonkapus");

Will define the army Kifirbizan consisting of 4 soldiers with all different types. You have to overload the -= i operator for an unsigned integer to delete the ith soldier from the soldiers array and -= p for a Player pointer to match and delete the player with pointer p (you can call the integer version to delete). In deletion you should shift the pointers up to fill the deleted position. Note that used elements of the array soldiers are always in the range [0..n-1]. Also you should deallocate the death players memory. Also overload the operator ostream &operator<<(ostream &,Army &) to list all soldiers in the army in the following format:

Army: Kifirbizan
  Hosterbulgan 5 0
  Hinzurgabon 5 5
  Paspason 1 0
  Hokustonkapus 0 5

When your implementation is finished, the following war procedure should work for two armies A and B:

int ia=0,ib=0,count=0;

while (count<1000000L && A.n && B.n) { // While no army is defated cout << A[ia] << B[ib]; try { A[ia]*B[ib]; } // Two soldiers fight catch (Player *p) { // p is death if (p==A.soldiers[ia]) { // if soldier of A A-=ia; // delete the death soldier from army A ia-; // correct ia since soldiers are shifted } else { B-=ib; // delete the death soldier from army B ib-; // correct ib since soldiers are shifted } } ia++; if (ia>=A.n) ia=0; ib++; if (ib>=B.n) ib=0; count++; // to avoid infinite loop of two powerless soldiers }

Submission details will be announced later.