Using the strategy pattern
The strategy design pattern is a very smart way of designing code. In games, this is mostly used for the AI component. In this pattern, we define a large number of algorithms and have all of them from a common interface signature. Then at runtime, we can change the clients of the algorithms. So in effect, the algorithms are independent of the clients.
Getting ready
To work through this recipe, you will need a machine running Windows. You also need to have a working copy of Visual Studio installed on your Windows machine. No other prerequisites are required.
How to do it…
In this recipe, we will find out how easy it is to implement the strategy pattern:
Open Visual Studio.
Create a new C++ project.
Select a Win32 console application.
Add a
Source.cpp
file.Add the following lines of code to it:
#include <iostream> #include <conio.h> using namespace std; class SpecialPower { public: virtual void power() = 0; }; class Fire : public SpecialPower { public: ...