In its discussion of recursive backtracking, Chapter 9 offered a general outline for creating two-player games based on the minimax algorithm. At that point in the text, it was difficult to encapsulate the code for the minimax algorithm in a way that would allow the code to be shared by many different games. The combination of templates and inheritance make it much easier to define a base class for two-player games that is easy to extend for particular games. Design and implement a template class called Two Player Game that takes the type used to represent a move as a template parameter. Specific games can then extend that class by overriding the methods that pertain only to that game, leaving intact the methods that implement the minimax algorithm itself. For example, the class definition for the simple Nim game described in Chapter 9 would look like this:
The NimMove type is defined in the same way that the Move type was in the implementation of the Nim game. In Chapter 9, the code for the minimax algorithm required that type to be named Move. Specifying template parameters makes it possible to use a name that is more descriptive. Test your implementation of the TwoPlayerGame class by completing the definition of the Nim class. Once you have a working implementation of Nim, demonstrate the flexibility of your class by using it to implement one of the other two-player games described in the exercises in Chapter 9.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here