lundi 29 juin 2015

Avoid type checking when function dependant on two distinct classes

I'm trying to code a game hub of sorts that is basically a collection of board games or games that can be easily played at the command line. (Tic Tac Toe, Connect Four, etc.) The user also has the option of making any of the two players computers. If I were making only Tic Tac Toe this wouldn't be an issue since I could just have "move" implemented in the human and computer subclass of Player and then invoke the correct one polymorphically, but with the addition of other games I'm not sure how to get away from type checking. For instance, if move is a function within player, how else other than through type checking would the function know whether to move according to the rules of Tic Tac Toe or Connect Four?

I was toying with the idea of having a separate move class with different subclasses for each scenario but I'm not sure if that's correct or how to implement it.

This is being written in C++ by the way.

Aucun commentaire:

Enregistrer un commentaire