[Solved] C Program Help Need Dier Help Create Class Hierarchy Game Actors Use Stack Template Along Q37191905
C++ Program Help, need dier help!!
Create a class hierarchy for game actors Use the Stack(template) along with the Command Pattern to create an undoableaction Simulate a battle between two Actors with the ability toundo moves
Program Overview:
This program will consist of several Actors (Knight, Ghost, andWarrior) that are capable of performing attacks and healing. Youwill run a simulation of a battle between these actors with arunning “history” of moves. The simulation will first prompt you toselect a player and an opponent. Once the two are set, and a battlehas begun, There will be 3 options available: 1) Player attacksOpponent 2) Opponent attacks Player 3) Undo last move The first twowill invoke an attack from one player to another. The third optionwill undo the last move that was done with a technique discussedbelow. After each choice, the updated player information is printed(Player, type, health), (Opponent, type health) This will proceeduntil a character is dead.
Undoable Commands Overview: ——– In manymodern applications, we have the option to undo certain actions weperform. For example in Microsoft word, we can hit CTRL+Z aftertyping some text to bring the document back to it’s original state.In practice this is done with the use of a Stack ADT holdingreferences to “commands”. A command comes from the Command Pattern,one of the many Design Patterns which came out of this (nowclassic) book:https://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional-ebook/dp/B000SEIBB8This book outlines 23 patterns to solve commonly recuring problemsin software engineering. One of those patterns is the Commandpattern. (I highly reccomend picking up a copy of this book onceyou are more comfortable with C++. Design patterns are fundamentalto architecting non-trivial software systems) The essense of thecommand pattern is to represent some action (think functionexecuting) as an object. This is done in the following way: Anabstract class is created known as Command (or ICommand with the Istanding for interface). This class will have an virtual Executemethod. Because Command is abstract, we never create instances ofit. Instead we derive from it to create “concrete” Commands. Theseclasses which derive from ICommand will have to implement theExecute method. Polymorphism allows us to have collections ofcommands which we can execute (by invoking the Execute() method),and having each command do it’s own thing. For example imagine wehave some application state (a global integer in this case). Wewant some Commands to change that state. So we create an ICommandand two concrete commands which derive form ICommand. We can thenhave a vector of commands which we can execute and each one willperform it’s own specific implementation of Execute.
// int state = 0; class ICommand{ public: virtual voidExecute()=0; }; class IncrementCommand: public ICommand { public:void Execute(){ state++; } }; class DecrementCommand: publicICommand{ public: void Execute(){ state–; } }; int main(void){vector<ICommand*> commands; commands.push_back(newIncrementCommand()); commands.push_back(new IncrementCommand());commands.push_back(new DecrementCommand()); for(auto c : commands){c->Execute(); } cout << state << endl; // Should be1; }
// This is the essence of the command pattern, but it’s notquite what we need. If we want undo functionality, we also need away to revert the state to how it was before the action. For thiswe can modify our example above by having our Commands not onlyExecute, but also Undo to reverse the effects Execute had onstate:
// int state = 0; class ICommand{ public: virtual voidExecute()=0; }; class IUndoableCommand: public ICommand{ public:virtual void Undo()=0; }; class IncrementCommand: publicIUndoableCommand { public: void Execute(){ state++; } void Undo(){state–; } }; class DecrementCommand: public IUndoableCommand{public: void Execute(){ state–; } void Undo(){ state++; } }; intmain(void){ vector<IUndoableCommand*> commands;commands.push_back(new IncrementCommand()); commands.push_back(newIncrementCommand()); for(auto c : commands){ c->Execute(); }commands[1]->Undo(); commands[0]->Undo(); cout << state<< endl; // Should be 0; }
// This command pattern takes us part of the way towards undofunctionality but we need to consider how we can keep a history ofcommands with the ability to undo them. Notice that currently Ihave to Undo the commands in the opposite order I executed them.This should remind you of the Stack ADT we discussed, where we havea LIFO processing order (Last In First Out). If we keep a stack ofstate changes (i.e. IUndoableCommands), we have a running historyof everything we did to alter our state. As actions are executed,they are pushed onto the stack with the last action being on thetop. If we need to undo an action we can look at the top of ourstack (stack.Top()) and call the Undo method of thatIUndoableCommand object. After we pop it off the stack (andpotentially delete it if it’s a pointer). We can repeat this forall the elements in our stack until we run through the entirehistory of state changes to get back to the initial condition.Example: int main(void){ stack<IUndoableCommand*> stack;IUndoableCommand* command1 = new IncrementCommand();IUndoableCommand* command2 = new IncrementCommand();
// Perform increment (state should be 1) command1->Execute();stack.Push(command1);
// Perform another increment (state should be 2)command2->Execute(); stack.Push(command2);
// Undo last action (state should be 1); stack.Top()->Undo();stack.Pop(); cout << state << endl;
// State should be 1 } The last thing we need to do is to makesure we manage memory We want a CommandManager class to take careof maintaining a stack, and one that will release the resourcesonce we are done working with this stack. See Commands4.cpp for thefinal example:——————————————————————————————-Classes for our Battle simulator:
We will incorporate the Undoable command pattern to help uspreserve a history of BattleMoves done by our Actors. Our Projectwill be broken up into several classes The hierarchies will look asfollows: Actor – General game character with the following ADTspec: public: Actor(int health, string type); DoMove(MoveManagermgr, MoveType, Actor* other) Hit(int damage); //public method tohit THIS actor with damage Heal(int amount); //public method toheal this actor with an amount GetMoves() //vector of MoveTypesIsDead() protected: string type; int health; vector<MoveType>From Actor we will derive: Ghost Will have AttackOne and Heal invector of MoveTypes, and 100 health Knight Will have AttackTwo andHeal in vector of MoveTypes, and 100 health Warrior Will haveAttackOne and AttackTwo in vector of MoveTypes, and 100 healthBattleMove – A battle move which can either hurt the opponent, orheal the player. ADT spec: private: Actor* self Actor* otherpublic: void virtual Execute()=0; void virtual Undo()=0; (Note:Each battle move will have an Execute method. That execute willeither work on self (if heal) or other (if attack)) Therefore whenwe construct a move we should pass in the self and other pointersto any kind of concrete BattleMove implementation. From BattleMovewe will derive:
AttackOne Will generate a random damage between 10 and 15,Execute will call the Hit method of other and save the actualDamagedone in a local variable. Undo will call the Heal method of otherwith the saved actualDamage variable (to undo the damage)
AttackTwo Will generate a random damage between 0 and 25, Samelogic as attack one
Heal Will generate a random heal amount between 10 and 15Execute will call Heal on self with the actual heal amount and saveit in a member variable. Undo will call Hit on self to undo theHeal (with amount stored in the member variable)
An enumeration or array of const char *’s to go with BattleMove(MoveType), An enumeration or array of const char *’s to go withActor types (ActorType) These will make it easy to set up aswitch/case statement to instantiate the correct moves and actorsbased on user input.
MoveManager – Analogous to our CommandManager class. Will holdon to a stack (use the template implementation of stack from thelecture code on Templates) of BattleMove*’s and will be referencedby every actor. Whenever an actor performs a move, it will gothrough the MoveManager’s method so that history is recorded on thestack. Move manager will have DoMove() and UndoLastMove() methodsdefined, a constructor to initialize a stack, as well as adestructor to delete all the BattleMove*’s still in the stack. AMoveManager’s DoMove will switch based on the MoveType passed in,create an new BattleMove object of that type, execute the move, andpush the move onto the stack. UndoLastMove will call undo on theTop() element of the stack, pop it off the stack, and delete theobject. Useexception handling logic (try/catch) to check for an empty stack.In the case that the stack is empty, make sure you print anappropriate message to the user (e.g. “No moves to undo”).
Expert Answer
Answer to C++ Program Help, need dier help!! Create a class hierarchy for game actors Use the Stack (template) along with the Comm… . . .
OR

