Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***|***|****|
- Program: main.cpp / Robot.h
- Course: OOPDS
- Name: MUHAMMAD AQIL BIN RAHMAT
- ID: 1211107976
- Lecture Section: TC4L
- Tutorial Section: T13L
- Email: 1211107976@student.mmu.edu.my
- Phone: 019-8148313
- Name: TENG CHAY XUAN
- ID: 1231300802
- Lecture Section: TC4L
- Tutorial Section: T13L
- Email: 1231300802@student.mmu.edu.my
- Phone: 011-10588063
- Name: Akid Syazwan bin Nor Azman Shah
- ID: 1211111238
- Lecture Section: TC4L
- Tutorial Section: T13L
- Email: 121111123@student.mmu.edu.my
- Phone: 014-9020901
- Name: MUHAMMAD NABIL NAUFAL BIN MD ZAID
- ID: 1221101160
- Lecture Section: TC4L
- Tutorial Section: T13L
- Email: 1221101160@student.mmu.edu.my
- Phone: 010-5509230
- ***|***|****/
- #pragma once
- #include <bits/stdc++.h>
- using namespace std;
- //Movement
- class Robot {
- public:
- virtual void setPos(int x, int y){}
- virtual string getType(){ return {};}
- virtual string getName(){ return {};}
- virtual char getLabel(){ return {};}
- virtual int getPosX(){ return {};}
- virtual int getPosY(){ return {};}
- virtual bool getAlive(){ return {};}
- virtual int getLives(){ return {};}
- virtual void respawned(){}
- virtual void died(){}
- virtual void minusLife(){}
- virtual int getKills(){ return {};}
- virtual void kill(){}
- void checkAlive(char (*Bfield), int m, int n)
- {
- char label = getLabel();
- bool isAlive = getAlive();
- for(int i=0; i<m; i++)
- {
- for(int j=0; j<n; j++)
- {
- if (*((Bfield + i * n) + j*2) != label)
- {
- isAlive = false;
- }
- else
- {
- isAlive = true;
- return;
- }
- }
- }
- if(!isAlive)
- died();
- }
- virtual char move(char (*Bfield), int m, int n, ofstream &output) = 0;
- virtual char fire(char (*Bfield), int m, int n, ofstream &output) = 0;
- virtual char look(char (*Bfield), int m, int n, ofstream &output) = 0;
- virtual char step(char (*Bfield), int m, int n, int targetX, int targetY, ofstream &output) = 0;
- // Friend function to overload the << operator
- friend ostream& operator<<(ostream& os, Robot& robot);
- };
- // We overload the << operator to printout the name of the robot.
- ostream& operator<<(ostream& os, Robot& Robot)
- {
- os << "[" << Robot.getLabel() << "]" << "(" << Robot.getType() << ")" << Robot.getName();
- return os;
- }
- //--------------Robot Functions--------------------
- class MovingRobot : virtual public Robot {
- public:
- virtual char move(char (*Bfield), int m, int n, ofstream &output)
- {
- bool invalid = false;
- int PosX = getPosX();
- int PosY = getPosY();
- char label = getLabel();
- int moveX = 0;
- int moveY = 0;
- //loop to check if there are no valid movements
- for(int i=PosY-1; i<PosY+2; i++)
- {
- for(int j=PosX-1; j<PosX+2; j++)
- {
- //Not a single valid movement found for the robot
- if(*((Bfield + i * n) + j*2) != '-' && *((Bfield + i * n) + j*2) == label && j == 0 && j == m-1 && i == 0 && (i*2) == n)
- {
- invalid = true;
- cout << getName() << "[" << getType() << "] is stuck and cannot move!" << endl;
- output << getName() << "[" << getType() << "] is stuck and cannot move!" << endl;
- return *Bfield;
- }
- else //Any single valid movement found
- {
- break;
- }
- }
- }
- if(!invalid)
- {
- //set previous position to '-' to make empty
- *((Bfield + PosY * n) + PosX*2) = '-';
- //loop to get where the robot moves using rand
- do{
- moveX = -1 + (1-(-1)) * rand() % 3;
- moveY = -1 + (1-(-1)) * rand() % 3;
- }while((moveX == 0 && moveY == 0) || PosY + moveY == 0 || PosY + moveY == m-1 || *((Bfield + (PosY + moveY) * n) + (PosX + moveX)*2) != '-');
- }
- //add movex and movey to respective x and y positions to change robot position
- int newPosX = PosX + moveX;
- int newPosY = PosY + moveY;
- //move robot label to its position
- *((Bfield + newPosY * n) + newPosX*2) = label;
- cout << getName() << " at (" << PosX << "," << PosY << ") moved to (" << newPosX << "," << newPosY << ")" << endl;
- output << getName() << " at (" << PosX << "," << PosY << ") moved to (" << newPosX << "," << newPosY << ")" << endl;
- //change robot position
- setPos(newPosX, newPosY);
- return *Bfield;
- }
- virtual void nothing() = 0;
- };
- class ShootingRobot : virtual public Robot {
- public:
- virtual char fire(char (*Bfield), int m, int n, ofstream &output)
- {
- int PosX = getPosX();
- int PosY = getPosY();
- int shootCoordX, shootCoordY;
- int shootX = 0;
- int shootY = 0;
- while(shootX == 0 && shootY == 0 || abs(shootX)+abs(shootY) > 5 || *((Bfield + shootCoordY * n) + shootCoordX*2) == '*')
- {
- shootCoordX = rand() % (n-3)/2+1;
- shootCoordY = rand() % (m-2)+1;
- shootX = shootCoordX - PosX;
- shootY = shootCoordY - PosY;
- }
- if(*((Bfield + shootCoordY * n) + shootCoordX*2) != '-' && *((Bfield + shootCoordY * n) + shootCoordX*2) != '*')
- {
- cout << getName() << " hit another robot at (" << shootX << "," << shootY << ") from it's own position!" << endl;
- *((Bfield + shootCoordY * n) + shootCoordX*2) = '*';
- output << getName() << " hit another robot at (" << shootX << "," << shootY << ") from it's own position!" << endl;
- *((Bfield + shootCoordY * n) + shootCoordX*2) = '*';
- kill();
- return *Bfield;
- }
- cout << getName() << " hit nothing at (" << shootX << "," << shootY << ") from it's own position" << endl;
- output << getName() << " hit nothing at (" << shootX << "," << shootY << ") from it's own position" << endl;
- { return {};}
- }
- virtual void nothing() = 0;
- };
- class SteppingRobot : virtual public Robot {
- public:
- virtual char step(char (*Bfield), int m, int n, int targetX, int targetY, ofstream &output)
- {
- int PosX = getPosX();
- int PosY = getPosY();
- char label = getLabel();
- //Set previous position in battlefield to '-' to mark as empty
- *((Bfield + PosY * n) + PosX*2) = '-';
- PosX = targetX;
- PosY = targetY;
- //Change robot position
- setPos(PosX, PosY);
- //Move to new position in battlefield
- *((Bfield + PosY * n) + PosX*2) = label;
- cout << getName() << " found a target and stepped on another robot at (" << PosX << "," << PosY << ")!" << endl;
- output << getName() << " found a target and stepped on another robot at (" << PosX << "," << PosY << ")!" << endl;
- //Increase robot kill
- kill();
- return *Bfield;
- }
- virtual void nothing() = 0;
- };
- class SeeingRobot : virtual public Robot {
- public:
- virtual char look(char (*Bfield), int m, int n, ofstream &output)
- {
- int PosX = getPosX();
- int PosY = getPosY();
- char label = getLabel();
- bool target;
- int targetX, targetY;
- //loop to find any target 1 tile around the robot
- for(int i=PosY-1; i<PosY+2; i++)
- {
- for(int j=PosX-1; j<PosX+2; j++)
- {
- //if a target is found
- if(*((Bfield + i * n) + j*2) != '-' && *((Bfield + i * n) + j*2) != '|' && *((Bfield + i * n) + j*2) != '+' && *((Bfield + i * n) + j*2) != label && *((Bfield + i * n) + j*2) != '*')
- {
- target = true;
- targetX = j;
- targetY = i;
- step((char*)Bfield, m, n, targetX, targetY, output);
- return *Bfield;
- }
- }
- }
- move((char*)Bfield, m, n, output);
- { return {};}
- }
- virtual void nothing() = 0;
- };
- //-------------ROBOT TYPES-----------------
- ///RoboCop: Moves and Shoots - Upgrade: TerminatorRoboCop
- class RoboCop : public SeeingRobot, public MovingRobot, public ShootingRobot {
- private:
- string type, name;
- char label;
- int kills, lives;
- int PosX, PosY;
- bool alive;
- public:
- RoboCop(string Rname, char Rlabel, int Rlives, int x, int y)
- {
- type = "RoboCop";
- name = Rname;
- label = Rlabel;
- kills = 0;
- lives = Rlives;
- PosX = x;
- PosY = y;
- alive = true;
- }
- string getType(){return type;}
- string getName(){return name;}
- char getLabel(){return label;}
- int getPosX(){return PosX;}
- int getPosY(){return PosY;}
- bool getAlive(){return alive;}
- int getLives(){return lives;}
- void respawned(){alive = true;}
- void died(){alive = false;}
- void minusLife(){lives--;}
- int getKills(){return kills;}
- void kill(){kills++;}
- void nothing() {}
- void setPos(int x,int y)
- {
- PosX = x;
- PosY = y;
- }
- ~RoboCop(){cout << getName() << "[" << getType() << "] Destroyed and Scrapped";}
- char move(char (*Bfield), int m, int n, ofstream &output){
- return MovingRobot::move(Bfield,m,n,output);
- }
- char fire(char (*Bfield), int m, int n, ofstream &output){
- return ShootingRobot::fire(Bfield,m,n,output);
- }
- char look(char (*Bfield), int m, int n, ofstream &output){ return {};}
- char step(char (*Bfield), int m, int n, int targetX, int targetY, ofstream &output){ return {};}
- };
- ///Terminator: Looks and Moves - Upgrade: TerminatorRoboCop
- class Terminator : public SeeingRobot, public MovingRobot, public SteppingRobot {
- private:
- string type, name;
- char label;
- int kills, lives;
- int PosX, PosY;
- int targetX, targetY;
- bool alive;
- public:
- Terminator(string Rname, char Rlabel, int Rlives, int x, int y)
- {
- type = "Terminator";
- name = Rname;
- label = Rlabel;
- kills = 0;
- lives = Rlives;
- PosX = x;
- PosY = y;
- alive = true;
- }
- string getType(){return type;}
- string getName(){return name;}
- char getLabel(){return label;}
- int getPosX(){return PosX;}
- int getPosY(){return PosY;}
- bool getAlive(){return alive;}
- int getLives(){return lives;}
- void respawned(){alive = true;}
- void died(){alive = false;}
- void minusLife(){lives--;}
- void kill(){kills++;}
- int getKills(){return kills;}
- void nothing() {}
- void setPos(int x,int y)
- {
- PosX = x;
- PosY = y;
- }
- void setTarget(int x,int y)
- {
- targetX = x;
- targetY = y;
- }
- ~Terminator(){cout << getName() << "[" << getType() << "] Destroyed and Scrapped";}
- char move(char (*Bfield), int m, int n, ofstream &output){
- return MovingRobot::move(Bfield,m,n,output);
- }
- char fire(char (*Bfield), int m, int n, ofstream &output){ return {};}
- char look(char (*Bfield), int m, int n, ofstream &output){
- return SeeingRobot::look(Bfield,m,n,output);
- }
- char step(char (*Bfield), int m, int n, int targetX, int targetY, ofstream &output){
- return SteppingRobot::step(Bfield,m ,n, targetX, targetY,output);
- }
- };
- ///BlueThunder: Shoots - Upgrade: MadBot
- class BlueThunder : public ShootingRobot {
- private:
- string type, name;
- char label;
- int kills, lives;
- int PosX, PosY;
- bool alive;
- int currentDirection;
- public:
- BlueThunder(string Rname, char Rlabel, int Rlives, int x, int y)
- {
- type = "BlueThunder";
- name = Rname;
- label = Rlabel;
- kills = 0;
- lives = Rlives;
- PosX = x;
- PosY = y;
- alive = true;
- currentDirection = 0;
- }
- string getType(){return type;}
- string getName(){return name;}
- char getLabel(){return label;}
- int getPosX(){return PosX;}
- int getPosY(){return PosY;}
- bool getAlive(){return alive;}
- int getLives(){return lives;}
- void respawned(){alive = true;}
- void died(){alive = false;}
- void minusLife(){lives--;}
- void kill(){kills++;}
- int getKills(){return kills;}
- int getCD(){return currentDirection;}
- void nothing() {}
- void setPos(int x,int y)
- {
- PosX = x;
- PosY = y;
- }
- char fire(char (*Bfield), int m, int n, ofstream &output) override
- {
- int targetX, targetY;
- int directionX, directionY;
- //array to store BlueThunder's firing directions
- int directions[8][2] = {
- {0, 1}, // Up
- {1, 1}, // Up-Right
- {1, 0}, // Right
- {1, -1}, // Down-Right
- {0, -1}, // Down
- {-1, -1}, // Down-Left
- {-1, 0}, // Left
- {-1, 1} // Up-Left
- };
- //Set X and Y positions of BlueThunder's next shot
- do{
- directionX = directions[currentDirection][0];
- directionY = directions[currentDirection][1];
- targetX = PosX + directionX;
- targetY = PosY + directionY;
- //Set next direction for shot in the next turn
- currentDirection++;
- if (currentDirection == 8) {
- currentDirection = 0;
- }
- }while(*((Bfield + targetY * n) + targetX*2) == '|' || targetY == 0 || targetY == m-1);// Check for Boundaries
- // If shot hits another robot
- if(*((Bfield + targetY * n) + targetX*2) != '-' && *((Bfield + targetY * n) + targetX*2) != '*')
- {
- cout << getName() << " hit another robot at (" << directionX << "," << directionY << ") from it's own position!" << endl;
- *((Bfield + targetY * n) + targetX*2) = '*';
- output << getName() << " hit another robot at (" << directionX << "," << directionY << ") from it's own position!" << endl;
- *((Bfield + targetY * n) + targetX*2) = '*';
- kill();
- return *Bfield;
- }
- cout << getName() << " hit nothing at (" << directionX << "," << directionY << ") from it's own position" << endl;
- output << getName() << " hit nothing at (" << directionX << "," << directionY << ") from it's own position" << endl;
- { return {}; }
- }
- char move(char (*Bfield), int m, int n, ofstream &output){ return {};}
- char look(char (*Bfield), int m, int n, ofstream &output){ return {};}
- char step(char (*Bfield), int m, int n, int targetX, int targetY, ofstream &output){ return {};}
- };
- ///Madbot: Shoots - Upgrade: RoboTank
- class MadBot : public ShootingRobot {
- private:
- string type, name;
- char label;
- int kills, lives;
- int PosX, PosY;
- bool alive;
- public:
- MadBot(string Rname, char Rlabel, int Rlives, int x, int y)
- {
- type = "MadBot";
- name = Rname;
- label = Rlabel;
- kills = 0;
- lives = Rlives;
- PosX = x;
- PosY = y;
- alive = true;
- }
- string getType(){return type;}
- string getName(){return name;}
- char getLabel() {return label;}
- int getPosX() {return PosX;}
- int getPosY() {return PosY;}
- bool getAlive(){return alive;}
- int getLives(){return lives;}
- void respawned(){alive = true;}
- void died(){alive = false;}
- void minusLife(){lives--;}
- void kill(){kills++;}
- int getKills(){return kills;}
- void nothing() {}
- void setPos(int x,int y)
- {
- PosX = x;
- PosY = y;
- }
- char fire(char (*Bfield), int m, int n, ofstream &output) override
- {
- int shootX, shootY;
- int PosX = getPosX();
- int PosY = getPosY();
- do{
- shootX = -1 + (1-(-1)) * rand() % 3;
- shootY = -1 + (1-(-1)) * rand() % 3;
- }while((shootX == 0 && shootY == 0) || PosX + shootX == 0 || (PosX + shootX)*2 == n-1 || PosY + shootY == 0 || PosY + shootY == m-1);
- int shootCoordX = PosX + shootX;
- int shootCoordY = PosY + shootY;
- if(*((Bfield + shootCoordY * n) + shootCoordX*2) != '-' && *((Bfield + shootCoordY * n) + shootCoordX*2) != '*')
- {
- cout << getName() << " hits another robot at (" << shootX << "," << shootY << ") from it's own position" << endl;
- *((Bfield + shootCoordY * n) + shootCoordX*2) = '*';
- output << getName() << " hits another robot at (" << shootX << "," << shootY << ") from it's own position" << endl;
- *((Bfield + shootCoordY * n) + shootCoordX*2) = '*';
- kill();
- return *Bfield;
- }
- cout << getName() << " shot and hit nothing at (" << shootX << "," << shootY << ") from it's own position" << endl;
- output << getName() << " shot and hit nothing at (" << shootX << "," << shootY << ") from it's own position" << endl;
- return *Bfield;
- }
- char move(char (*Bfield), int m, int n, ofstream &output){ return {};}
- char look(char (*Bfield), int m, int n, ofstream &output){ return {};}
- char step(char (*Bfield), int m, int n, int targetX, int targetY, ofstream &output){ return {};}
- };
- ///RoboTank: Shoots - Upgrade: UltimateRobot
- class RoboTank : public ShootingRobot {
- private:
- string type, name;
- char label;
- int kills, lives;
- int PosX, PosY;
- bool alive;
- public:
- RoboTank(string Rname, char Rlabel, int Rlives, int x, int y)
- {
- type = "RoboTank";
- name = Rname;
- label = Rlabel;
- kills = 0;
- lives = Rlives;
- PosX = x;
- PosY = y;
- alive = true;
- }
- string getType(){return type;}
- string getName(){return name;}
- char getLabel(){return label;}
- int getPosX(){return PosX;}
- int getPosY(){return PosY;}
- bool getAlive(){return alive;}
- int getLives(){return lives;}
- void respawned(){alive = true;}
- void died(){alive = false;}
- void minusLife(){lives--;}
- void kill(){kills++;}
- int getKills(){return kills;}
- void nothing() {}
- void setPos(int x,int y)
- {
- PosX = x;
- PosY = y;
- }
- char move(char (*Bfield), int m, int n, ofstream &output){ return {};}
- char fire(char (*Bfield), int m, int n, ofstream &output){
- return ShootingRobot::fire(Bfield,m,n, output);
- }
- char look(char (*Bfield), int m, int n, ofstream &output){ return {};}
- char step(char (*Bfield), int m, int n, int targetX, int targetY, ofstream &output){ return {};}
- };
- ///TerminatorRoboCop: Looks, Moves, Steps and Shoots - Upgrade: UltimateRobot
- class TerminatorRoboCop : public SeeingRobot, public MovingRobot, public SteppingRobot, public ShootingRobot{
- private:
- string type, name;
- char label;
- int kills, lives;
- int PosX, PosY;
- bool alive;
- public:
- string getType(){return type;}
- string getName(){return name;}
- char getLabel(){return label;}
- int getPosX(){return PosX;}
- int getPosY(){return PosY;}
- bool getAlive(){return alive;}
- int getLives(){return lives;}
- void respawned(){alive = true;}
- void died(){alive = false;}
- void minusLife(){lives--;}
- void kill(){kills++;}
- int getKills(){return kills;}
- void nothing() {}
- void setPos(int x,int y)
- {
- PosX = x;
- PosY = y;
- }
- TerminatorRoboCop(string Rname, char Rlabel, int Rlives, int x, int y)
- {
- cout << endl << Rname << " has been upgraded to TerminatorRoboCop!" << endl;
- type = "TerminatorRoboCop";
- name = Rname;
- label = Rlabel;
- kills = 0;
- lives = Rlives;
- PosX = x;
- PosY = y;
- alive = true;
- }
- char move(char (*Bfield), int m, int n, ofstream &output){
- return MovingRobot::move(Bfield,m,n, output);
- }
- char fire(char (*Bfield), int m, int n, ofstream &output){
- return ShootingRobot::fire(Bfield,m,n, output);
- }
- char look(char (*Bfield), int m, int n, ofstream &output){
- return SeeingRobot::look(Bfield,m,n, output);
- }
- char step(char (*Bfield), int m, int n, int targetX, int targetY, ofstream &output){
- return SteppingRobot::step(Bfield,m,n, targetX, targetY, output);
- }
- };
- ///UltimateRobot: Looks, Moves and Shoots - Upgrade: MAX
- class UltimateRobot : public SeeingRobot, public MovingRobot, public SteppingRobot, public ShootingRobot {
- private:
- string type, name;
- char label;
- int kills, lives;
- int PosX, PosY;
- bool alive;
- public:
- string getType(){return type;}
- string getName(){return name;}
- char getLabel(){return label;}
- int getPosX(){return PosX;}
- int getPosY(){return PosY;}
- bool getAlive(){return alive;}
- int getLives(){return lives;}
- void respawned(){alive = true;}
- void died(){alive = false;}
- void minusLife(){lives--;}
- void kill(){kills++;}
- int getKills(){return kills;}
- void nothing() {}
- void setPos(int x,int y)
- {
- PosX = x;
- PosY = y;
- }
- UltimateRobot(string Rname, char Rlabel, int Rlives, int x, int y)
- {
- cout << endl << Rname << " has been upgraded to UltimateRobot!" << endl;
- type = "UltimateRobot";
- name = Rname;
- label = Rlabel;
- kills = 0;
- lives = Rlives;
- PosX = x;
- PosY = y;
- alive = true;
- }
- char move(char (*Bfield), int m, int n, ofstream &output){
- return MovingRobot::move(Bfield,m,n, output);
- }
- char fire(char (*Bfield), int m, int n, ofstream &output){
- return ShootingRobot::fire(Bfield,m,n, output);
- }
- char look(char (*Bfield), int m, int n, ofstream &output){
- return SeeingRobot::look(Bfield,m,n, output);
- }
- char step(char (*Bfield), int m, int n, int targetX, int targetY, ofstream &output){ return {};}
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement