Game Development Reference
In-Depth Information
while (shouldEnd == false)
{
GivePlayerOptions();
string playerInput;
GetPlayerInput(playerInput);
PlayerOptions selectedOption = EvaluateInput(playerInput);
shouldEnd = selectedOption == PlayerOptions::Quit;
}
}
The UpdateOnOption method from Chapter 10 has been removed, as the code from that method has
been moved into the MoveOption class. This allows you to remove the call to that method from the
RunGame class.
Summary
This chapter has introduced polymorphism. This allows you to write code that is more flexible and
reusable. You have seen that base class pointers combined with virtual methods allow a programmer
to create interfaces and code that allows programs to evaluate their behavior at runtime for the
correct course of action. You have also seen how the dynamic_cast allows you to ensure that
you can safely move from one type of object pointer to another. Pure virtual functions were then
introduced, along with an example of how you can create abstract classes that enforce specific
methods to be overridden in classes that derive from that class.
The chapter ended with a practical example of how you can use polymorphism to improve your
code in a number of ways. The Option class creates a simple interface for in-game options. The
MoveOption and QuitOption classes show how you can use virtual methods to create types to
represent specific actions. Finally, you saw that polymorphism can be used to write methods that are
reusable and easily maintainable. New options can be added to Text Adventure without the need to
alter the Game GivePlayerOptions and EvaluateInput methods.
 
Search WWH ::




Custom Search