Implement a simple game called “Tiger, Deer, Grass”.The rule of this forest(like in every forest) is if the tiger meets deer, it eats the deer. If the deer meets grass, it eats the grass and if the...

1 answer below »
Implement a simple game called “Tiger, Deer, Grass”.The rule of this forest(like in every forest) is if the tiger meets deer, it eats the deer. If the deer meets grass, it eats the grass and if the tiger meets grass, it doesn’t eat the grass. You need to do the following things to complete the game. • Trace the path of two objects in a forest. • Based on the path of two objects, determine whether the first object meets the second one. • Output the forest with the path of the objects traced out. The rest of the details are attached in the Zip files.


forest.h #include #include "object.h" using namespace std; //Template class forest with two generic types t1 and t2 template class forest { private: int rows;//rows of the grid int cols;//columns of the grid string ** grid;//pointer to pointer to point the dynamic 2d array of string t1 * obj1;//first object of the forest t2 * obj2;//second object of the forest bool doTheyMeet(int);//private member function to determine whether two objects meet int displayForest(int,int);//private function to print the forest public: forest(int,int,t1 *,t2*);//constructor to initialize the forest. void simulate();//function that handles all the movement, print and other operations of the forest ~forest();//destructor to deallocate the dynamic variables. }; /*Implementation of constructors and member functions begins*/ /*Implement the constructor*/ /**************************************************** *****************************************************/ template void forest::simulate() { cout <><"simulation begins="" for="" forest="" with="">getName()<" and="">getName()move(grid,rows,cols,0); obj2->move(grid,rows,cols,0); obj1->setCurrentPosition(obj1->getStartingPosition().x,obj1->getStartingPosition().y); obj2->setCurrentPosition(obj2->getStartingPosition().x,obj2->getStartingPosition().y); //cout getCurrentPosition().xgetCurrentPosition().ygetCurrentPosition().xgetCurrentPosition().ygetSymbol()=='T' && obj2->getSymbol()=='G') cout getName()<" finds="">getName()<" but="" tiger="" doesn't="" eat="">getName()<" eats="">getName()getName()< "="" couldn't="" find="">getName()<"simulation><> forest::~forest() { delete obj1; delete obj2; for(int i=0;i #include "forest.h" #include "object.h" using namespace std; int main() { position posTiger,posDeer,posGrass;//position for tiger, deer and grass. tiger *t;//pointer to point tiger object deer *d;//pointer to point deer object grass *g;//pointer to point grass object /*Creating first forest of tiger and deer*/ posTiger.x=3;//starting position x for tiger posTiger.y=3;//starting position y for tiger t=new tiger("tiger1",'T',"UULLLDD",posTiger);//declaring a new tiger object posDeer.x=1;//starting position x for deer posDeer.y=5;//starting position y for deer d=new deer("deer1",'D',"LLUUUU",posDeer);//declaring a new deer object forest *f1 = new forest(7,7,t,d);//createing a new forest of size 7,7 with tiger and deer object f1->simulate();//simulating the forest operation delete f1;//deallocating the forest. /********************************************/ /*Creating second forest of deer and grass*/ posDeer.x=1; posDeer.y=0; d = new deer("deer2",'D',"RRRDRD",posDeer); posGrass.x=0; posGrass.y=4; g = new grass("grass1",'G',"LDDLDR",posGrass); forest *f2=new forest(5,5,d,g); f2->simulate(); delete f2; /********************************************/ /*Creating third forest of tiger and grass*/ posTiger.x=1; posTiger.y=2; t=new tiger("tiger2",'T',"LDRRRR",posTiger); posGrass.x=0; posGrass.y=1; g=new grass("grass2",'G',"RRDD",posGrass); forest *f3=new forest(3,5,t,g); f3->simulate(); delete f3; /*******************************************/ /*Creating fourth forest of tiger and deer*/ posTiger.x=4; posTiger.y=1; t=new tiger("tiger3",'T',"DDLDRRRUUUUR",posTiger); posDeer.x=0; posDeer.y=6; d=new deer("deer3",'D',"RRDDDDDLLLD",posDeer); forest *f4=new forest(8,10,t,d); f4->simulate(); delete f4; /*******************************************/ return 0; } __MACOSX/._main.cpp makefile # Makefile assignment #6 OBJS= main.o CC= g++ CCFLAGS= -Wall -Wextra -pedantic -std=c++11 -g DEPS= object.h forest.h all: main main: $(OBJS) $(CC) $(CCFLAGS) -o main $(OBJS) main.o:main.cpp $(DEPS) $(CC) $(CCFLAGS) -c main.cpp # ----- # clean by removing object files. clean: rm $(OBJS) __MACOSX/._makefile object.h #ifndef OBJECT_H #define OBJECT_H #include using namespace std; //Struct position to hold x and y position within the grid struct position { int x; int y; }; //object class that will be the base class of three objects. Tiger, deer and grass class object { private: string name;//name of the object string path;//path of the object char symbol;//symbol of the object position currentPosition;//current position of the object position startingPosition;//starting position of the object public: object(string,char,string, position);//constructor to initialize the object with name, symbol, path and starting position object(object&);//copy constructor void move(string** &,int,int,int);//move function that traces the movement of objects /*Getter functions*/ string getName(); char getSymbol(); string getPath(); position getCurrentPosition(); position getStartingPosition(); /*****************************/ /*Mutator function to set the current position.*/ void setCurrentPosition(int,int); void setCurrentPosition(char); /*********************************************/ }; //Tiger class class tiger: public object { public: tiger(string n,char s,string p,position pos): object(n,s,p,pos){} }; //Deer class class deer: public object { public: deer(string n,char s,string p,position pos): object(n,s,p,pos){} }; //Grass class class grass: public object { public: grass(string n,char s,string p,position pos):object(n,s,p,pos){} //grass(grass& g): object(g){} }; /*Implementation of constructors and member functions of object class*/ object::object(string n,char s,string p, position pos) { name=n; path=p; symbol=s; currentPosition=pos; startingPosition=pos; } object::object(object& obj) { name=obj.name; path=obj.path; } /*Implement the move function*/ /**************************************************** *****************************************************/ string object::getName() { return name; } char object::getSymbol() { return symbol; } string object::getPath() { return path; } position object::getCurrentPosition() { return currentPosition; } position object::getStartingPosition() { return startingPosition; } void object::setCurrentPosition(int x,int y) { currentPosition.x=x; currentPosition.y=y; } void object::setCurrentPosition(char nextDirection) { switch(nextDirection) { case 'U': --currentPosition.x; break; case 'D': ++currentPosition.x; break; case 'L': --currentPosition.y; break; case 'R': ++currentPosition.y; } } #endif __MACOSX/._object.h Assignment8_instruction.pdf CS202: Assignment 8(Recursion, Exception handling and templates) Fall 2021 1 Problem Introduction In this assignment, you will use your knowledge from recursion, exception handling and tem- plates. You will be implementing various functions recursively, perform exception handling for the edge cases and also complete the implementation of a template class. Now let’s talk about the problem. We are going to implement a simple game in this assignment. I’m not sure if this game has got a name already but I’m going to name the game “Tiger, Deer, Grass”. The game is based on a forest implemented through a 2D array of strings. A forest can have only two of the three objects: tiger, deer and grass, at a time. Now, these objects move around the forest following a path that’s given to them. In real world, grass can’t move on its own(It can grow, of course, but we are talking about moving here), but for the sake of this assignment, let’s assume there is some unseen hand that moves the grass just like tiger and deer. The rule of this forest(like in every forest) is if the tiger meets deer, it eats the deer. If the deer meets grass, it eats the grass and if the tiger meets grass, it doesn’t eat the grass. You need to do the following things to complete the game. • Trace the path of two objects in a forest. • Based on the path of two objects, determine whether the first object meets the second one. • Output the forest with the path of the objects traced out. Next, let’s talk about the classes and files. 2 Classes and Files 2.1 Object class This is the base class for all the three objects: tiger, deer and grass. Note that this is not an abstract class. Following is the class diagram for object class. The class has been given to you in object.h file. object -name: string -path: string -symbol: char -currentPosition: position -startingPosition: position +object(string,char, string, position) +object(object ) +move(string** &,int,int,int):void +getName():string +getSymbol():char +getPath():string +getStartingPosition():position +getCurrentPosition():position +setCurrentPosition(int,int):void +setCurrentPosition(char):void Member variables • name: Name of the object. 1 • symbol: Symbol of the object. ’T’ for Tiger, ’D’ for Deer and ’G’ for Grass. • path: String that contains the path the object should take. Each character of the string can be either ’U’ for up, ’D’ for down, ’L’ for left and ’R’ for right. Example: “UULDDR”. You should extract each character for next movement. • startingPosition: A struct variable of type position to denote the starting position of the object. • currentPosition: A struct variable of type position to denote the current position of the object. Member functions and constructors • object(string, char, string,position): Constructor to initialize the object with name, symbol, path and the starting position. • object(object): A copy constructor to do a deep copy. • getName(): Returns the name of the object. • getSymbol(): Returns the symbol of the object. • getPath(): Returns the path of the object. • getStartingPosition(): Returns the starting position of the object. • getCurrentPosition(): Returns the current position of the object. • setCurrentPosition(int,int): Sets the current position of the object with two integer value passed as parameters. • setCurrentPosition(char): Takes a single character from path string and sets the new position of the object based on the direction(U,D,L or R). • move(string** &, int,int, int): This function implements the movement of the object based on the path. You need to implement this function by yourself. Make this a recursive function. You need to do the following things inside the function. It takes the grid, the rows and cols of the grid and the starting index for the path to extract the movement direction. We will discuss what you need to do inside this function next. Implementation of recursive move function. • If the current position of the object is outside the boundary of the grid, throw a string “⟨ object⟩ went out of the forest” where ⟨object⟩ is the name of the object. You can get the name of the object using function getName(). Then catch a string exception, print that thrown string and stop the function execution. • If the current position of the object is within the boundary of the grid then: If that current position contains ‘X’ replace it with the symbol of the object using getSym- bol(). If that current position already has some other object symbol then concatenate it with the current object’s symbol. • Print “⟨object⟩ is moving” where ⟨object⟩ is the object name. Use getName(). • To get the next direction from the path, use string.at(index) function. This function throws out of range exception if the index provided is out of range. Once all the characters from path is read, string.at(index) should throw the exception. Catch this exception and print “⟨object⟩ finished moving” where ⟨object⟩ is the object name. Then stop the function execution. 2.2 Tiger class This class is publicly derived from object class. It just has a constructor that invokes the constructor of the base class. You don’t need to add anything to this class. 2.3 Deer class This class is publicly derived from object class. It just has a constructor that invokes the constructor of the base class. You don’t need to add anything to this class. 2 2.4 Grass class This class is publicly derived from object class. It just has a constructor that invokes the constructor of the base class. You don’t need to add anything to this class. 2.5 Forest class Since a forest can have two of the three objects at a time, it makes sense to make forest a template class with two generic types. Below is the class diagram for the forest class. forest⟨t1, t2⟩ -rows: int -cols: int -grid: string** -obj1: t1* -obj2: t2* -doTheyMeet(int): bool -displayForest(int,int): int +forest(int,int,t1*,t2*) +simulate():void +~forest() Member variables: • grid: A pointer to pointer of type string that is used to point a dynamic 2D array
Answered 8 days AfterNov 08, 2021

Answer To: Implement a simple game called “Tiger, Deer, Grass”.The rule of this forest(like in every forest) is...

Kamal answered on Nov 16 2021
138 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here